Beispiel #1
0
def error_of_number_of_positional_arguments_uses_correct_singular_wording():
    assert_raises_regexp(
        TypeError, r"takes 1 positional argument but 2 were given",
        lambda: dodge.data_class("User", ["username"])("bob", "password1"))
    assert_raises_regexp(TypeError,
                         r"takes 0 positional arguments but 1 was given",
                         lambda: dodge.data_class("User", [])("bob"))
def flat_list_for_nested_data_classes_contains_all_field_values_as_flat_list():
    Profile = dodge.data_class("Profile", ["bio"])

    User = dodge.data_class("User", ["username", dodge.field("profile", type=Profile)])

    user = User("bob", Profile("I'm Bob."))

    assert_equal(["bob", "I'm Bob."], dodge.obj_to_flat_list(user))
def can_convert_nested_data_classes_to_and_from_flat_list():
    Profile = dodge.data_class("Profile", ["bio"])

    User = dodge.data_class("User", ["username", dodge.field("profile", type=Profile)])

    user = User("bob", Profile("I'm Bob."))
    serialised_user = dodge.obj_to_flat_list(user)
    unserialised_user = dodge.flat_list_to_obj(serialised_user, User)
    assert_equal(user, unserialised_user)
Beispiel #4
0
def error_of_number_of_positional_arguments_uses_correct_singular_wording():
    assert_raises_regexp(
        TypeError, r"takes 1 positional argument but 2 were given",
        lambda: dodge.data_class("User", ["username"])("bob", "password1")
    )
    assert_raises_regexp(
        TypeError, r"takes 0 positional arguments but 1 was given",
        lambda: dodge.data_class("User", [])("bob")
    )
def can_convert_nested_data_classes_to_and_from_string():
    Profile = dodge.data_class("Profile", ["bio"])

    User = dodge.data_class("User", ["username", dodge.field("profile", type=Profile)])

    user = User("bob", Profile("I'm Bob."))
    serialised_user = dodge.dumps(user)
    unserialised_user = dodge.loads(serialised_user, User)
    assert_equal(user, unserialised_user)
def flat_list_for_nested_data_classes_contains_all_field_values_as_flat_list():
    Profile = dodge.data_class("Profile", ["bio"])

    User = dodge.data_class("User", [
        "username",
        dodge.field("profile", type=Profile),
    ])

    user = User("bob", Profile("I'm Bob."))

    assert_equal(["bob", "I'm Bob."], dodge.obj_to_flat_list(user))
def can_convert_nested_data_classes_to_and_from_dict():
    Profile = dodge.data_class("Profile", ["bio"])

    User = dodge.data_class("User", [
        "username",
        dodge.field("profile", type=Profile),
    ])

    user = User("bob", Profile("I'm Bob."))
    serialised_user = dodge.obj_to_dict(user)
    unserialised_user = dodge.dict_to_obj(serialised_user, User)
    assert_equal(user, unserialised_user)
Beispiel #8
0
def instances_of_data_class_are_equal_iff_all_fields_have_the_same_value():
    User = dodge.data_class("User", ["username", "password"])

    assert User("bob", "password1") == User("bob", "password1")
    assert not (User("jim", "password1") == User("bob", "password1"))
    assert not (User("bob", "password1") == User("bob", "password2"))
    assert not (User("jim", "password1") == User("bob", "password2"))
Beispiel #9
0
def field_value_is_shown_if_it_has_default_value():
    User = dodge.data_class(
        "User",
        ["username", dodge.field("password", default="password1")])

    user = User("bob")
    assert_equal("User('bob', password='******')", repr(user))
Beispiel #10
0
def keyword_only_field_is_shown_as_kwarg():
    User = dodge.data_class(
        "User",
        ["username", dodge.field("password", keyword_only=True)])

    user = User("bob", password="******")
    assert_equal("User('bob', password='******')", repr(user))
Beispiel #11
0
def data_class_arguments_can_be_passed_by_position_and_keyword():
    User = dodge.data_class("User", ["username", "salt", "password"])

    user = User("bob", "asf", password="******")
    assert_equal("bob", user.username)
    assert_equal("password1", user.password)
    assert_equal("asf", user.salt)
