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