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(namespaces.Namespaceable): |
12
|
|
|
pass |
13
|
|
|
assert Test |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
def test_basic_namespace(namespaces): |
17
|
|
|
class Test(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(namespaces.Namespaceable): |
27
|
|
|
with namespaces.Namespace() as ns: |
28
|
|
|
a = 1 |
29
|
|
|
del a |
30
|
|
|
b = 2 |
31
|
|
|
assert ns |
32
|
|
|
assert Test |
33
|
|
|
assert Test().ns |
34
|
|
|
assert Test.ns.b == 2 |
35
|
|
|
assert Test().ns.b == 2 |
36
|
|
|
with pytest.raises(AttributeError): |
37
|
|
|
del Test().ns.b |
38
|
|
|
del Test.ns.b |
39
|
|
|
with pytest.raises(AttributeError): |
40
|
|
|
Test.ns.b |
41
|
|
|
with pytest.raises(AttributeError): |
42
|
|
|
Test().ns.b |
43
|
|
|
|
44
|
|
|
|
45
|
|
|
def test_set(namespaces): |
46
|
|
|
class Test(namespaces.Namespaceable): |
47
|
|
|
with namespaces.Namespace() as ns: |
48
|
|
|
a = 1 |
49
|
|
|
del a |
50
|
|
|
assert ns |
51
|
|
|
assert Test |
52
|
|
|
assert Test().ns |
53
|
|
|
Test.ns.b = 2 |
54
|
|
|
assert Test.ns.b == 2 |
55
|
|
|
assert Test().ns.b == 2 |
56
|
|
|
test = Test() |
57
|
|
|
test.ns.b = 3 |
58
|
|
|
assert Test.ns.b == 2 |
59
|
|
|
assert test.ns.b == 3 |
60
|
|
|
test2 = Test() |
61
|
|
|
test2.ns.c = 3 |
62
|
|
|
with pytest.raises(AttributeError): |
63
|
|
|
Test.ns.c |
64
|
|
|
with pytest.raises(AttributeError): |
65
|
|
|
test.ns.c |
66
|
|
|
assert test2.ns.c == 3 |
67
|
|
|
|
68
|
|
|
|
69
|
|
|
@pytest.mark.xfail(sys.version_info < (3, 4), |
70
|
|
|
reason="python3.4 api changes?", strict=True) |
71
|
|
|
def test_dir(namespaces): |
72
|
|
|
class Test(namespaces.Namespaceable): |
73
|
|
|
with namespaces.Namespace() as ns: |
74
|
|
|
a = 1 |
75
|
|
|
assert ns |
76
|
|
|
assert dir(ns) == ['a'] |
77
|
|
|
assert dir(Test.ns) == ['a'] |
78
|
|
|
assert dir(Test().ns) == ['a'] |
79
|
|
|
|
80
|
|
|
|
81
|
|
|
def test_shadow(namespaces): |
82
|
|
|
class Test(namespaces.Namespaceable): |
83
|
|
|
foo = 1 |
84
|
|
|
with namespaces.Namespace() as ns: |
85
|
|
|
foo = 2 |
86
|
|
|
assert foo == 2 |
87
|
|
|
assert foo == 1 |
88
|
|
|
assert Test().foo == 1 |
89
|
|
|
assert Test().ns.foo == 2 |
90
|
|
|
|
91
|
|
View Code Duplication |
|
|
|
|
|
92
|
|
|
def test_resume(namespaces): |
93
|
|
|
class Test(namespaces.Namespaceable): |
94
|
|
|
foo = 1 |
95
|
|
|
with namespaces.Namespace() as ns: |
96
|
|
|
foo = 2 |
97
|
|
|
assert foo == 2 |
98
|
|
|
foo = 3 |
99
|
|
|
with ns: |
100
|
|
|
foo = 4 |
101
|
|
|
assert foo == 3 |
102
|
|
|
assert Test().foo == 3 |
103
|
|
|
assert Test().ns.foo == 4 |
104
|
|
|
|
105
|
|
View Code Duplication |
|
|
|
|
|
106
|
|
|
def test_redundant_resume(namespaces): |
107
|
|
|
class Test(namespaces.Namespaceable): |
108
|
|
|
foo = 1 |
109
|
|
|
with namespaces.Namespace() as ns: |
110
|
|
|
foo = 2 |
111
|
|
|
assert foo == 2 |
112
|
|
|
foo = 3 |
113
|
|
|
ns = ns |
114
|
|
|
with ns as ns: |
115
|
|
|
foo = 4 |
116
|
|
|
assert foo == 3 |
117
|
|
|
assert Test().foo == 3 |
118
|
|
|
assert Test().ns.foo == 4 |
119
|
|
|
|
120
|
|
|
|
121
|
|
|
def test_basic_inherit(namespaces): |
122
|
|
|
class Test(namespaces.Namespaceable): |
123
|
|
|
foo = 1 |
124
|
|
|
with namespaces.Namespace() as ns: |
125
|
|
|
foo = 2 |
126
|
|
|
|
127
|
|
|
class Subclass(Test): |
128
|
|
|
pass |
129
|
|
|
assert Subclass().foo == 1 |
130
|
|
|
assert Subclass().ns.foo == 2 |
131
|
|
|
|
132
|
|
|
|
133
|
|
|
def test_basic_super(namespaces): |
134
|
|
|
class Test(namespaces.Namespaceable): |
135
|
|
|
with namespaces.Namespace() as ns: |
136
|
|
|
def hello(self): |
137
|
|
|
return 1 |
138
|
|
|
|
139
|
|
|
class Subclass(Test): |
140
|
|
|
with namespaces.Namespace() as ns: |
141
|
|
|
def hello(self): |
142
|
|
|
return super().ns.hello() |
143
|
|
|
|
144
|
|
|
assert Test().ns.hello() == 1 |
145
|
|
|
assert Subclass().ns.hello() == 1 |
146
|
|
|
|
147
|
|
|
|
148
|
|
|
def test_private(namespaces): |
149
|
|
|
class Test(namespaces.Namespaceable): |
150
|
|
|
with namespaces.Namespace() as __ns: |
151
|
|
|
foo = 2 |
152
|
|
|
|
153
|
|
|
def foo(self): |
154
|
|
|
return self.__ns.foo |
155
|
|
|
|
156
|
|
|
class Subclass(Test): |
157
|
|
|
pass |
158
|
|
|
|
159
|
|
|
assert Test().foo() == 2 |
160
|
|
|
assert Subclass().foo() == 2 |
161
|
|
|
|
162
|
|
|
|
163
|
|
|
def test_nested_namespace(namespaces): |
164
|
|
|
class Test(namespaces.Namespaceable): |
165
|
|
|
with namespaces.Namespace() as ns: |
166
|
|
|
with namespaces.Namespace() as ns: |
167
|
|
|
a = 1 |
168
|
|
|
assert Test().ns.ns.a == 1 |
169
|
|
|
|
170
|
|
|
|
171
|
|
|
def test_basic_shadow(namespaces): |
172
|
|
|
class Test(namespaces.Namespaceable): |
173
|
|
|
with namespaces.Namespace() as ns: |
174
|
|
|
foo = 2 |
175
|
|
|
|
176
|
|
|
class Subclass(Test): |
177
|
|
|
ns = 1 |
178
|
|
|
assert Subclass().ns == 1 |
179
|
|
|
|
180
|
|
|
|
181
|
|
|
def test_double_shadow(namespaces): |
182
|
|
|
class Test(namespaces.Namespaceable): |
183
|
|
|
with namespaces.Namespace() as ns: |
184
|
|
|
foo = 2 |
185
|
|
|
|
186
|
|
|
class Subclass(Test): |
187
|
|
|
ns = 1 |
188
|
|
|
|
189
|
|
|
class DoubleSubclass(Subclass): |
190
|
|
|
with namespaces.Namespace() as ns: |
191
|
|
|
bar = 1 |
192
|
|
|
assert not hasattr(DoubleSubclass().ns, 'foo') |
193
|
|
|
|
194
|
|
|
|
195
|
|
|
def test_overlap(namespaces): |
196
|
|
|
class Test(namespaces.Namespaceable): |
197
|
|
|
with namespaces.Namespace() as ns: |
198
|
|
|
foo = 2 |
199
|
|
|
|
200
|
|
|
class Subclass(Test): |
201
|
|
|
with namespaces.Namespace() as ns: |
202
|
|
|
bar = 3 |
203
|
|
|
assert Subclass().ns.foo == 2 |
204
|
|
|
assert Subclass().ns.bar == 3 |
205
|
|
|
|
206
|
|
View Code Duplication |
|
|
|
|
|
207
|
|
|
def test_advanced_overlap(namespaces): |
208
|
|
|
class Test(namespaces.Namespaceable): |
209
|
|
|
with namespaces.Namespace() as ns: |
210
|
|
|
foo = 2 |
211
|
|
|
with namespaces.Namespace() as ns: |
212
|
|
|
qux = 4 |
213
|
|
|
|
214
|
|
|
class Subclass(Test): |
215
|
|
|
with namespaces.Namespace() as ns: |
216
|
|
|
bar = 3 |
217
|
|
|
assert Subclass().ns.foo == 2 |
218
|
|
|
assert Subclass().ns.bar == 3 |
219
|
|
|
assert Subclass().ns.ns.qux == 4 |
220
|
|
|
|
221
|
|
|
|
222
|
|
|
def test_empty_nameless(namespaces): |
223
|
|
|
class Test(namespaces.Namespaceable): |
224
|
|
|
with pytest.raises(RuntimeError): |
225
|
|
|
with namespaces.Namespace(): |
226
|
|
|
pass |
227
|
|
|
|
228
|
|
|
|
229
|
|
|
def test_non_empty_nameless(namespaces): |
230
|
|
|
class Test(namespaces.Namespaceable): |
231
|
|
|
with pytest.raises(RuntimeError): |
232
|
|
|
with namespaces.Namespace(): |
233
|
|
|
a = 1 |
234
|
|
|
|
235
|
|
|
|
236
|
|
|
def test_rename(namespaces): |
237
|
|
|
class Test(namespaces.Namespaceable): |
238
|
|
|
with namespaces.Namespace() as ns: |
239
|
|
|
pass |
240
|
|
|
with pytest.raises(ValueError): |
241
|
|
|
with ns as ns2: |
242
|
|
|
pass |
243
|
|
|
|
244
|
|
|
|
245
|
|
|
def test_use_namespace(namespaces): |
246
|
|
|
class Test(namespaces.Namespaceable): |
247
|
|
|
with namespaces.Namespace() as ns: |
248
|
|
|
foo = 1 |
249
|
|
|
qux = 3 |
250
|
|
|
assert ns.foo == 1 |
251
|
|
|
ns.bar = 2 |
252
|
|
|
assert ns.bar == 2 |
253
|
|
|
del ns.qux |
254
|
|
|
with pytest.raises(AttributeError): |
255
|
|
|
del ns.qux |
256
|
|
|
assert Test.ns.foo == 1 |
257
|
|
|
assert Test.ns.bar == 2 |
258
|
|
|
|
259
|
|
|
|
260
|
|
|
def test_basic_prop(namespaces): |
261
|
|
|
class Test(namespaces.Namespaceable): |
262
|
|
|
with namespaces.Namespace() as ns: |
263
|
|
|
@property |
264
|
|
|
def foo(self): |
265
|
|
|
return 1 |
266
|
|
|
assert Test().ns.foo == 1 |
267
|
|
|
|
268
|
|
|
|
269
|
|
|
def test_complicated_prop(namespaces): |
270
|
|
|
class Test(namespaces.Namespaceable): |
271
|
|
|
with namespaces.Namespace() as ns: |
272
|
|
|
@property |
273
|
|
|
def var(self): |
274
|
|
|
return self.__private.var |
275
|
|
|
|
276
|
|
|
@var.setter |
277
|
|
|
def var(self, value): |
278
|
|
|
self.__private.var = value + 1 |
279
|
|
|
|
280
|
|
|
@var.deleter |
281
|
|
|
def var(self): |
282
|
|
|
del self.__private.var |
283
|
|
|
|
284
|
|
|
with namespaces.Namespace() as __private: |
285
|
|
|
var = None |
286
|
|
|
|
287
|
|
|
test = Test() |
288
|
|
|
assert test.ns.var is None |
289
|
|
|
test.ns.var = 1 |
290
|
|
|
assert test.ns.var == 2 |
291
|
|
|
del test.ns.var |
292
|
|
|
assert test.ns.var is None |
293
|
|
|
|
294
|
|
|
|
295
|
|
|
def test_override_method(namespaces): |
296
|
|
|
class Test(namespaces.Namespaceable): |
297
|
|
|
with namespaces.Namespace() as ns: |
298
|
|
|
def foo(self): |
299
|
|
|
return 1 |
300
|
|
|
test = Test() |
301
|
|
|
assert test.ns.foo() == 1 |
302
|
|
|
test.ns.foo = 2 |
303
|
|
|
print(vars(test)) |
304
|
|
|
assert test.ns.foo == 2 |
305
|
|
|
del test.ns.foo |
306
|
|
|
assert test.ns.foo() == 1 |
307
|
|
|
Test.ns.foo = 3 |
308
|
|
|
assert Test.ns.foo == 3 |
309
|
|
|
assert test.ns.foo == 3 |
310
|
|
|
|
311
|
|
|
|
312
|
|
|
def test_can_t_preload_with_namespace(namespaces): |
313
|
|
|
with pytest.raises(ValueError): |
314
|
|
|
namespaces.Namespace(ns=namespaces.Namespace()) |
315
|
|
|
|
316
|
|
|
|
317
|
|
|
def test_add_later(namespaces): |
318
|
|
|
class Test(namespaces.Namespaceable): |
319
|
|
View Code Duplication |
pass |
|
|
|
|
320
|
|
|
|
321
|
|
|
Test.ns = namespaces.Namespace() |
322
|
|
|
Test.ns.ns = namespaces.Namespace() |
323
|
|
|
Test.ns.value = 1 |
324
|
|
|
Test.ns.ns.value = 2 |
325
|
|
|
assert Test.ns.value == 1 |
326
|
|
|
assert Test.ns.ns.value == 2 |
327
|
|
|
|
328
|
|
|
|
329
|
|
|
@pytest.mark.xfail(sys.version_info < (3, 6), |
330
|
|
|
reason="python3.6 api changes", strict=True) |
331
|
|
|
def test_3_6_descriptor(namespaces): |
332
|
|
|
class Descriptor: |
333
|
|
|
def __set_name__(self, owner, name): |
334
|
|
|
self.owner = owner |
335
|
|
|
self.name = name |
336
|
|
|
assert namespaces.namespaces._DescriptorInspector( |
337
|
|
|
Descriptor()).is_descriptor |
338
|
|
|
|
339
|
|
|
class Test(namespaces.Namespaceable): |
340
|
|
|
with namespaces.Namespace() as ns: |
341
|
|
|
d = Descriptor() |
342
|
|
|
|
343
|
|
|
assert Test.ns.d.name == 'd' |
344
|
|
|
|
345
|
|
|
|
346
|
|
|
def test_basic_meta(namespaces): |
347
|
|
|
class Meta(namespaces.Namespaceable, type(namespaces.Namespaceable)): |
348
|
|
|
with namespaces.Namespace() as ns: |
349
|
|
|
meta_var = 1 |
350
|
|
|
|
351
|
|
|
class Test(namespaces.Namespaceable, metaclass=Meta): |
352
|
|
|
pass |
353
|
|
|
|
354
|
|
|
assert Meta.ns.meta_var == 1 |
355
|
|
|
assert Test.ns.meta_var == 1 |
356
|
|
|
with pytest.raises(AttributeError): |
357
|
|
|
Test().ns.meta_var |
358
|
|
|
|
359
|
|
|
|
360
|
|
|
def test_somewhat_weirder_meta(namespaces): |
361
|
|
|
class Meta(namespaces.Namespaceable, type(namespaces.Namespaceable)): |
362
|
|
|
with namespaces.Namespace() as ns: |
363
|
|
|
meta_var = 1 |
364
|
|
|
|
365
|
|
|
class Test(namespaces.Namespaceable, metaclass=Meta): |
366
|
|
|
with namespaces.Namespace() as ns: |
367
|
|
|
cls_var = 2 |
368
|
|
|
|
369
|
|
|
assert Meta.ns.meta_var == 1 |
370
|
|
|
assert Test.ns.meta_var == 1 |
371
|
|
|
assert Test.ns.cls_var == 2 |
372
|
|
|
assert Test().ns.cls_var == 2 |
373
|
|
|
with pytest.raises(AttributeError): |
374
|
|
|
Test().ns.meta_var |
375
|
|
|
with pytest.raises(AttributeError): |
376
|
|
|
Test.ns.var |
377
|
|
|
with pytest.raises(AttributeError): |
378
|
|
|
Meta.ns.cls_var |
379
|
|
|
Test.var = 3 |
380
|
|
|
assert Test.var == 3 |
381
|
|
|
Meta.var = 4 |
382
|
|
|
assert Meta.var == 4 |
383
|
|
|
|
384
|
|
|
|
385
|
|
|
def test_classmethod_basic(namespaces): |
386
|
|
|
class Test(namespaces.Namespaceable): |
387
|
|
|
with namespaces.Namespace() as ns: |
388
|
|
|
@classmethod |
389
|
|
|
def cls_mthd(cls): |
390
|
|
|
return 'called' |
391
|
|
|
|
392
|
|
|
assert Test.ns.cls_mthd() == 'called' |
393
|
|
|
assert Test().ns.cls_mthd() == 'called' |
394
|
|
View Code Duplication |
|
|
|
|
|
395
|
|
|
|
396
|
|
|
def test_meta_plus_classmethod(namespaces): |
397
|
|
|
class Meta(namespaces.Namespaceable, type(namespaces.Namespaceable)): |
398
|
|
|
with namespaces.Namespace() as ns: |
399
|
|
|
pass |
400
|
|
|
|
401
|
|
|
class Test(namespaces.Namespaceable, metaclass=Meta): |
402
|
|
|
with namespaces.Namespace() as ns: |
403
|
|
|
@classmethod |
404
|
|
|
def cls_mthd(cls): |
405
|
|
|
return 'called' |
406
|
|
|
|
407
|
|
|
assert Test().ns.cls_mthd() == 'called' |
408
|
|
|
assert Test.ns.cls_mthd() == 'called' |
409
|
|
|
|
410
|
|
|
|
411
|
|
|
def test_get_through_namespace(namespaces): |
412
|
|
|
class Test(namespaces.Namespaceable): |
413
|
|
|
var = 1 |
414
|
|
|
with namespaces.Namespace() as ns: |
415
|
|
|
var2 = var |
416
|
|
|
|
417
|
|
|
assert Test.var == 1 |
418
|
|
|
assert Test.ns.var2 == 1 |
419
|
|
|
|
420
|
|
|
|
421
|
|
|
def test_multiple_inheritance(namespaces): |
422
|
|
|
class Test1(namespaces.Namespaceable): |
423
|
|
|
with namespaces.Namespace() as ns: |
424
|
|
|
with namespaces.Namespace() as ns: |
425
|
|
|
var = 1 |
426
|
|
|
|
427
|
|
|
class Test2(namespaces.Namespaceable): |
428
|
|
|
with namespaces.Namespace() as ns: |
429
|
|
|
var = 2 |
430
|
|
|
|
431
|
|
|
class Test3(Test2, Test1): |
432
|
|
|
pass |
433
|
|
|
|
434
|
|
|
assert Test3.ns.ns.var == 1 |
435
|
|
|
assert Test3.ns.var == 2 |
436
|
|
|
|
437
|
|
|
|
438
|
|
|
def test_star_attr_functions(namespaces): |
439
|
|
|
class Test(namespaces.Namespaceable): |
440
|
|
|
with namespaces.Namespace() as ns: |
441
|
|
|
with namespaces.Namespace() as ns: |
442
|
|
|
with namespaces.Namespace() as ns: |
443
|
|
|
pass |
444
|
|
|
|
445
|
|
|
setattr(Test, 'ns.ns.ns.var', 1) |
446
|
|
|
assert hasattr(Test, 'ns.ns.ns.var') |
447
|
|
|
assert getattr(Test, 'ns.ns.ns.var') == 1 |
448
|
|
|
assert Test.ns.ns.ns.var == 1 |
449
|
|
|
delattr(Test, 'ns.ns.ns.var') |
450
|
|
|
assert not hasattr(Test, 'ns.ns.ns.var') |
451
|
|
|
|
452
|
|
|
|
453
|
|
|
def test_must_inherit(namespaces): |
454
|
|
|
with pytest.raises(ValueError): |
455
|
|
|
class Test(metaclass=type(namespaces.Namespaceable)): |
456
|
|
|
pass |
457
|
|
|
|