Beispiel #12
0
def instances_of_data_class_are_not_equal_to_other_types():
    User = dodge.data_class("User", ["username", "password"])

    assert not (User("bob", "password1") == "bob")
    assert not ("bob" == User("bob", "password1"))
    assert User("bob", "password1") != "bob"
    assert "bob" != User("bob", "password1")
Beispiel #13
0
def field_is_set_to_default_if_value_not_provided():
    User = dodge.data_class(
        "User",
        ["username", dodge.field("password", default="password1")])

    user = User("bob")
    assert_equal("password1", user.password)
Beispiel #14
0
def instances_of_data_class_are_not_equal_to_other_types():
    User = dodge.data_class("User", ["username", "password"])
    
    assert not (User("bob", "password1") == "bob")
    assert not ("bob" == User("bob", "password1"))
    assert User("bob", "password1") != "bob"
    assert "bob" != User("bob", "password1")
Beispiel #15
0
def error_is_raised_if_init_value_is_missing():
    User = dodge.data_class("User", ["username", "password"])
    
    assert_raises_regexp(
        TypeError, "^Missing argument: 'password'$",
        lambda: User("bob")
    )
Beispiel #16
0
def field_with_default_is_shown_as_kwarg():
    User = dodge.data_class(
        "User",
        ["username", dodge.field("password", default="password1")])

    user = User("bob", "password2")
    assert_equal("User('bob', password='******')", repr(user))
def converting_object_to_dict_preserves_ordering():
    User = dodge.data_class("User", ["username", "salt", "password", "email_address"])

    user = User("bob", "!%ksdg", "password1", "*****@*****.**")
    result = dodge.obj_to_dict(user)

    assert_equal(["username", "salt", "password", "emailAddress"], list(result.keys()))
Beispiel #18
0
def data_class_arguments_can_be_passed_by_position_and_keyword():
    User = dodge.data_class("User", ["username", "salt", "password"])
    
    user = User("bob", "asf", password="******")
    assert_equal("bob", user.username)
    assert_equal("password1", user.password)
    assert_equal("asf", user.salt)
Beispiel #19
0
def error_is_raised_if_too_many_positional_arguments_are_passed_to_init():
    User = dodge.data_class("User", ["username", "password"])
    
    assert_raises_regexp(
        TypeError, r"takes 2 positional arguments but 3 were given",
        lambda: User("bob", "password1", "salty")
    )
Beispiel #20
0
def instances_of_data_class_are_equal_iff_all_fields_have_the_same_value():
    User = dodge.data_class("User", ["username", "password"])
    
    assert User("bob", "password1") == User("bob", "password1")
    assert not (User("jim", "password1") == User("bob", "password1"))
    assert not (User("bob", "password1") == User("bob", "password2"))
    assert not (User("jim", "password1") == User("bob", "password2"))
Beispiel #21
0
def instances_of_data_class_are_not_equal_iff_any_fields_have_different_values():
    User = dodge.data_class("User", ["username", "password"])
    
    assert not (User("bob", "password1") != User("bob", "password1"))
    assert User("jim", "password1") != User("bob", "password1")
    assert User("bob", "password1") != User("bob", "password2")
    assert User("jim", "password1") != User("bob", "password2")
Beispiel #22
0
def field_can_be_configured_to_be_hidden_in_repr_if_it_has_default_value():
    User = dodge.data_class("User", [
        "username",
        dodge.field("password", default="password1", show_default=False)
    ])
    
    user = User("bob")
    assert_equal("User('bob')", repr(user))
def conversion_from_dict_to_obj_uses_items_as_constructor_args():
    User = dodge.data_class("User", ["username", "password"])

    input_dict = {"username": "******", "password": "******"}
    converted_user = dodge.dict_to_obj(input_dict, User)

    expected_user = User("bob", "password1")
    assert_equal(expected_user, converted_user)
def conversion_from_obj_to_dict_converts_underscores_to_camel_case():
    User = dodge.data_class("User", ["is_root"])

    input_user = User(is_root=True)
    result = dodge.obj_to_dict(input_user)

    expected_dict = {"isRoot": True}
    assert_equal(expected_dict, result)
