def test_unknown(self, C): """ Wanting to change an unknown attribute raises a ValueError. """ # No generated class will have a four letter attribute. with pytest.raises(AttrsAttributeNotFoundError) as e: assoc(C(), aaaa=2) assert ( "aaaa is not an attrs attribute on {cls!r}.".format(cls=C), ) == e.value.args
def test_unknown(self, C): """ Wanting to change an unknown attribute raises a ValueError. """ @attributes class C(object): x = attr() y = 42 with pytest.raises(ValueError) as e: assoc(C(1), y=2) assert ("y is not an attrs attribute on {cl!r}.".format(cl=C),) == e.value.args
def test_unknown(self): """ Wanting to change an unknown attribute raises a ValueError. """ @attributes class C(object): x = attr() y = 42 with pytest.raises(ValueError) as e: assoc(C(1), y=2) assert ("y is not an attrs attribute on {cl!r}.".format( cl=C), ) == e.value.args
def test_no_changes(self, C): """ No changes means a verbatim copy. """ i1 = C(1, 2) i2 = assoc(i1) assert i1 is not i2 assert i1 == i2
def test_no_changes(self, C): """ No changes means a verbatim copy. """ i1 = C() i2 = assoc(i1) assert i1 is not i2 assert i1 == i2
def test_frozen(self): """ Works on frozen classes. """ @attributes(frozen=True) class C(object): x = attr() y = attr() assert C(3, 2) == assoc(C(1, 2), x=3)
def test_change(self, C, val): """ Changes work. """ # Take the first attribute, and change it. assume(fields(C)) # Skip classes with no attributes. original = C() attribute = fields(C)[0] changed = assoc(original, **{attribute.name: val}) assert getattr(changed, attribute.name) == val
def test_empty(self): """ Empty classes without changes get copied. """ @attributes class C(object): pass i1 = C() i2 = assoc(i1) assert i1 is not i2 assert i1 == i2
def test_empty(self, slots, frozen): """ Empty classes without changes get copied. """ @attributes(slots=slots, frozen=frozen) class C(object): pass i1 = C() i2 = assoc(i1) assert i1 is not i2 assert i1 == i2
def test_change(self, C, data): """ Changes work. """ # Take the first attribute, and change it. assume(fields(C)) # Skip classes with no attributes. field_names = [a.name for a in fields(C)] original = C() chosen_names = data.draw(st.sets(st.sampled_from(field_names))) change_dict = {name: data.draw(st.integers()) for name in chosen_names} changed = assoc(original, **change_dict) for k, v in change_dict.items(): assert getattr(changed, k) == v
def test_change(self, C): """ Changes work. """ i = assoc(C(1, 2), x=42) assert C(42, 2) == i