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
|
|
|
|
92
|
|
View Code Duplication |
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
|
|
|
|
106
|
|
|
def get_ns(ns): |
107
|
|
|
from class_namespaces import scope_proxy |
108
|
|
|
return scope_proxy._PROXY_INFOS[ns][ns] |
109
|
|
|
|
110
|
|
|
|
111
|
|
|
def scope_dicts_length_equals(ns, length): |
112
|
|
|
scope = get_ns(ns).scope |
113
|
|
|
assert len(scope.dicts) == length |
114
|
|
|
|
115
|
|
|
|
116
|
|
View Code Duplication |
def test_redundant_resume(namespaces): |
|
|
|
|
117
|
|
|
class Test(namespaces.Namespaceable): |
118
|
|
|
foo = 1 |
119
|
|
|
with namespaces.Namespace() as ns: |
120
|
|
|
foo = 2 |
121
|
|
|
assert foo == 2 |
122
|
|
|
scope_dicts_length_equals(ns, 1) |
123
|
|
|
foo = 3 |
124
|
|
|
ns = ns |
125
|
|
|
scope_dicts_length_equals(ns, 1) |
126
|
|
|
with ns as ns: |
127
|
|
|
foo = 4 |
128
|
|
|
assert foo == 3 |
129
|
|
|
assert Test().foo == 3 |
130
|
|
|
assert Test().ns.foo == 4 |
131
|
|
|
|
132
|
|
|
|
133
|
|
|
def test_basic_inherit(namespaces): |
134
|
|
|
class Test(namespaces.Namespaceable): |
135
|
|
|
foo = 1 |
136
|
|
|
with namespaces.Namespace() as ns: |
137
|
|
|
foo = 2 |
138
|
|
|
|
139
|
|
|
class Subclass(Test): |
140
|
|
|
pass |
141
|
|
|
assert Subclass().foo == 1 |
142
|
|
|
assert Subclass().ns.foo == 2 |
143
|
|
|
|
144
|
|
|
|
145
|
|
|
def test_basic_super(namespaces): |
146
|
|
|
class Test(namespaces.Namespaceable): |
147
|
|
|
with namespaces.Namespace() as ns: |
148
|
|
|
def hello(self): |
149
|
|
|
return 1 |
150
|
|
|
|
151
|
|
|
class Subclass(Test): |
152
|
|
|
with namespaces.Namespace() as ns: |
153
|
|
|
def hello(self): |
154
|
|
|
return super().ns.hello() |
155
|
|
|
|
156
|
|
|
assert Test().ns.hello() == 1 |
157
|
|
|
assert Subclass().ns.hello() == 1 |
158
|
|
|
|
159
|
|
|
|
160
|
|
|
def test_private(namespaces): |
161
|
|
|
class Test(namespaces.Namespaceable): |
162
|
|
|
with namespaces.Namespace() as __ns: |
163
|
|
|
foo = 2 |
164
|
|
|
|
165
|
|
|
def foo(self): |
166
|
|
|
return self.__ns.foo |
167
|
|
|
|
168
|
|
|
class Subclass(Test): |
169
|
|
|
pass |
170
|
|
|
|
171
|
|
|
assert Test().foo() == 2 |
172
|
|
|
assert Subclass().foo() == 2 |
173
|
|
|
|
174
|
|
|
|
175
|
|
|
def test_nested_namespace(namespaces): |
176
|
|
|
class Test(namespaces.Namespaceable): |
177
|
|
|
with namespaces.Namespace() as ns: |
178
|
|
|
with namespaces.Namespace() as ns: |
179
|
|
|
a = 1 |
180
|
|
|
assert Test().ns.ns.a == 1 |
181
|
|
|
|
182
|
|
|
|
183
|
|
|
def test_basic_shadow(namespaces): |
184
|
|
|
class Test(namespaces.Namespaceable): |
185
|
|
|
with namespaces.Namespace() as ns: |
186
|
|
|
foo = 2 |
187
|
|
|
|
188
|
|
|
class Subclass(Test): |
189
|
|
|
ns = 1 |
190
|
|
|
assert Subclass().ns == 1 |
191
|
|
|
|
192
|
|
|
|
193
|
|
|
def test_double_shadow(namespaces): |
194
|
|
|
class Test(namespaces.Namespaceable): |
195
|
|
|
with namespaces.Namespace() as ns: |
196
|
|
|
foo = 2 |
197
|
|
|
|
198
|
|
|
class Subclass(Test): |
199
|
|
|
ns = 1 |
200
|
|
|
|
201
|
|
|
class DoubleSubclass(Subclass): |
202
|
|
|
with namespaces.Namespace() as ns: |
203
|
|
|
bar = 1 |
204
|
|
|
assert not hasattr(DoubleSubclass().ns, 'foo') |
205
|
|
|
|
206
|
|
View Code Duplication |
|
|
|
|
|
207
|
|
|
def test_overlap(namespaces): |
208
|
|
|
class Test(namespaces.Namespaceable): |
209
|
|
|
with namespaces.Namespace() as ns: |
210
|
|
|
foo = 2 |
211
|
|
|
|
212
|
|
|
class Subclass(Test): |
213
|
|
|
with namespaces.Namespace() as ns: |
214
|
|
|
bar = 3 |
215
|
|
|
assert Subclass().ns.foo == 2 |
216
|
|
|
assert Subclass().ns.bar == 3 |
217
|
|
|
|
218
|
|
|
|
219
|
|
|
def test_advanced_overlap(namespaces): |
220
|
|
|
class Test(namespaces.Namespaceable): |
221
|
|
|
with namespaces.Namespace() as ns: |
222
|
|
|
foo = 2 |
223
|
|
|
with namespaces.Namespace() as ns: |
224
|
|
|
qux = 4 |
225
|
|
|
|
226
|
|
|
class Subclass(Test): |
227
|
|
|
with namespaces.Namespace() as ns: |
228
|
|
|
bar = 3 |
229
|
|
|
assert Subclass().ns.foo == 2 |
230
|
|
|
assert Subclass().ns.bar == 3 |
231
|
|
|
assert Subclass().ns.ns.qux == 4 |
232
|
|
|
|
233
|
|
|
|
234
|
|
|
def test_empty_nameless(namespaces): |
235
|
|
|
class Test(namespaces.Namespaceable): |
236
|
|
|
with pytest.raises(RuntimeError): |
237
|
|
|
with namespaces.Namespace(): |
238
|
|
|
pass |
239
|
|
|
|
240
|
|
|
|
241
|
|
|
def test_non_empty_nameless(namespaces): |
242
|
|
|
class Test(namespaces.Namespaceable): |
243
|
|
|
with pytest.raises(RuntimeError): |
244
|
|
|
with namespaces.Namespace(): |
245
|
|
|
a = 1 |
246
|
|
|
|
247
|
|
|
|
248
|
|
|
def test_rename(namespaces): |
249
|
|
|
class Test(namespaces.Namespaceable): |
250
|
|
|
with namespaces.Namespace() as ns: |
251
|
|
|
pass |
252
|
|
|
with pytest.raises(ValueError): |
253
|
|
|
with ns as ns2: |
254
|
|
|
pass |
255
|
|
|
|
256
|
|
|
|
257
|
|
|
def test_use_namespace(namespaces): |
258
|
|
|
class Test(namespaces.Namespaceable): |
259
|
|
|
with namespaces.Namespace() as ns: |
260
|
|
|
foo = 1 |
261
|
|
|
qux = 3 |
262
|
|
|
assert ns.foo == 1 |
263
|
|
|
ns.bar = 2 |
264
|
|
|
assert ns.bar == 2 |
265
|
|
|
del ns.qux |
266
|
|
|
with pytest.raises(AttributeError): |
267
|
|
|
del ns.qux |
268
|
|
|
assert Test.ns.foo == 1 |
269
|
|
|
assert Test.ns.bar == 2 |
270
|
|
|
|
271
|
|
|
|
272
|
|
|
def test_basic_prop(namespaces): |
273
|
|
|
class Test(namespaces.Namespaceable): |
274
|
|
|
with namespaces.Namespace() as ns: |
275
|
|
|
@property |
276
|
|
|
def foo(self): |
277
|
|
|
return 1 |
278
|
|
|
assert Test().ns.foo == 1 |
279
|
|
|
|
280
|
|
|
|
281
|
|
|
def test_complicated_prop(namespaces): |
282
|
|
|
class Test(namespaces.Namespaceable): |
283
|
|
|
with namespaces.Namespace() as ns: |
284
|
|
|
@property |
285
|
|
|
def var(self): |
286
|
|
|
return self.__private.var |
287
|
|
|
|
288
|
|
|
@var.setter |
289
|
|
|
def var(self, value): |
290
|
|
|
self.__private.var = value + 1 |
291
|
|
|
|
292
|
|
|
@var.deleter |
293
|
|
|
def var(self): |
294
|
|
|
del self.__private.var |
295
|
|
|
|
296
|
|
|
with namespaces.Namespace() as __private: |
297
|
|
|
var = None |
298
|
|
|
|
299
|
|
|
test = Test() |
300
|
|
|
assert test.ns.var is None |
301
|
|
|
test.ns.var = 1 |
302
|
|
|
assert test.ns.var == 2 |
303
|
|
|
del test.ns.var |
304
|
|
|
assert test.ns.var is None |
305
|
|
|
|
306
|
|
|
|
307
|
|
|
def test_override_method(namespaces): |
308
|
|
|
class Test(namespaces.Namespaceable): |
309
|
|
|
with namespaces.Namespace() as ns: |
310
|
|
|
def foo(self): |
311
|
|
|
return 1 |
312
|
|
|
test = Test() |
313
|
|
|
assert test.ns.foo() == 1 |
314
|
|
|
test.ns.foo = 2 |
315
|
|
|
print(vars(test)) |
316
|
|
|
assert test.ns.foo == 2 |
317
|
|
|
del test.ns.foo |
318
|
|
|
assert test.ns.foo() == 1 |
319
|
|
View Code Duplication |
Test.ns.foo = 3 |
|
|
|
|
320
|
|
|
assert Test.ns.foo == 3 |
321
|
|
|
assert test.ns.foo == 3 |
322
|
|
|
|
323
|
|
|
|
324
|
|
|
def test_can_t_preload_with_namespace(namespaces): |
325
|
|
|
with pytest.raises(ValueError): |
326
|
|
|
namespaces.Namespace(ns=namespaces.Namespace()) |
327
|
|
|
|
328
|
|
|
|
329
|
|
|
def test_add_later(namespaces): |
330
|
|
|
class Test(namespaces.Namespaceable): |
331
|
|
|
pass |
332
|
|
|
|
333
|
|
|
ns = namespaces.Namespace() |
334
|
|
|
Test.ns = ns |
335
|
|
|
print('ns props') |
336
|
|
|
for slot in namespaces.Namespace.__slots__: |
337
|
|
|
print(slot, getattr(ns, slot)) |
338
|
|
|
ns2 = namespaces.Namespace() |
339
|
|
|
Test.ns.ns = ns2 |
340
|
|
|
print('ns2 props') |
341
|
|
|
for slot in namespaces.Namespace.__slots__: |
342
|
|
|
print(slot, getattr(ns2, slot)) |
343
|
|
|
Test.ns.value = 1 |
344
|
|
|
assert Test.ns.value == 1 |
345
|
|
|
Test.ns.ns.value = 2 |
346
|
|
|
assert Test.ns.ns.value == 2 |
347
|
|
|
assert Test.ns.value == 1 |
348
|
|
|
|
349
|
|
|
|
350
|
|
|
@pytest.mark.xfail(sys.version_info < (3, 6), |
351
|
|
|
reason="python3.6 api changes", strict=True) |
352
|
|
|
def test_3_6_descriptor(namespaces): |
353
|
|
|
class Descriptor: |
354
|
|
|
def __set_name__(self, owner, name): |
355
|
|
|
self.owner = owner |
356
|
|
|
self.name = name |
357
|
|
|
assert namespaces.namespaces._DescriptorInspector( |
358
|
|
|
Descriptor()).is_descriptor |
359
|
|
|
|
360
|
|
|
class Test(namespaces.Namespaceable): |
361
|
|
|
with namespaces.Namespace() as ns: |
362
|
|
|
d = Descriptor() |
363
|
|
|
|
364
|
|
|
assert Test.ns.d.name == 'd' |
365
|
|
|
|
366
|
|
|
|
367
|
|
|
def test_basic_meta(namespaces): |
368
|
|
|
class Meta(namespaces.Namespaceable, type(namespaces.Namespaceable)): |
369
|
|
|
with namespaces.Namespace() as ns: |
370
|
|
|
meta_var = 1 |
371
|
|
|
|
372
|
|
|
class Test(namespaces.Namespaceable, metaclass=Meta): |
373
|
|
|
pass |
374
|
|
|
|
375
|
|
|
assert Meta.ns.meta_var == 1 |
376
|
|
|
assert Test.ns.meta_var == 1 |
377
|
|
|
with pytest.raises(AttributeError): |
378
|
|
|
Test().ns.meta_var |
379
|
|
|
|
380
|
|
|
|
381
|
|
|
def test_somewhat_weirder_meta(namespaces): |
382
|
|
|
class Meta(namespaces.Namespaceable, type(namespaces.Namespaceable)): |
383
|
|
|
with namespaces.Namespace() as ns: |
384
|
|
|
meta_var = 1 |
385
|
|
|
|
386
|
|
|
class Test(namespaces.Namespaceable, metaclass=Meta): |
387
|
|
|
with namespaces.Namespace() as ns: |
388
|
|
|
cls_var = 2 |
389
|
|
|
|
390
|
|
|
assert Meta.ns.meta_var == 1 |
391
|
|
|
assert Test.ns.meta_var == 1 |
392
|
|
|
assert Test.ns.cls_var == 2 |
393
|
|
|
assert Test().ns.cls_var == 2 |
394
|
|
View Code Duplication |
with pytest.raises(AttributeError): |
|
|
|
|
395
|
|
|
Test().ns.meta_var |
396
|
|
|
with pytest.raises(AttributeError): |
397
|
|
|
Test.ns.var |
398
|
|
|
with pytest.raises(AttributeError): |
399
|
|
|
Meta.ns.cls_var |
400
|
|
|
Test.var = 3 |
401
|
|
|
assert Test.var == 3 |
402
|
|
|
Meta.var = 4 |
403
|
|
|
assert Meta.var == 4 |
404
|
|
|
|
405
|
|
|
|
406
|
|
|
def test_classmethod_basic(namespaces): |
407
|
|
|
class Test(namespaces.Namespaceable): |
408
|
|
|
with namespaces.Namespace() as ns: |
409
|
|
|
@classmethod |
410
|
|
|
def cls_mthd(cls): |
411
|
|
|
return 'called' |
412
|
|
|
|
413
|
|
|
assert Test.ns.cls_mthd() == 'called' |
414
|
|
|
assert Test().ns.cls_mthd() == 'called' |
415
|
|
|
|
416
|
|
|
|
417
|
|
|
def test_meta_plus_classmethod(namespaces): |
418
|
|
|
class Meta(namespaces.Namespaceable, type(namespaces.Namespaceable)): |
419
|
|
|
with namespaces.Namespace() as ns: |
420
|
|
|
pass |
421
|
|
|
|
422
|
|
|
class Test(namespaces.Namespaceable, metaclass=Meta): |
423
|
|
|
with namespaces.Namespace() as ns: |
424
|
|
|
@classmethod |
425
|
|
|
def cls_mthd(cls): |
426
|
|
|
return 'called' |
427
|
|
|
|
428
|
|
|
assert Test().ns.cls_mthd() == 'called' |
429
|
|
|
assert Test.ns.cls_mthd() == 'called' |
430
|
|
|
|
431
|
|
|
|
432
|
|
|
def test_get_through_namespace(namespaces): |
433
|
|
|
class Test(namespaces.Namespaceable): |
434
|
|
|
var = 1 |
435
|
|
|
with namespaces.Namespace() as ns: |
436
|
|
|
var2 = var |
437
|
|
|
|
438
|
|
|
assert Test.var == 1 |
439
|
|
|
assert Test.ns.var2 == 1 |
440
|
|
|
|
441
|
|
|
|
442
|
|
|
def test_multiple_inheritance(namespaces): |
443
|
|
|
class Test1(namespaces.Namespaceable): |
444
|
|
|
with namespaces.Namespace() as ns: |
445
|
|
|
with namespaces.Namespace() as ns: |
446
|
|
|
var = 1 |
447
|
|
|
|
448
|
|
|
class Test2(namespaces.Namespaceable): |
449
|
|
|
with namespaces.Namespace() as ns: |
450
|
|
|
var = 2 |
451
|
|
|
|
452
|
|
|
class Test3(Test2, Test1): |
453
|
|
|
pass |
454
|
|
|
|
455
|
|
|
assert Test3.ns.ns.var == 1 |
456
|
|
|
assert Test3.ns.var == 2 |
457
|
|
|
|
458
|
|
|
|
459
|
|
|
def test_star_attr_functions(namespaces): |
460
|
|
|
class Test(namespaces.Namespaceable): |
461
|
|
|
with namespaces.Namespace() as ns: |
462
|
|
|
with namespaces.Namespace() as ns: |
463
|
|
|
with namespaces.Namespace() as ns: |
464
|
|
|
pass |
465
|
|
|
|
466
|
|
|
setattr(Test, 'ns.ns.ns.var', 1) |
467
|
|
|
assert hasattr(Test, 'ns.ns.ns.var') |
468
|
|
|
assert getattr(Test, 'ns.ns.ns.var') == 1 |
469
|
|
|
assert Test.ns.ns.ns.var == 1 |
470
|
|
|
delattr(Test, 'ns.ns.ns.var') |
471
|
|
|
assert not hasattr(Test, 'ns.ns.ns.var') |
472
|
|
|
|
473
|
|
|
|
474
|
|
|
def test_must_inherit(namespaces): |
475
|
|
|
with pytest.raises(ValueError): |
476
|
|
|
class Test(metaclass=type(namespaces.Namespaceable)): |
477
|
|
|
pass |
478
|
|
|
|
479
|
|
|
|
480
|
|
|
def test_regular_delete(namespaces): |
481
|
|
|
class Test(namespaces.Namespaceable): |
482
|
|
|
pass |
483
|
|
|
Test.var = 1 |
484
|
|
|
assert Test.var == 1 |
485
|
|
|
del Test.var |
486
|
|
|
|
487
|
|
|
|
488
|
|
|
def test_too_deep(namespaces): |
489
|
|
|
class Test(namespaces.Namespaceable): |
490
|
|
|
var = None |
491
|
|
|
with pytest.raises(ValueError): |
492
|
|
|
getattr(Test, 'var.__str__') |
493
|
|
|
|
494
|
|
|
|
495
|
|
|
def test_block_reparent(namespaces): |
496
|
|
|
class Test1(namespaces.Namespaceable): |
497
|
|
|
with namespaces.Namespace() as ns: |
498
|
|
|
with pytest.raises(ValueError): |
499
|
|
|
with ns: |
500
|
|
|
pass |
501
|
|
|
with pytest.raises(ValueError): |
502
|
|
|
ns.ns = ns |
503
|
|
|
with pytest.raises(ValueError): |
504
|
|
|
ns.ns = ns |
505
|
|
|
with namespaces.Namespace() as ns2: |
506
|
|
|
with pytest.raises(ValueError): |
507
|
|
|
with ns: |
508
|
|
|
pass |
509
|
|
|
|
510
|
|
|
class Test2(namespaces.Namespaceable): |
511
|
|
|
with pytest.raises(ValueError): |
512
|
|
|
ns = Test1.ns |
513
|
|
|
|
514
|
|
|
|
515
|
|
|
def test_can_t_get_path(namespaces): |
516
|
|
|
with pytest.raises(ValueError): |
517
|
|
|
namespaces.Namespace().path |
518
|
|
|
|
519
|
|
|
|
520
|
|
|
def test_non_existent_attribute_during_creation(namespaces): |
521
|
|
|
class Test(namespaces.Namespaceable): |
522
|
|
|
with namespaces.Namespace() as ns: |
523
|
|
|
pass |
524
|
|
|
with pytest.raises(AttributeError): |
525
|
|
|
ns.var |
526
|
|
|
|