def conversion_from_obj_to_dict_uses_hacktastic_fields_property():
    User = dodge.data_class("User", ["username", "password"])

    user = User("bob", "password1")
    result = dodge.obj_to_dict(user)

    expected_dict = {"username": "******", "password": "******"}
    assert_equal(expected_dict, result)
def unrecognised_fields_are_ignored():
    User = dodge.data_class("User", ["username"])

    input_dict = {"username": "******", "password": "******"}
    converted_user = dodge.dict_to_obj(input_dict, User)

    expected_user = User("bob")
    assert_equal(expected_user, converted_user)
def arguments_with_camelcase_names_are_converted_to_use_underscores():
    User = dodge.data_class("User", ["is_root"])

    input_dict = {"isRoot": True}
    converted_user = dodge.dict_to_obj(input_dict, User)

    expected_user = User(is_root=True)
    assert_equal(expected_user, converted_user)
Beispiel #28
0
def field_is_set_to_default_if_value_not_provided():
    User = dodge.data_class("User", [
        "username",
        dodge.field("password", default="password1")
    ])
    
    user = User("bob")
    assert_equal("password1", user.password)
Beispiel #29
0
def instances_of_data_class_are_not_equal_iff_any_fields_have_different_values(
):
    User = dodge.data_class("User", ["username", "password"])

    assert not (User("bob", "password1") != User("bob", "password1"))
    assert User("jim", "password1") != User("bob", "password1")
    assert User("bob", "password1") != User("bob", "password2")
    assert User("jim", "password1") != User("bob", "password2")
Beispiel #30
0
def copying_data_class_creates_distinct_object_with_same_field_values():
    User = dodge.data_class("User", ["username", "password"])

    original = User("bob", "password1")
    copy = dodge.copy(original)

    assert original is not copy
    assert_equal("bob", copy.username)
Beispiel #31
0
def field_can_be_configured_to_be_hidden_in_repr_if_it_has_default_value():
    User = dodge.data_class("User", [
        "username",
        dodge.field("password", default="password1", show_default=False)
    ])

    user = User("bob")
    assert_equal("User('bob')", repr(user))
Beispiel #32
0
def field_with_default_is_shown_as_kwarg():
    User = dodge.data_class("User", [
        "username",
        dodge.field("password", default="password1")
    ])
    
    user = User("bob", "password2")
    assert_equal("User('bob', password='******')", repr(user))
def unrecognised_fields_are_ignored():
    User = dodge.data_class("User", ["username"])

    input_dict = {"username": "******", "password": "******"}
    converted_user = dodge.dict_to_obj(input_dict, User)

    expected_user = User("bob")
    assert_equal(expected_user, converted_user)
Beispiel #34
0
def error_is_raised_if_kwarg_field_is_passed_as_positional_arg():
    User = dodge.data_class(
        "User",
        ["username", dodge.field("password", keyword_only=True)])

    assert_raises_regexp(TypeError,
                         "takes 1 positional argument but 2 were given",
                         lambda: User("bob", "password2"))
def arguments_with_camelcase_names_are_converted_to_use_underscores():
    User = dodge.data_class("User", ["is_root"])

    input_dict = {"isRoot": True}
    converted_user = dodge.dict_to_obj(input_dict, User)

    expected_user = User(is_root=True)
    assert_equal(expected_user, converted_user)
def conversion_from_dict_to_obj_uses_items_as_constructor_args():
    User = dodge.data_class("User", ["username", "password"])

    input_dict = {"username": "******", "password": "******"}
    converted_user = dodge.dict_to_obj(input_dict, User)

    expected_user = User("bob", "password1")
    assert_equal(expected_user, converted_user)
def conversion_from_obj_to_dict_uses_hacktastic_fields_property():
    User = dodge.data_class("User", ["username", "password"])

    user = User("bob", "password1")
    result = dodge.obj_to_dict(user)

    expected_dict = {"username": "******", "password": "******"}
    assert_equal(expected_dict, result)
