|
| 1 | +# Authors: Sylvain Marie <sylvain.marie@se.com> |
| 2 | +# |
| 3 | +# Copyright (c) Schneider Electric Industries, 2019. All right reserved. |
| 4 | +import sys |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +from autoclass import autorepr |
| 9 | + |
| 10 | + |
| 11 | +@pytest.mark.skipif(sys.version_info < (3, 6), reason="class vars order is not preserved") |
| 12 | +@pytest.mark.parametrize('only_public_fields', [True, False], ids=lambda x: 'only_public' if x else 'including class-private dunder fields') |
| 13 | +@pytest.mark.parametrize('only_known_fields', [True, False], ids=lambda x: 'only_constructor_args' if x else 'all_obj_fields') |
| 14 | +@pytest.mark.parametrize("curly_mode", [False, True], ids="curly_mode={}".format) |
| 15 | +def test_autorepr(only_known_fields, only_public_fields, curly_mode): |
| 16 | + """ @autorepr functionality with various customization options for only_constructor_args/only_public_fields """ |
| 17 | + |
| 18 | + if curly_mode: |
| 19 | + def format_pairs(cls, pairs): |
| 20 | + return "%s(**{%s})" % (cls.__name__, ", ".join(["%r: %r" % pair for pair in pairs])) |
| 21 | + else: |
| 22 | + def format_pairs(cls, pairs): |
| 23 | + return "%s(%s)" % (cls.__name__, ", ".join(["%s=%r" % pair for pair in pairs])) |
| 24 | + |
| 25 | + @autorepr(only_known_fields=only_known_fields, only_public_fields=only_public_fields, curly_string_repr=curly_mode) |
| 26 | + class FooConfigA(object): |
| 27 | + |
| 28 | + dummy_class_field = 'just to be sure it does not appear' |
| 29 | + |
| 30 | + def __init__(self, |
| 31 | + a, # type: str, |
| 32 | + b # type: List[str] |
| 33 | + ): |
| 34 | + self.a = a |
| 35 | + self.b = b |
| 36 | + self.c = 't' |
| 37 | + self._weak_private = 'r' |
| 38 | + self.__class_private = 't' |
| 39 | + |
| 40 | + def dummy_func(self): |
| 41 | + """ we create this just to be sure the function is not in the dict view """ |
| 42 | + pass |
| 43 | + |
| 44 | + t = FooConfigA('rhubarb', ['pie', 'pie2']) |
| 45 | + t.new_field = 0 |
| 46 | + t._new_field_weak_private = 1 |
| 47 | + t.__new_field_class_private_incorrect = 0 |
| 48 | + |
| 49 | + class Dummy: |
| 50 | + t.__new_field_class_private = 1 |
| 51 | + |
| 52 | + # check the str/repr |
| 53 | + assert str(t) == repr(t) |
| 54 | + |
| 55 | + if only_known_fields: |
| 56 | + pairs = [('a', 'rhubarb'), # only the two constructor fields appear |
| 57 | + ('b', ['pie', 'pie2'])] |
| 58 | + elif only_public_fields: |
| 59 | + pairs = [('a', 'rhubarb'), |
| 60 | + ('b', ['pie', 'pie2']), |
| 61 | + ('c', 't'), |
| 62 | + # _FooConfigA__class_private should not appear |
| 63 | + ('new_field', 0) |
| 64 | + #'_weak_private': 'r', |
| 65 | + #'_new_field_weak_private': 1, |
| 66 | + # private fields defined out of the objects class are still visible |
| 67 | + #'__new_field_class_private_incorrect': 0, |
| 68 | + #'_Dummy__new_field_class_private': 1 |
| 69 | + ] |
| 70 | + else: |
| 71 | + pairs = [('a', 'rhubarb'), |
| 72 | + ('b', ['pie', 'pie2']), |
| 73 | + ('c', 't'), |
| 74 | + ('_weak_private', 'r'), |
| 75 | + ('_FooConfigA__class_private', 't'), # <= this is the one private field that appears now |
| 76 | + ('new_field', 0), |
| 77 | + ('_new_field_weak_private', 1), |
| 78 | + # private fields defined out of the objects class are still visible |
| 79 | + ('__new_field_class_private_incorrect', 0), |
| 80 | + ('_Dummy__new_field_class_private', 1)] |
| 81 | + |
| 82 | + assert str(t) == format_pairs(FooConfigA, pairs) |
| 83 | + |
| 84 | + |
| 85 | +@pytest.mark.parametrize("curly_mode", [False, True], ids="curly_mode={}".format) |
| 86 | +def test_autorepr_pyfields(curly_mode): |
| 87 | + """tests that @autorepr works with pyfields""" |
| 88 | + |
| 89 | + from pyfields import field |
| 90 | + |
| 91 | + @autorepr |
| 92 | + class Foo(object): |
| 93 | + foo1 = field() |
| 94 | + foo2 = field(default=0) |
| 95 | + |
| 96 | + @autorepr(curly_string_repr=curly_mode) |
| 97 | + class Bar(Foo): |
| 98 | + bar = field() |
| 99 | + |
| 100 | + # create an object manually |
| 101 | + a = Bar() |
| 102 | + a.bar = 2 |
| 103 | + a.foo1 = 'th' |
| 104 | + |
| 105 | + # order in prints is correct in legacy str mode |
| 106 | + if curly_mode: |
| 107 | + assert str(a) == "Bar(**{'foo1': 'th', 'foo2': 0, 'bar': 2})" |
| 108 | + else: |
| 109 | + assert str(a) == "Bar(foo1='th', foo2=0, bar=2)" |
0 commit comments