Skip to content

Commit 5596b9b

Browse files
author
Sylvain MARIE
committed
Improved test to cover all ordering issues fixed recently
1 parent 45990b2 commit 5596b9b

1 file changed

Lines changed: 29 additions & 4 deletions

File tree

‎pyfields/tests/test_init.py‎

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55

66
import pytest
77

8-
from pyfields import field, init_fields, inject_fields, make_init, MandatoryFieldInitError, copy_field
8+
from pyfields import field, init_fields, inject_fields, make_init, MandatoryFieldInitError, copy_field, get_fields
9+
from pyfields.core import PY36
910

1011

1112
@pytest.mark.parametrize("native", [False, True], ids="native={}".format)
@@ -147,10 +148,12 @@ def __init__(self):
147148
print(vars(c))
148149

149150

150-
def test_init_order2():
151+
@pytest.mark.parametrize("a_first", [None, False, True], ids="ancestor_first={}".format)
152+
def test_init_order2(a_first):
151153
""""""
152154
class A(object):
153155
a = field()
156+
d = field(default=5)
154157

155158
class B(object):
156159
b = field()
@@ -159,13 +162,35 @@ class C(B, A):
159162
a = field(default=None)
160163
c = field(default_factory=copy_field('b'))
161164

162-
@init_fields
165+
@init_fields(ancestor_fields_first=a_first)
163166
def __init__(self):
164167
pass
165168

169+
fields = get_fields(C, include_inherited=True, ancestors_first=a_first if a_first is not None else True,
170+
_auto_fix_fields=not PY36)
171+
field_names = [f.name for f in fields]
172+
if a_first is None or a_first:
173+
assert field_names == ['a', 'd', 'b', 'c']
174+
else:
175+
assert field_names == ['a', 'c', 'b', 'd']
176+
166177
# make sure that a and c have default values and therefore just passing b is ok.
167178
c = C(1)
168-
assert vars(c) == {'a': None, 'b': 1, 'c': 1}
179+
assert vars(c) == {'b': 1, 'c': 1, 'a': None, 'd': 5}
180+
181+
c = C(1, 2, 3)
182+
if a_first is None or a_first:
183+
assert vars(c) == {'b': 1, # 1st arg
184+
'c': 1, # default: copy of b
185+
'a': 2, # 2d arg
186+
'd': 3 # 3d arg
187+
}
188+
else:
189+
assert vars(c) == {'b': 1, # 1st arg
190+
'c': 3, # 3d arg
191+
'a': 2, # 2d arg
192+
'd': 5 # default: 5
193+
}
169194

170195

171196
def test_init_inheritance():

0 commit comments

Comments
 (0)