def conversion_from_obj_to_dict_converts_underscores_to_camel_case():
    User = dodge.data_class("User", ["is_root"])

    input_user = User(is_root=True)
    result = dodge.obj_to_dict(input_user)

    expected_dict = {"isRoot": True}
    assert_equal(expected_dict, result)
Beispiel #39
0
def field_value_is_shown_if_it_has_default_value():
    User = dodge.data_class("User", [
        "username",
        dodge.field("password", default="password1")
    ])
    
    user = User("bob")
    assert_equal("User('bob', password='******')", repr(user))
Beispiel #40
0
def keyword_only_field_is_shown_as_kwarg():
    User = dodge.data_class("User", [
        "username",
        dodge.field("password", keyword_only=True)
    ])
    
    user = User("bob", password="******")
    assert_equal("User('bob', password='******')", repr(user))
Beispiel #41
0
def error_is_raised_if_copy_kwarg_is_not_field():
    User = dodge.data_class("User", ["username", "password"])
    
    original = User("bob", "password1")
    assert_raises_regexp(
        TypeError, "__init__ does not take keyword argument",
        lambda: dodge.copy(original, salt="salty")
    )
Beispiel #42
0
def copying_data_class_creates_distinct_object_with_same_field_values():
    User = dodge.data_class("User", ["username", "password"])
    
    original = User("bob", "password1")
    copy = dodge.copy(original)
    
    assert original is not copy
    assert_equal("bob", copy.username)
Beispiel #43
0
def copy_of_data_object_has_values_changed_to_copy_kwargs():
    User = dodge.data_class("User", ["username", "password"])
    
    original = User("bob", "password1")
    copy = dodge.copy(original, password="******")
    
    assert original is not copy
    assert_equal("password1", original.password)
    assert_equal("password2", copy.password)
Beispiel #44
0
def copy_of_data_object_has_values_changed_to_copy_kwargs():
    User = dodge.data_class("User", ["username", "password"])

    original = User("bob", "password1")
    copy = dodge.copy(original, password="******")

    assert original is not copy
    assert_equal("password1", original.password)
    assert_equal("password2", copy.password)
def can_convert_data_classes_to_and_from_flat_list():
    User = dodge.data_class("User", ["username", "password"])

    user = User("bob", "password1")
    serialised_user = dodge.obj_to_flat_list(user)
    unserialised_user = dodge.flat_list_to_obj(serialised_user, User)

    expected_user = User("bob", "password1")
    assert_equal(unserialised_user, expected_user)
def can_convert_data_classes_to_and_from_string():
    User = dodge.data_class("User", ["username", "password"])

    user = User("bob", "password1")
    serialised_user = dodge.dumps(user)
    unserialised_user = dodge.loads(serialised_user, User)

    expected_user = User("bob", "password1")
    assert_equal(unserialised_user, expected_user)
def can_convert_data_classes_to_and_from_string():
    User = dodge.data_class("User", ["username", "password"])

    user = User("bob", "password1")
    serialised_user = dodge.dumps(user)
    unserialised_user = dodge.loads(serialised_user, User)

    expected_user = User("bob", "password1")
    assert_equal(unserialised_user, expected_user)
def can_convert_data_classes_to_and_from_flat_list():
    User = dodge.data_class("User", ["username", "password"])

    user = User("bob", "password1")
    serialised_user = dodge.obj_to_flat_list(user)
    unserialised_user = dodge.flat_list_to_obj(serialised_user, User)

    expected_user = User("bob", "password1")
    assert_equal(unserialised_user, expected_user)
def converting_object_to_dict_preserves_ordering():
    User = dodge.data_class("User",
                            ["username", "salt", "password", "email_address"])

    user = User("bob", "!%ksdg", "password1", "*****@*****.**")
    result = dodge.obj_to_dict(user)

    assert_equal(["username", "salt", "password", "emailAddress"],
                 list(result.keys()))
Beispiel #50
0
def error_is_raised_if_kwarg_field_is_passed_as_positional_arg():
    User = dodge.data_class("User", [
        "username",
        dodge.field("password", keyword_only=True)
    ])
    
    assert_raises_regexp(
        TypeError, "takes 1 positional argument but 2 were given",
        lambda: User("bob", "password2")
    )
