Ejemplo n.º 1
0
def test_method_struct_defined_method():
    Dog = Struct(
        "Dog", "name", "age", methods={"say": lambda self: self.__dict__["name"]}
    )
    dog = Dog()
    dog.name = "rex"
    dog.age = 12
    assert dog.say() == "rex"
Ejemplo n.º 2
0
def test_eq_two_structs_are_not_equivalent():
    Dog = Struct("Dog", "name", "age")
    d1 = Dog()
    d1.name = "caesar"
    d1.age = 20
    d2 = Dog()
    d2.name = "rex"
    d2.age = 12
    assert d1 != d2
Ejemplo n.º 3
0
def test_hash_two_structs_produce_the_same_key():
    Dog = Struct("Dog", "name", "age")
    d1 = Dog()
    d1.name = "rex"
    d1.age = 12
    d2 = Dog()
    d2.name = "rex"
    d2.age = 12
    hsh = {}
    hsh[d1] = 1
    hsh[d2] = 2
    assert len(hsh) == 1
Ejemplo n.º 4
0
def test_hash_two_structs_produce_different_keys():
    Dog = Struct("Dog", "name", "age")
    d1 = Dog()
    d1.name = "rex"
    d1.age = 12
    d2 = Dog()
    d2.name = "caesar"
    d2.age = 20
    hsh = {}
    hsh[d1] = 1
    hsh[d2] = 2
    assert len(hsh) == 2
Ejemplo n.º 5
0
def main():
    Dog = Struct(
        "Dog",
        "name",
        "age",
        methods={
            "say": lambda self: print("Hello {}".format(self.__dict__["name"]))
        },
    )

    d = Dog()
    d.name = "rex"
    d.age = 54
    d.say()
    print(d.members())

    d2 = Dog()
    d2.name = "Wolfie"
    d2.age = 22
    print("Age: {}".format(d2.age))
    d2.say()

    d3 = Dog()
    d3.name = "Wolfie"
    d3.age = 22
    d3.say()
    print("d2 == d3", d2 == d3)

    h = {}
    h[d] = 1
    h[d2] = 2
    h[d3] = 3
    print(len(h))

    o = OpenStruct()
    o.dog_name = "Rex"
    print(o.dog_name)
    print(o)

    b = OpenStruct({"age": 12})
    print(b.age)
Ejemplo n.º 6
0
def test_members_struct_returns_field_names():
    Dog = Struct("Dog", "name", "age")
    dog = Dog()
    assert dog.members() == ("name", "age")
Ejemplo n.º 7
0
def test_init_struct_raises_error_if_less_values():
    Dog = Struct("Dog", "name", "age")
    with pytest.raises(ValueError):
        dog = Dog("Rex")
Ejemplo n.º 8
0
def test_init_struct_use_arguments():
    Dog = Struct("Dog", "name", "age")
    dog = Dog("Rex", 12)
    assert dog.name == "Rex" and dog.age == 12
Ejemplo n.º 9
0
def test_str_struct_str():
    Dog = Struct("Dog", "name", "age")
    dog = Dog()
    dog.name = "rex"
    dog.age = 12
    assert "[name=rex, age=12]" == str(dog)
Ejemplo n.º 10
0
def test_field_struct_declared_field_is_none():
    Dog = Struct("Dog", "age")
    dog = Dog()
    assert dog.age == None
Ejemplo n.º 11
0
def test_to_dict_struct_returns_dict_of_fields_and_values():
    Dog = Struct("Dog", "name", "age")
    dog = Dog("Rex", 12)
    assert dog.to_dict() == {"name": "Rex", "age": 12}
Ejemplo n.º 12
0
def test_len_struct_returns_size_of_fields():
    Dog = Struct("Dog", "name", "age")
    dog = Dog("Rex", 12)
    assert len(dog) == 2
Ejemplo n.º 13
0
def test_values_struct_returns_values_tuple():
    Dog = Struct("Dog", "name", "age")
    dog = Dog("Rex", 12)
    assert dog.values() == ("Rex", 12)
Ejemplo n.º 14
0
def test_field_struct_declared_fields_and_set_values():
    Dog = Struct("Dog", "name", "age")
    dog = Dog()
    dog.name = "rex"
    dog.age = 12
    assert dog.name == "rex" and dog.age == 12