Coverage for Users/vladimirpavlov/PycharmProjects/parameterizable/tests/test_json_processor_cover_slots_branch.py: 100%

1 from mixinforge.json_processor import _to_serializable_dict, _recreate_object, _Markers

2

3

4 class SlotsMaskGetstate:

5 __slots__ = ("x",)

6

7 def __init__(self, x):

8 self.x = x

9

10 # Make hasattr(inst, "__getstate__") return False even if CPython provides a default

11 def __getattribute__(self, name):

12 if name == "__getstate__":

13 raise AttributeError

14 return object.__getattribute__(self, name)

15

16

17 class HybridSlotsMaskGetstate:

18 __slots__ = ("__dict__", "y")

19

20 def __init__(self, y):

21 self.y = y

22 self.extra = "e"

23

24 def __getattribute__(self, name):

25 if name == "__getstate__":

26 raise AttributeError

27 return object.__getattribute__(self, name)

28

29

30 def test_slots_only_branch_is_used_when_getstate_hidden():

31 obj = SlotsMaskGetstate(123)

32 ser = _to_serializable_dict(obj)

33

34 # Ensure slots path produced a STATE payload

35 assert _Markers.STATE in ser

36

37 back = _recreate_object(ser)

38 assert isinstance(back, SlotsMaskGetstate)

39 assert back.x == 123

40

41

42 def test_hybrid_slots_branch_is_used_when_getstate_hidden():

43 obj = HybridSlotsMaskGetstate(5)

44 obj.more = 42

45

46 ser = _to_serializable_dict(obj)

47 assert _Markers.STATE in ser

48

49 back = _recreate_object(ser)

50 assert isinstance(back, HybridSlotsMaskGetstate)

51 assert back.y == 5

52 assert back.extra == "e"

53 assert back.more == 42