Beispiel #51
0
def super_init_is_called_if_super_class_is_specified():
    class HasId(object):
        def __init__(self):
            self.id = 42

    User = dodge.data_class("User", ["username"], bases=(HasId, ))

    user = User("bob")
    assert isinstance(user, HasId)
    assert_equal(42, user.id)
Beispiel #52
0
def super_init_is_called_if_super_class_is_specified():
    class HasId(object):
        def __init__(self):
            self.id = 42
    
    User = dodge.data_class("User", ["username"], bases=(HasId,))
    
    user = User("bob")
    assert isinstance(user, HasId)
    assert_equal(42, user.id)
Beispiel #53
0
def can_convert_data_classes_with_non_string_fields_to_representation():
    Prime = dodge.data_class("Prime", ["index", "value"])

    assert_equal("Prime(0, 2)", repr(Prime(0, 2)))
Beispiel #54
0
def fields_of_instance_can_be_retrieved():
    User = dodge.data_class("User", ["username"])

    user = User("bob")
    field, = dodge.fields(user)
    assert_equal("username", field.name)
Beispiel #55
0
def error_is_raised_if_copy_kwarg_is_not_field():
    User = dodge.data_class("User", ["username", "password"])

    original = User("bob", "password1")
    assert_raises_regexp(TypeError, "__init__ does not take keyword argument",
                         lambda: dodge.copy(original, salt="salty"))
Beispiel #56
0
def error_is_raised_if_fields_have_duplicate_names():
    assert_raises_regexp(
        ValueError, "^duplicate field name: 'username'$",
        lambda: dodge.data_class("User", ["username", "username"]))
Beispiel #57
0
import json
import contextlib

import dodge


statements = Statements = dodge.data_class("Statements", ["statements"])

if_ = IfElse = dodge.data_class("IfElse", ["condition", "true_body", "false_body"])
while_ = WhileLoop = dodge.data_class("WhileLoop", ["condition", "body"])
BreakStatement = dodge.data_class("BreakStatement", [])
ContinueStatement = dodge.data_class("ContinueStatement", [])

break_ = BreakStatement()
continue_ = ContinueStatement()

throw = Throw = dodge.data_class("Throw", ["value"])

expression_statement = ExpressionStatement = dodge.data_class("ExpressionStatement", ["value"])
ret = ReturnStatement = dodge.data_class("ReturnStatement", ["value"])

def assignment_expression(target, value):
    if isinstance(target, str):
        target = ref(target)
    
    return binary_operation("=", target, value)

property_access = PropertyAccess = dodge.data_class("PropertyAccess", ["value", "property"])
binary_operation = BinaryOperation = dodge.data_class("BinaryOperation", ["operator", "left", "right"])
unary_operation = UnaryOperation = dodge.data_class("UnaryOperation", ["operator", "operand"])
ternary_conditional = TernaryConditional = dodge.data_class("TernaryConditional", ["condition", "true_value", "false_value"])
Beispiel #58
0
import dodge


Document = dodge.data_class("Document", ["children", "footnotes"])
Paragraph = dodge.data_class("Paragraph", ["children", "style_id", "style_name", "numbering"])
Run = dodge.data_class("Run", [
    "children", "style_id", "style_name", "is_bold", "is_italic",
    "is_underline", "vertical_alignment",
])
Text = dodge.data_class("Text", ["value"])
Hyperlink = dodge.data_class("Hyperlink", ["href", "children"])
Table = dodge.data_class("Table", ["children"])
TableRow = dodge.data_class("TableRow", ["children"])
TableCell = dodge.data_class("TableCell", ["children"])
LineBreak = dodge.data_class("LineBreak", [])

class Tab(object):
    pass


class Image(object):
    def __init__(self, alt_text, content_type, open):
        self.alt_text = alt_text
        self.content_type = content_type
        self.open = open


def document(children, footnotes=None):
    if footnotes is None:
        footnotes = Footnotes({})
    return Document(children, footnotes)
Beispiel #59
0
def class_module_is_not_dodge():
    User = dodge.data_class("User", [])
    assert_equal("dodge_tests", User.__module__)