point = make_point()
assert repr(point) == 'Point(x=1, y=2)', f'point repr {point=!r}'
assert str(point) == 'Point(x=1, y=2)'
assert bool(point)
h1 = hash(point)
assert h1 != 0
h2 = hash(point)
assert h1 == h2
point2 = make_point()
assert hash(point) == hash(point2)
d = {point: 'first'}
assert d[point] == 'first'
assert d[point2] == 'first'
s = {point, point2}
assert len(s) == 1
alice = make_user('Alice')
bob = make_user('Bob')
assert hash(alice) != hash(bob)
assert point == point2
assert alice != bob
assert point != alice
assert point != (1, 2)
assert point != {'x': 1, 'y': 2}
mut_point = make_mutable_point()
assert repr(mut_point) == 'MutablePoint(x=1, y=2)', f'mutable point repr {mut_point=!r}'
assert point != mut_point
assert mut_point != point
alice = make_user('Alice')
assert repr(alice) == "User(name='Alice', active=True)", f'user repr with string field {alice=!r}'
points = [point, mut_point, alice]
assert len(points) == 3
assert point.x == 1
assert point.y == 2
assert mut_point.x == 1
assert mut_point.y == 2
assert alice.name == 'Alice'
assert alice.active == True
mut_point.x = 10
assert mut_point.x == 10
mut_point.y = 20
assert mut_point.y == 20
assert repr(mut_point) == 'MutablePoint(x=10, y=20)', f'repr after attribute update {mut_point=!r}'
mut_point.z = 30
assert mut_point.z == 30
assert repr(mut_point) == 'MutablePoint(x=10, y=20)'
aug_point = make_mutable_point()
aug_point.x += 5
assert aug_point.x == 6
aug_point.y -= 1
assert aug_point.y == 1
aug_point.x *= 3
assert aug_point.x == 18
outer_aug = make_mutable_point()
inner_aug = make_mutable_point()
outer_aug.x = inner_aug
outer_aug.x.y += 100
assert inner_aug.y == 102
ca = make_mutable_point()
cb = make_mutable_point()
ca.x = cb.x = 77
assert ca.x == 77
assert cb.x == 77
holder = [0]
cm = make_mutable_point()
val = cm.y = holder[0] = 321
assert val == 321
assert cm.y == 321
assert holder[0] == 321
attr_last_name_box = [0]
attr_last_obj = make_mutable_point()
attr_last_name = attr_last_name_box[0] = attr_last_obj.y = 555
assert attr_last_name == 555
assert attr_last_name_box[0] == 555
assert attr_last_obj.y == 555
attr_first_obj = make_mutable_point()
attr_first_obj.x = attr_first_name = 556
assert attr_first_obj.x == 556
assert attr_first_name == 556
pa = make_mutable_point()
pb = make_mutable_point()
pc = make_mutable_point()
pa.x = pb.x = pc.x = 999
assert pa.x == 999
assert pb.x == 999
assert pc.x == 999
nest_outer = make_mutable_point()
nest_inner = make_mutable_point()
nest_outer.x = nest_inner
nested_chain_name = nest_outer.x.y = 444
assert nested_chain_name == 444
assert nest_inner.y == 444
assert nest_outer.x.y == 444
unpack_dst = make_mutable_point()
(ua1, ua2) = unpack_dst.x = unpack_chain_name = (13, 14)
assert ua1 == 13 and ua2 == 14, 'attr-unpack chain: unpack'
assert unpack_dst.x == (13, 14)
assert unpack_chain_name == (13, 14)
outer = make_mutable_point()
inner = make_mutable_point()
inner.x = 100
inner.y = 200
outer.x = inner
assert outer.x.x == 100
assert outer.x.y == 200
outer.x.x = 999
assert outer.x.x == 999
outer.x.y = 888
assert outer.x.y == 888
assert inner.x == 999
assert inner.y == 888
level1 = make_mutable_point()
level2 = make_mutable_point()
level3 = make_mutable_point()
level3.x = 42
level2.x = level3
level1.x = level2
assert level1.x.x.x == 42
level1.x.x.x = 7
assert level1.x.x.x == 7
assert level3.x == 7
empty = make_empty()
assert repr(empty) == 'Empty()'
assert str(empty) == 'Empty()'
frozen_point = make_point()
caught = False
try:
frozen_point.x = 10
except AttributeError:
caught = True
assert caught
try:
point.nonexistent
assert False, 'should have raised AttributeError for missing attr'
except AttributeError as e:
assert str(e) == "'Point' object has no attribute 'nonexistent'", f'wrong message: {e}'
try:
point._private
assert False, 'should have raised AttributeError for private attr'
except AttributeError as e:
assert str(e) == "'Point' object has no attribute '_private'", f'wrong message: {e}'
try:
point.__nonexistent__()
assert False, 'should have raised AttributeError for dunder'
except AttributeError as e:
assert str(e) == "'Point' object has no attribute '__nonexistent__'", f'wrong message: {e}'
try:
point._private_method()
assert False, 'should have raised AttributeError for private method'
except AttributeError as e:
assert str(e) == "'Point' object has no attribute '_private_method'", f'wrong message: {e}'
try:
point.x()
assert False, 'should have raised TypeError for calling int field'
except TypeError as e:
assert str(e) == "'int' object is not callable", f'wrong message: {e}'
try:
point.nonexistent_method()
assert False, 'should have raised AttributeError for missing method'
except AttributeError as e:
assert str(e) == "'Point' object has no attribute 'nonexistent_method'", f'wrong message: {e}'
try:
mut_point.nonexistent
assert False, 'should have raised AttributeError on mutable dc'
except AttributeError as e:
assert str(e) == "'MutablePoint' object has no attribute 'nonexistent'", f'wrong message: {e}'
try:
mut_point.x()
assert False, 'should have raised TypeError on mutable dc field call'
except TypeError as e:
assert str(e) == "'int' object is not callable", f'wrong message: {e}'
result = point.sum()
assert result == 3, f'Point.sum() should be 3, got {result}'
new_point = point.add(10, 20)
assert new_point.x == 11, f'Point.add x should be 11, got {new_point.x}'
assert new_point.y == 22, f'Point.add y should be 22, got {new_point.y}'
scaled = point.scale(3)
assert scaled.x == 3, f'Point.scale x should be 3, got {scaled.x}'
assert scaled.y == 6, f'Point.scale y should be 6, got {scaled.y}'
desc = point.describe('pt')
assert desc == 'pt(1, 2)', f'Point.describe should be pt(1, 2), got {desc}'
mut_p2 = make_mutable_point()
mut_sum = mut_p2.sum()
assert mut_sum == 3, f'MutablePoint.sum() should be 3, got {mut_sum}'
alice2 = make_user('Alice')
greeting = alice2.greeting()
assert greeting == 'Hello, Alice!', f'User.greeting should be Hello, Alice!, got {greeting}'
p3 = point.add(0, 0)
assert p3.x == 1, f'chained method access: p3.x should be 1, got {p3.x}'
assert p3.y == 2, f'chained method access: p3.y should be 2, got {p3.y}'
desc_kw = point.describe(label='custom')
assert desc_kw == 'custom(1, 2)', f'Point.describe(label=) should be custom(1, 2), got {desc_kw}'
try:
mut_p2.nonexistent_method()
assert False, 'should have raised AttributeError for missing method on mutable dc'
except AttributeError as e:
assert str(e) == "'MutablePoint' object has no attribute 'nonexistent_method'", f'wrong message: {e}'
try:
alice2.missing()
assert False, 'should have raised AttributeError for missing method on User'
except AttributeError as e:
assert str(e) == "'User' object has no attribute 'missing'", f'wrong message: {e}'