|
26 | 26 |
|
27 | 27 | import pytest |
28 | 28 |
|
| 29 | +from pytools import Record |
| 30 | + |
29 | 31 |
|
30 | 32 | logger = logging.getLogger(__name__) |
31 | 33 | from typing import FrozenSet |
@@ -783,6 +785,78 @@ def test_unique(): |
783 | 785 | assert next(unique([]), None) is None |
784 | 786 |
|
785 | 787 |
|
| 788 | +# This class must be defined globally to be picklable |
| 789 | +class SimpleRecord(Record): |
| 790 | + pass |
| 791 | + |
| 792 | + |
| 793 | +def test_record(): |
| 794 | + r = SimpleRecord(c=3, b=2, a=1) |
| 795 | + |
| 796 | + assert r.a == 1 |
| 797 | + assert r.b == 2 |
| 798 | + assert r.c == 3 |
| 799 | + |
| 800 | + # Fields are sorted alphabetically in records |
| 801 | + assert str(r) == "SimpleRecord(a=1, b=2, c=3)" |
| 802 | + |
| 803 | + # Unregistered fields are (silently) ignored for printing |
| 804 | + r.f = 6 |
| 805 | + assert str(r) == "SimpleRecord(a=1, b=2, c=3)" |
| 806 | + |
| 807 | + # Registered fields are printed |
| 808 | + r.register_fields({"d", "e"}) |
| 809 | + assert str(r) == "SimpleRecord(a=1, b=2, c=3)" |
| 810 | + |
| 811 | + r.d = 4 |
| 812 | + r.e = 5 |
| 813 | + assert str(r) == "SimpleRecord(a=1, b=2, c=3, d=4, e=5)" |
| 814 | + |
| 815 | + with pytest.raises(AttributeError): |
| 816 | + r.ff |
| 817 | + |
| 818 | + # Test pickling |
| 819 | + import pickle |
| 820 | + r_pickled = pickle.loads(pickle.dumps(r)) |
| 821 | + assert r == r_pickled |
| 822 | + |
| 823 | + # }}} |
| 824 | + |
| 825 | + # {{{ __slots__, __dict__, __weakref__ handling |
| 826 | + |
| 827 | + class RecordWithEmptySlots(Record): |
| 828 | + __slots__ = [] |
| 829 | + |
| 830 | + assert hasattr(RecordWithEmptySlots(), "__slots__") |
| 831 | + assert not hasattr(RecordWithEmptySlots(), "__dict__") |
| 832 | + assert not hasattr(RecordWithEmptySlots(), "__weakref__") |
| 833 | + |
| 834 | + class RecordWithUnsetSlots(Record): |
| 835 | + pass |
| 836 | + |
| 837 | + assert hasattr(RecordWithUnsetSlots(), "__slots__") |
| 838 | + assert hasattr(RecordWithUnsetSlots(), "__dict__") |
| 839 | + assert hasattr(RecordWithUnsetSlots(), "__weakref__") |
| 840 | + |
| 841 | + from pytools import ImmutableRecord |
| 842 | + |
| 843 | + class ImmutableRecordWithEmptySlots(ImmutableRecord): |
| 844 | + __slots__ = [] |
| 845 | + |
| 846 | + assert hasattr(ImmutableRecordWithEmptySlots(), "__slots__") |
| 847 | + assert hasattr(ImmutableRecordWithEmptySlots(), "__dict__") |
| 848 | + assert hasattr(ImmutableRecordWithEmptySlots(), "__weakref__") |
| 849 | + |
| 850 | + class ImmutableRecordWithUnsetSlots(ImmutableRecord): |
| 851 | + pass |
| 852 | + |
| 853 | + assert hasattr(ImmutableRecordWithUnsetSlots(), "__slots__") |
| 854 | + assert hasattr(ImmutableRecordWithUnsetSlots(), "__dict__") |
| 855 | + assert hasattr(ImmutableRecordWithUnsetSlots(), "__weakref__") |
| 856 | + |
| 857 | + # }}} |
| 858 | + |
| 859 | + |
786 | 860 | if __name__ == "__main__": |
787 | 861 | if len(sys.argv) > 1: |
788 | 862 | exec(sys.argv[1]) |
|
0 commit comments