1
|
|
|
import sys |
2
|
|
|
|
3
|
|
|
import pytest |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
def test_import(namespaces): |
7
|
|
|
assert namespaces |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
def test_meta_basic(namespaces): |
11
|
|
|
class Test(metaclass=namespaces.Namespaceable): |
12
|
|
|
pass |
13
|
|
|
assert Test |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
def test_basic_namespace(namespaces): |
17
|
|
|
class Test(metaclass=namespaces.Namespaceable): |
18
|
|
|
with namespaces.Namespace() as ns: |
19
|
|
|
a = 1 |
20
|
|
|
assert ns |
21
|
|
|
assert Test |
22
|
|
|
assert Test().ns |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
def test_delete(namespaces): |
26
|
|
|
class Test(metaclass=namespaces.Namespaceable): |
27
|
|
|
with namespaces.Namespace() as ns: |
28
|
|
|
a = 1 |
29
|
|
|
del a |
30
|
|
|
assert ns |
31
|
|
|
assert Test |
32
|
|
|
assert Test().ns |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
@pytest.mark.xfail(sys.version_info < (3, 4), |
36
|
|
|
reason="python3.4 api changes?", strict=True) |
37
|
|
|
def test_dir(namespaces): |
38
|
|
|
class Test(metaclass=namespaces.Namespaceable): |
39
|
|
|
with namespaces.Namespace() as ns: |
40
|
|
|
a = 1 |
41
|
|
|
assert ns |
42
|
|
|
assert dir(Test.ns) == ['a'] |
43
|
|
|
assert dir(Test().ns) == ['a'] |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
def test_shadow(namespaces): |
47
|
|
|
class Test(metaclass=namespaces.Namespaceable): |
48
|
|
|
foo = 1 |
49
|
|
|
with namespaces.Namespace() as ns: |
50
|
|
|
foo = 2 |
51
|
|
|
assert foo == 2 |
52
|
|
|
assert foo == 1 |
53
|
|
|
assert Test().foo == 1 |
54
|
|
|
assert Test().ns.foo == 2 |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
def test_resume(namespaces): |
58
|
|
|
class Test(metaclass=namespaces.Namespaceable): |
59
|
|
|
foo = 1 |
60
|
|
|
with namespaces.Namespace() as ns: |
61
|
|
|
foo = 2 |
62
|
|
|
assert foo == 2 |
63
|
|
|
foo = 3 |
64
|
|
|
with ns: |
65
|
|
|
foo = 4 |
66
|
|
|
assert foo == 3 |
67
|
|
|
assert Test().foo == 3 |
68
|
|
|
assert Test().ns.foo == 4 |
69
|
|
|
|
70
|
|
|
|
71
|
|
|
def test_redundant_resume(namespaces): |
72
|
|
|
class Test(metaclass=namespaces.Namespaceable): |
73
|
|
|
foo = 1 |
74
|
|
|
with namespaces.Namespace() as ns: |
75
|
|
|
foo = 2 |
76
|
|
|
assert foo == 2 |
77
|
|
|
foo = 3 |
78
|
|
|
ns = ns |
79
|
|
|
with ns as ns: |
80
|
|
|
foo = 4 |
81
|
|
|
assert foo == 3 |
82
|
|
|
assert Test().foo == 3 |
83
|
|
|
assert Test().ns.foo == 4 |
84
|
|
|
|
85
|
|
|
|
86
|
|
|
def test_basic_inherit(namespaces): |
87
|
|
|
class Test(metaclass=namespaces.Namespaceable): |
88
|
|
|
foo = 1 |
89
|
|
|
with namespaces.Namespace() as ns: |
90
|
|
|
foo = 2 |
91
|
|
|
|
92
|
|
|
class Subclass(Test): |
93
|
|
|
pass |
94
|
|
|
assert Subclass().foo == 1 |
95
|
|
|
assert Subclass().ns.foo == 2 |
96
|
|
|
|
97
|
|
|
|
98
|
|
|
def test_basic_super(namespaces): |
99
|
|
|
class Test(metaclass=namespaces.Namespaceable): |
100
|
|
|
with namespaces.Namespace() as ns: |
101
|
|
|
def hello(self): |
102
|
|
|
return 1 |
103
|
|
|
|
104
|
|
|
class Subclass(Test): |
105
|
|
|
with namespaces.Namespace() as ns: |
106
|
|
|
def hello(self): |
107
|
|
|
return super().ns.hello() |
108
|
|
|
|
109
|
|
|
assert Test().ns.hello() == 1 |
110
|
|
|
assert Subclass().ns.hello() == 1 |
111
|
|
|
|
112
|
|
|
|
113
|
|
|
def test_private(namespaces): |
114
|
|
|
class Test(metaclass=namespaces.Namespaceable): |
115
|
|
|
with namespaces.Namespace() as __ns: |
116
|
|
|
foo = 2 |
117
|
|
|
|
118
|
|
|
def foo(self): |
119
|
|
|
return self.__ns.foo |
120
|
|
|
|
121
|
|
|
class Subclass(Test): |
122
|
|
|
pass |
123
|
|
|
|
124
|
|
|
assert Test().foo() == 2 |
125
|
|
|
assert Subclass().foo() == 2 |
126
|
|
|
|
127
|
|
|
|
128
|
|
|
def test_nested_namespace(namespaces): |
129
|
|
|
class Test(metaclass=namespaces.Namespaceable): |
130
|
|
|
with namespaces.Namespace() as ns: |
131
|
|
|
with namespaces.Namespace() as ns: |
132
|
|
|
a = 1 |
133
|
|
|
assert Test().ns.ns.a == 1 |
134
|
|
|
|
135
|
|
|
|
136
|
|
|
def test_basic_shadow(namespaces): |
137
|
|
|
class Test(metaclass=namespaces.Namespaceable): |
138
|
|
|
with namespaces.Namespace() as ns: |
139
|
|
|
foo = 2 |
140
|
|
|
|
141
|
|
|
class Subclass(Test): |
142
|
|
|
ns = 1 |
143
|
|
|
assert Subclass().ns == 1 |
144
|
|
|
|
145
|
|
|
|
146
|
|
|
def test_double_shadow(namespaces): |
147
|
|
|
class Test(metaclass=namespaces.Namespaceable): |
148
|
|
|
with namespaces.Namespace() as ns: |
149
|
|
|
foo = 2 |
150
|
|
|
|
151
|
|
|
class Subclass(Test): |
152
|
|
|
ns = 1 |
153
|
|
|
|
154
|
|
|
class DoubleSubclass(Subclass): |
155
|
|
|
with namespaces.Namespace() as ns: |
156
|
|
|
bar = 1 |
157
|
|
|
assert not hasattr(DoubleSubclass().ns, 'foo') |
158
|
|
|
|
159
|
|
|
|
160
|
|
|
def test_overlap(namespaces): |
161
|
|
|
class Test(metaclass=namespaces.Namespaceable): |
162
|
|
|
with namespaces.Namespace() as ns: |
163
|
|
|
foo = 2 |
164
|
|
|
|
165
|
|
|
class Subclass(Test): |
166
|
|
|
with namespaces.Namespace() as ns: |
167
|
|
|
bar = 3 |
168
|
|
|
assert Subclass().ns.foo == 2 |
169
|
|
|
assert Subclass().ns.bar == 3 |
170
|
|
|
|
171
|
|
|
|
172
|
|
|
def test_advanced_overlap(namespaces): |
173
|
|
|
class Test(metaclass=namespaces.Namespaceable): |
174
|
|
|
with namespaces.Namespace() as ns: |
175
|
|
|
foo = 2 |
176
|
|
|
with namespaces.Namespace() as ns: |
177
|
|
|
qux = 4 |
178
|
|
|
|
179
|
|
|
class Subclass(Test): |
180
|
|
|
with namespaces.Namespace() as ns: |
181
|
|
|
bar = 3 |
182
|
|
|
assert Subclass().ns.foo == 2 |
183
|
|
|
assert Subclass().ns.bar == 3 |
184
|
|
|
assert Subclass().ns.ns.qux == 4 |
185
|
|
|
|
186
|
|
|
|
187
|
|
|
def test_empty_nameless(namespaces): |
188
|
|
|
class Test(metaclass=namespaces.Namespaceable): |
189
|
|
|
with pytest.raises(RuntimeError): |
190
|
|
|
with namespaces.Namespace(): |
191
|
|
|
pass |
192
|
|
|
|
193
|
|
|
|
194
|
|
|
def test_non_empty_nameless(namespaces): |
195
|
|
|
class Test(metaclass=namespaces.Namespaceable): |
196
|
|
|
with pytest.raises(RuntimeError): |
197
|
|
|
with namespaces.Namespace(): |
198
|
|
|
a = 1 |
199
|
|
|
|
200
|
|
|
|
201
|
|
|
def test_rename(namespaces): |
202
|
|
|
class Test(metaclass=namespaces.Namespaceable): |
203
|
|
|
with namespaces.Namespace() as ns: |
204
|
|
|
pass |
205
|
|
|
with pytest.raises(ValueError): |
206
|
|
|
with ns as ns2: |
207
|
|
|
pass |
208
|
|
|
|
209
|
|
|
|
210
|
|
|
def test_use_namespace(namespaces): |
211
|
|
|
class Test(metaclass=namespaces.Namespaceable): |
212
|
|
|
with namespaces.Namespace() as ns: |
213
|
|
|
foo = 1 |
214
|
|
|
assert ns.foo == 1 |
215
|
|
|
ns.bar = 2 |
216
|
|
|
assert ns.bar == 2 |
217
|
|
|
assert Test.ns.foo == 1 |
218
|
|
|
assert Test.ns.bar == 2 |
219
|
|
|
|
220
|
|
|
|
221
|
|
|
def test_basic_prop(namespaces): |
222
|
|
|
class Test(metaclass=namespaces.Namespaceable): |
223
|
|
|
with namespaces.Namespace() as ns: |
224
|
|
|
@property |
225
|
|
|
def foo(self): |
226
|
|
|
return 1 |
227
|
|
|
assert Test().ns.foo == 1 |
228
|
|
|
|
229
|
|
|
|
230
|
|
|
def test_override_method(namespaces): |
231
|
|
|
class Test(metaclass=namespaces.Namespaceable): |
232
|
|
|
with namespaces.Namespace() as ns: |
233
|
|
|
def foo(self): |
234
|
|
|
return 1 |
235
|
|
|
test = Test() |
236
|
|
|
assert test.ns.foo() == 1 |
237
|
|
|
test.ns.foo = 2 |
238
|
|
|
print(vars(test)) |
239
|
|
|
assert test.ns.foo == 2 |
240
|
|
|
del test.ns.foo |
241
|
|
|
assert test.ns.foo() == 1 |
242
|
|
|
Test.ns.foo = 3 |
243
|
|
|
assert Test.ns.foo == 3 |
244
|
|
|
assert test.ns.foo == 3 |
245
|
|
|
|
246
|
|
|
|
247
|
|
|
def test_can_t_preload_with_namespace(namespaces): |
248
|
|
|
with pytest.raises(ValueError): |
249
|
|
|
namespaces.Namespace(ns=namespaces.Namespace()) |
250
|
|
|
|
251
|
|
|
|
252
|
|
|
def test_add_later(namespaces): |
253
|
|
|
class Test(metaclass=namespaces.Namespaceable): |
254
|
|
|
pass |
255
|
|
|
|
256
|
|
|
Test.ns = namespaces.Namespace() |
257
|
|
|
Test.ns.ns = namespaces.Namespace() |
258
|
|
|
Test.ns.value = 1 |
259
|
|
|
Test.ns.ns.value = 2 |
260
|
|
|
assert Test.ns.value == 1 |
261
|
|
|
assert Test.ns.ns.value == 2 |
262
|
|
|
|
263
|
|
|
|
264
|
|
|
@pytest.mark.xfail(sys.version_info < (3, 6), |
265
|
|
|
reason="python3.6 api changes", strict=True) |
266
|
|
|
def test_3_6_descriptor(namespaces): |
267
|
|
|
class Descriptor: |
268
|
|
|
def __set_name__(self, owner, name): |
269
|
|
|
self.owner = owner |
270
|
|
|
self.name = name |
271
|
|
|
assert namespaces.namespaces._DescriptorInspector( |
272
|
|
|
Descriptor()).is_descriptor |
273
|
|
|
|
274
|
|
|
class Test(metaclass=namespaces.Namespaceable): |
275
|
|
|
with namespaces.Namespace() as ns: |
276
|
|
|
d = Descriptor() |
277
|
|
|
|
278
|
|
|
assert Test.ns.d.name == 'd' |
279
|
|
|
|
280
|
|
|
|
281
|
|
|
def test_basic_meta(namespaces): |
282
|
|
|
class Meta(type, metaclass=namespaces.Namespaceable): |
283
|
|
|
with namespaces.Namespace() as ns: |
284
|
|
|
meta_var = 1 |
285
|
|
|
|
286
|
|
|
class Test(metaclass=Meta): |
287
|
|
|
pass |
288
|
|
|
|
289
|
|
|
assert Meta.ns.meta_var == 1 |
290
|
|
|
assert Test.ns.meta_var == 1 |
291
|
|
|
|
292
|
|
|
|
293
|
|
|
def test_weird_meta(namespaces): |
294
|
|
|
class Meta(namespaces.Namespaceable, metaclass=namespaces.Namespaceable): |
295
|
|
|
with namespaces.Namespace() as ns: |
296
|
|
|
meta_var = 1 |
297
|
|
|
|
298
|
|
|
class Test(metaclass=Meta): |
299
|
|
|
with namespaces.Namespace() as ns: |
300
|
|
|
cls_var = 2 |
301
|
|
|
|
302
|
|
|
assert Meta.ns.meta_var == 1 |
303
|
|
|
assert Test.ns.meta_var == 1 |
304
|
|
|
assert Test.ns.cls_var == 2 |
305
|
|
|
assert Test().ns.cls_var == 2 |
306
|
|
|
with pytest.raises(AttributeError): |
307
|
|
|
Test().ns.meta_var |
308
|
|
|
|