-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathtest_declarations.py
More file actions
2685 lines (2042 loc) · 81.1 KB
/
test_declarations.py
File metadata and controls
2685 lines (2042 loc) · 81.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
##############################################################################
#
# Copyright (c) 2003 Zope Foundation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Test the new API for making and checking interface declarations
"""
import unittest
from zope.interface.tests import MissingSomeAttrs
from zope.interface.tests import OptimizationTestMixin
from zope.interface.tests import SubclassableMixin
from zope.interface.tests.test_interface import \
NameAndModuleComparisonTestsMixin
# pylint:disable=inherit-non-class,too-many-lines,protected-access
# pylint:disable=blacklisted-name,attribute-defined-outside-init
class _Py3ClassAdvice:
def _run_generated_code(
self, code, globs, locs, fails_under_py3k=True,
):
# pylint:disable=exec-used,no-member
import warnings
with warnings.catch_warnings(record=True) as _:
warnings.resetwarnings()
try:
exec(code, globs, locs)
except TypeError:
return False
else:
if fails_under_py3k:
self.fail("Didn't raise TypeError")
return None
class NamedTests(unittest.TestCase):
def test_class(self):
from zope.interface.declarations import named
@named('foo')
class Foo:
pass
self.assertEqual(
Foo.__component_name__, 'foo'
) # pylint:disable=no-member
def test_function(self):
from zope.interface.declarations import named
@named('foo')
def doFoo(o):
raise NotImplementedError()
self.assertEqual(doFoo.__component_name__, 'foo')
def test_instance(self):
from zope.interface.declarations import named
class Foo:
pass
foo = Foo()
named('foo')(foo)
self.assertEqual(
foo.__component_name__, 'foo'
) # pylint:disable=no-member
class EmptyDeclarationTests(unittest.TestCase):
# Tests that should pass for all objects that are empty
# declarations. This includes a Declaration explicitly created
# that way, and the empty ImmutableDeclaration.
def _getEmpty(self):
from zope.interface.declarations import Declaration
return Declaration()
def test___iter___empty(self):
decl = self._getEmpty()
self.assertEqual(list(decl), [])
def test_flattened_empty(self):
from zope.interface.interface import Interface
decl = self._getEmpty()
self.assertEqual(list(decl.flattened()), [Interface])
def test___contains___empty(self):
from zope.interface.interface import Interface
decl = self._getEmpty()
self.assertNotIn(Interface, decl)
def test_extends_empty(self):
from zope.interface.interface import Interface
decl = self._getEmpty()
self.assertTrue(decl.extends(Interface))
self.assertTrue(decl.extends(Interface, strict=True))
def test_interfaces_empty(self):
decl = self._getEmpty()
iface_list = list(decl.interfaces())
self.assertEqual(iface_list, [])
def test___sro___(self):
from zope.interface.interface import Interface
decl = self._getEmpty()
self.assertEqual(decl.__sro__, (decl, Interface,))
def test___iro___(self):
from zope.interface.interface import Interface
decl = self._getEmpty()
self.assertEqual(decl.__iro__, (Interface,))
def test_get(self):
decl = self._getEmpty()
self.assertIsNone(decl.get('attr'))
self.assertEqual(decl.get('abc', 'def'), 'def')
# It's a positive cache only (when it even exists)
# so this added nothing.
self.assertFalse(decl._v_attrs)
def test_changed_w_existing__v_attrs(self):
decl = self._getEmpty()
decl._v_attrs = object()
decl.changed(decl)
self.assertFalse(decl._v_attrs)
class DeclarationTests(EmptyDeclarationTests):
def _getTargetClass(self):
from zope.interface.declarations import Declaration
return Declaration
def _makeOne(self, *args, **kw):
return self._getTargetClass()(*args, **kw)
def test_ctor_no_bases(self):
decl = self._makeOne()
self.assertEqual(list(decl.__bases__), [])
def test_ctor_w_interface_in_bases(self):
from zope.interface.interface import InterfaceClass
IFoo = InterfaceClass('IFoo')
decl = self._makeOne(IFoo)
self.assertEqual(list(decl.__bases__), [IFoo])
def test_ctor_w_implements_in_bases(self):
from zope.interface.declarations import Implements
impl = Implements()
decl = self._makeOne(impl)
self.assertEqual(list(decl.__bases__), [impl])
def test_changed_wo_existing__v_attrs(self):
decl = self._makeOne()
decl.changed(decl) # doesn't raise
self.assertIsNone(decl._v_attrs)
def test___contains__w_self(self):
decl = self._makeOne()
self.assertNotIn(decl, decl)
def test___contains__w_unrelated_iface(self):
from zope.interface.interface import InterfaceClass
IFoo = InterfaceClass('IFoo')
decl = self._makeOne()
self.assertNotIn(IFoo, decl)
def test___contains__w_base_interface(self):
from zope.interface.interface import InterfaceClass
IFoo = InterfaceClass('IFoo')
decl = self._makeOne(IFoo)
self.assertIn(IFoo, decl)
def test___iter___single_base(self):
from zope.interface.interface import InterfaceClass
IFoo = InterfaceClass('IFoo')
decl = self._makeOne(IFoo)
self.assertEqual(list(decl), [IFoo])
def test___iter___multiple_bases(self):
from zope.interface.interface import InterfaceClass
IFoo = InterfaceClass('IFoo')
IBar = InterfaceClass('IBar')
decl = self._makeOne(IFoo, IBar)
self.assertEqual(list(decl), [IFoo, IBar])
def test___iter___inheritance(self):
from zope.interface.interface import InterfaceClass
IFoo = InterfaceClass('IFoo')
IBar = InterfaceClass('IBar', (IFoo,))
decl = self._makeOne(IBar)
self.assertEqual(list(decl), [IBar]) # IBar.interfaces() omits bases
def test___iter___w_nested_sequence_overlap(self):
from zope.interface.interface import InterfaceClass
IFoo = InterfaceClass('IFoo')
IBar = InterfaceClass('IBar')
decl = self._makeOne(IBar, (IFoo, IBar))
self.assertEqual(list(decl), [IBar, IFoo])
def test_flattened_single_base(self):
from zope.interface.interface import Interface
from zope.interface.interface import InterfaceClass
IFoo = InterfaceClass('IFoo')
decl = self._makeOne(IFoo)
self.assertEqual(list(decl.flattened()), [IFoo, Interface])
def test_flattened_multiple_bases(self):
from zope.interface.interface import Interface
from zope.interface.interface import InterfaceClass
IFoo = InterfaceClass('IFoo')
IBar = InterfaceClass('IBar')
decl = self._makeOne(IFoo, IBar)
self.assertEqual(list(decl.flattened()), [IFoo, IBar, Interface])
def test_flattened_inheritance(self):
from zope.interface.interface import Interface
from zope.interface.interface import InterfaceClass
IFoo = InterfaceClass('IFoo')
IBar = InterfaceClass('IBar', (IFoo,))
decl = self._makeOne(IBar)
self.assertEqual(list(decl.flattened()), [IBar, IFoo, Interface])
def test_flattened_w_nested_sequence_overlap(self):
from zope.interface.interface import Interface
from zope.interface.interface import InterfaceClass
IFoo = InterfaceClass('IFoo')
IBar = InterfaceClass('IBar')
# This is the same as calling ``Declaration(IBar, IFoo, IBar)``
# which doesn't make much sense, but here it is. In older
# versions of zope.interface, the __iro__ would have been
# IFoo, IBar, Interface, which especially makes no sense.
decl = self._makeOne(IBar, (IFoo, IBar))
# Note that decl.__iro__ has IFoo first.
self.assertEqual(list(decl.flattened()), [IBar, IFoo, Interface])
def test___sub___unrelated_interface(self):
from zope.interface.interface import InterfaceClass
IFoo = InterfaceClass('IFoo')
IBar = InterfaceClass('IBar')
before = self._makeOne(IFoo)
after = before - IBar
self.assertIsInstance(after, self._getTargetClass())
self.assertEqual(list(after), [IFoo])
def test___sub___related_interface(self):
from zope.interface.interface import InterfaceClass
IFoo = InterfaceClass('IFoo')
before = self._makeOne(IFoo)
after = before - IFoo
self.assertEqual(list(after), [])
def test___sub___related_interface_by_inheritance(self):
from zope.interface.interface import InterfaceClass
IFoo = InterfaceClass('IFoo')
IBar = InterfaceClass('IBar', (IFoo,))
before = self._makeOne(IBar)
after = before - IBar
self.assertEqual(list(after), [])
def test___add___unrelated_interface(self):
from zope.interface.interface import InterfaceClass
IFoo = InterfaceClass('IFoo')
IBar = InterfaceClass('IBar')
before = self._makeOne(IFoo)
after = before + IBar
self.assertIsInstance(after, self._getTargetClass())
self.assertEqual(list(after), [IFoo, IBar])
def test___add___related_interface(self):
from zope.interface.interface import InterfaceClass
IFoo = InterfaceClass('IFoo')
IBar = InterfaceClass('IBar')
IBaz = InterfaceClass('IBaz')
before = self._makeOne(IFoo, IBar)
other = self._makeOne(IBar, IBaz)
after = before + other
self.assertEqual(list(after), [IFoo, IBar, IBaz])
def test___add___overlapping_interface(self):
# The derived interfaces end up with higher priority, and
# don't produce a C3 resolution order violation. This
# example produced a C3 error, and the resulting legacy order
# used to be wrong ([IBase, IDerived] instead of
# the other way).
from zope.interface import Interface
from zope.interface import ro
from zope.interface.interface import InterfaceClass
from zope.interface.tests.test_ro import C3Setting
IBase = InterfaceClass('IBase')
IDerived = InterfaceClass('IDerived', (IBase,))
with C3Setting(ro.C3.STRICT_IRO, True):
base = self._makeOne(IBase)
after = base + IDerived
self.assertEqual(after.__iro__, (IDerived, IBase, Interface))
self.assertEqual(after.__bases__, (IDerived, IBase))
self.assertEqual(list(after), [IDerived, IBase])
def test___add___overlapping_interface_implementedBy(self):
# Like test___add___overlapping_interface, but pulling
# in a realistic example. This one previously produced a
# C3 error, but the resulting legacy order was (somehow)
# correct.
from zope.interface import Interface
from zope.interface import implementedBy
from zope.interface import implementer
from zope.interface import ro
from zope.interface.tests.test_ro import C3Setting
class IBase(Interface):
pass
class IDerived(IBase):
pass
@implementer(IBase)
class Base:
pass
with C3Setting(ro.C3.STRICT_IRO, True):
after = implementedBy(Base) + IDerived
self.assertEqual(after.__sro__, (after, IDerived, IBase, Interface))
self.assertEqual(after.__bases__, (IDerived, IBase))
self.assertEqual(list(after), [IDerived, IBase])
class TestImmutableDeclaration(EmptyDeclarationTests):
def _getTargetClass(self):
from zope.interface.declarations import _ImmutableDeclaration
return _ImmutableDeclaration
def _getEmpty(self):
from zope.interface.declarations import _empty
return _empty
def test_pickle(self):
import pickle
copied = pickle.loads(pickle.dumps(self._getEmpty()))
self.assertIs(copied, self._getEmpty())
def test_singleton(self):
self.assertIs(
self._getTargetClass()(),
self._getEmpty()
)
def test__bases__(self):
self.assertEqual(self._getEmpty().__bases__, ())
def test_change__bases__(self):
empty = self._getEmpty()
empty.__bases__ = ()
self.assertEqual(self._getEmpty().__bases__, ())
with self.assertRaises(TypeError):
empty.__bases__ = (1,)
def test_dependents(self):
empty = self._getEmpty()
deps = empty.dependents
self.assertEqual({}, deps)
# Doesn't change the return.
deps[1] = 2
self.assertEqual({}, empty.dependents)
def test_changed(self):
# Does nothing, has no visible side-effects
self._getEmpty().changed(None)
def test_extends_always_false(self):
self.assertFalse(self._getEmpty().extends(self))
self.assertFalse(self._getEmpty().extends(self, strict=True))
self.assertFalse(self._getEmpty().extends(self, strict=False))
def test_get_always_default(self):
self.assertIsNone(self._getEmpty().get('name'))
self.assertEqual(self._getEmpty().get('name', 42), 42)
def test_v_attrs(self):
decl = self._getEmpty()
self.assertEqual(decl._v_attrs, {})
decl._v_attrs['attr'] = 42
self.assertEqual(decl._v_attrs, {})
self.assertIsNone(decl.get('attr'))
attrs = decl._v_attrs = {}
attrs['attr'] = 42
self.assertEqual(decl._v_attrs, {})
self.assertIsNone(decl.get('attr'))
class TestImplements(NameAndModuleComparisonTestsMixin,
unittest.TestCase):
def _getTargetClass(self):
from zope.interface.declarations import Implements
return Implements
def _makeOne(self, *args, **kw):
return self._getTargetClass()(*args, **kw)
def _makeOneToCompare(self):
from zope.interface.declarations import implementedBy
class A:
pass
return implementedBy(A)
def test_ctor_no_bases(self):
impl = self._makeOne()
self.assertEqual(impl.inherit, None)
self.assertEqual(impl.declared, ())
self.assertEqual(impl.__name__, '?')
self.assertEqual(list(impl.__bases__), [])
def test___repr__(self):
impl = self._makeOne()
impl.__name__ = 'Testing'
self.assertEqual(repr(impl), 'classImplements(Testing)')
def test___reduce__(self):
from zope.interface.declarations import implementedBy
impl = self._makeOne()
self.assertEqual(impl.__reduce__(), (implementedBy, (None,)))
def test_sort(self):
from zope.interface.declarations import implementedBy
from zope.interface.interface import InterfaceClass
class A:
pass
class B:
pass
IFoo = InterfaceClass('IFoo')
self.assertEqual(implementedBy(A), implementedBy(A))
self.assertEqual(hash(implementedBy(A)), hash(implementedBy(A)))
self.assertLess(implementedBy(A), None)
self.assertGreater(
None,
implementedBy(A)
)
self.assertLess(implementedBy(A), implementedBy(B))
self.assertGreater(implementedBy(A), IFoo)
self.assertLessEqual(implementedBy(A), implementedBy(B))
self.assertGreaterEqual(implementedBy(A), IFoo)
self.assertNotEqual(implementedBy(A), IFoo)
def test_proxy_equality(self):
# https://github.com/zopefoundation/zope.interface/issues/55
from zope.interface.declarations import implementedBy
class Proxy:
def __init__(self, wrapped):
self._wrapped = wrapped
def __getattr__(self, name):
raise NotImplementedError()
def __eq__(self, other):
return self._wrapped == other
def __ne__(self, other):
return self._wrapped != other
class A:
pass
class B:
pass
implementedByA = implementedBy(A)
implementedByB = implementedBy(B)
proxy = Proxy(implementedByA)
# The order of arguments to the operators matters,
# test both
self.assertEqual(
implementedByA,
implementedByA
)
self.assertNotEqual(implementedByA, implementedByB)
self.assertNotEqual(implementedByB, implementedByA)
self.assertEqual(proxy, implementedByA)
self.assertEqual(implementedByA, proxy)
self.assertEqual(proxy, implementedByA)
self.assertEqual(implementedByA, proxy)
self.assertNotEqual(proxy, implementedByB)
self.assertNotEqual(implementedByB, proxy)
def test_changed_deletes_super_cache(self):
impl = self._makeOne()
self.assertIsNone(impl._super_cache)
self.assertNotIn('_super_cache', impl.__dict__)
impl._super_cache = 42
self.assertIn('_super_cache', impl.__dict__)
impl.changed(None)
self.assertIsNone(impl._super_cache)
self.assertNotIn('_super_cache', impl.__dict__)
def test_changed_does_not_add_super_cache(self):
impl = self._makeOne()
self.assertIsNone(impl._super_cache)
self.assertNotIn('_super_cache', impl.__dict__)
impl.changed(None)
self.assertIsNone(impl._super_cache)
self.assertNotIn('_super_cache', impl.__dict__)
class Test_implementedByFallback(unittest.TestCase):
def _getTargetClass(self):
# pylint:disable=no-name-in-module
from zope.interface.declarations import implementedByFallback
return implementedByFallback
_getFallbackClass = _getTargetClass
def _callFUT(self, *args, **kw):
return self._getTargetClass()(*args, **kw)
def test_dictless_wo_existing_Implements_wo_registrations(self):
class Foo:
__slots__ = ('__implemented__',)
foo = Foo()
foo.__implemented__ = None
self.assertEqual(list(self._callFUT(foo)), [])
def test_dictless_wo_existing_Implements_cant_assign___implemented__(self):
class Foo:
def _get_impl(self):
raise NotImplementedError()
def _set_impl(self, val):
raise TypeError
__implemented__ = property(_get_impl, _set_impl)
def __call__(self):
# act like a factory
raise NotImplementedError()
foo = Foo()
self.assertRaises(TypeError, self._callFUT, foo)
def test_dictless_wo_existing_Implements_w_registrations(self):
from zope.interface import declarations
class Foo:
__slots__ = ('__implemented__',)
foo = Foo()
foo.__implemented__ = None
reg = object()
with _MonkeyDict(declarations,
'BuiltinImplementationSpecifications') as specs:
specs[foo] = reg
self.assertIs(self._callFUT(foo), reg)
def test_dictless_w_existing_Implements(self):
from zope.interface.declarations import Implements
impl = Implements()
class Foo:
__slots__ = ('__implemented__',)
foo = Foo()
foo.__implemented__ = impl
self.assertIs(self._callFUT(foo), impl)
def test_dictless_w_existing_not_Implements(self):
from zope.interface.interface import InterfaceClass
class Foo:
__slots__ = ('__implemented__',)
foo = Foo()
IFoo = InterfaceClass('IFoo')
foo.__implemented__ = (IFoo,)
self.assertEqual(list(self._callFUT(foo)), [IFoo])
def test_w_existing_attr_as_Implements(self):
from zope.interface.declarations import Implements
impl = Implements()
class Foo:
__implemented__ = impl
self.assertIs(self._callFUT(Foo), impl)
def test_builtins_added_to_cache(self):
from zope.interface import declarations
from zope.interface.declarations import Implements
with _MonkeyDict(declarations,
'BuiltinImplementationSpecifications') as specs:
self.assertEqual(list(self._callFUT(tuple)), [])
self.assertEqual(list(self._callFUT(list)), [])
self.assertEqual(list(self._callFUT(dict)), [])
for typ in (tuple, list, dict):
spec = specs[typ]
self.assertIsInstance(spec, Implements)
self.assertEqual(repr(spec),
'classImplements(%s)'
% (typ.__name__,))
def test_builtins_w_existing_cache(self):
from zope.interface import declarations
t_spec, l_spec, d_spec = object(), object(), object()
with _MonkeyDict(declarations,
'BuiltinImplementationSpecifications') as specs:
specs[tuple] = t_spec
specs[list] = l_spec
specs[dict] = d_spec
self.assertIs(self._callFUT(tuple), t_spec)
self.assertIs(self._callFUT(list), l_spec)
self.assertIs(self._callFUT(dict), d_spec)
def test_oldstyle_class_no_assertions(self):
# TODO: Figure out P3 story
class Foo:
pass
self.assertEqual(list(self._callFUT(Foo)), [])
def test_no_assertions(self):
# TODO: Figure out P3 story
class Foo:
pass
self.assertEqual(list(self._callFUT(Foo)), [])
def test_w_None_no_bases_not_factory(self):
class Foo:
__implemented__ = None
foo = Foo()
self.assertRaises(TypeError, self._callFUT, foo)
def test_w_None_no_bases_w_factory(self):
from zope.interface.declarations import objectSpecificationDescriptor
class Foo:
__implemented__ = None
def __call__(self):
raise NotImplementedError()
foo = Foo()
foo.__name__ = 'foo'
spec = self._callFUT(foo)
self.assertEqual(spec.__name__,
'interface.tests.test_declarations.foo')
self.assertIs(spec.inherit, foo)
self.assertIs(foo.__implemented__, spec)
self.assertIs(
foo.__providedBy__, objectSpecificationDescriptor
) # pylint:disable=no-member
self.assertNotIn('__provides__', foo.__dict__)
def test_w_None_no_bases_w_class(self):
from zope.interface.declarations import ClassProvides
class Foo:
__implemented__ = None
spec = self._callFUT(Foo)
self.assertEqual(spec.__name__,
'interface.tests.test_declarations.Foo')
self.assertIs(spec.inherit, Foo)
self.assertIs(Foo.__implemented__, spec)
self.assertIsInstance(
Foo.__providedBy__, ClassProvides
) # pylint:disable=no-member
self.assertIsInstance(
Foo.__provides__, ClassProvides
) # pylint:disable=no-member
self.assertEqual(
Foo.__provides__, Foo.__providedBy__
) # pylint:disable=no-member
def test_w_existing_Implements(self):
from zope.interface.declarations import Implements
impl = Implements()
class Foo:
__implemented__ = impl
self.assertIs(self._callFUT(Foo), impl)
def test_super_when_base_implements_interface(self):
from zope.interface import Interface
from zope.interface.declarations import implementer
class IBase(Interface):
pass
class IDerived(IBase):
pass
@implementer(IBase)
class Base:
pass
@implementer(IDerived)
class Derived(Base):
pass
self.assertEqual(list(self._callFUT(Derived)), [IDerived, IBase])
sup = super(Derived, Derived)
self.assertEqual(list(self._callFUT(sup)), [IBase])
def test_super_when_base_implements_interface_diamond(self):
from zope.interface import Interface
from zope.interface.declarations import implementer
class IBase(Interface):
pass
class IDerived(IBase):
pass
@implementer(IBase)
class Base:
pass
class Child1(Base):
pass
class Child2(Base):
pass
@implementer(IDerived)
class Derived(Child1, Child2):
pass
self.assertEqual(list(self._callFUT(Derived)), [IDerived, IBase])
sup = super(Derived, Derived)
self.assertEqual(list(self._callFUT(sup)), [IBase])
def test_super_when_parent_implements_interface_diamond(self):
from zope.interface import Interface
from zope.interface.declarations import implementer
class IBase(Interface):
pass
class IDerived(IBase):
pass
class Base:
pass
class Child1(Base):
pass
@implementer(IBase)
class Child2(Base):
pass
@implementer(IDerived)
class Derived(Child1, Child2):
pass
self.assertEqual(
Derived.__mro__, (Derived, Child1, Child2, Base, object)
)
self.assertEqual(list(self._callFUT(Derived)), [IDerived, IBase])
sup = super(Derived, Derived)
fut = self._callFUT(sup)
self.assertEqual(list(fut), [IBase])
self.assertIsNone(fut._dependents)
def test_super_when_base_doesnt_implement_interface(self):
from zope.interface import Interface
from zope.interface.declarations import implementer
class IBase(Interface):
pass
class IDerived(IBase):
pass
class Base:
pass
@implementer(IDerived)
class Derived(Base):
pass
self.assertEqual(list(self._callFUT(Derived)), [IDerived])
sup = super(Derived, Derived)
self.assertEqual(list(self._callFUT(sup)), [])
def test_super_when_base_is_object(self):
from zope.interface import Interface
from zope.interface.declarations import implementer
class IBase(Interface):
pass
class IDerived(IBase):
pass
@implementer(IDerived)
class Derived:
pass
self.assertEqual(list(self._callFUT(Derived)), [IDerived])
sup = super(Derived, Derived)
self.assertEqual(list(self._callFUT(sup)), [])
def test_super_multi_level_multi_inheritance(self):
from zope.interface import Interface
from zope.interface.declarations import implementer
class IBase(Interface):
pass
class IM1(Interface):
pass
class IM2(Interface):
pass
class IDerived(IBase):
pass
class IUnrelated(Interface):
pass
@implementer(IBase)
class Base:
pass
@implementer(IM1)
class M1(Base):
pass
@implementer(IM2)
class M2(Base):
pass
@implementer(IDerived, IUnrelated)
class Derived(M1, M2):
pass
d = Derived
sd = super(Derived, Derived)
sm1 = super(M1, Derived)
sm2 = super(M2, Derived)
self.assertEqual(list(self._callFUT(d)),
[IDerived, IUnrelated, IM1, IBase, IM2])
self.assertEqual(list(self._callFUT(sd)),
[IM1, IBase, IM2])
self.assertEqual(list(self._callFUT(sm1)),
[IM2, IBase])
self.assertEqual(list(self._callFUT(sm2)),
[IBase])
class Test_implementedBy(Test_implementedByFallback,
OptimizationTestMixin):
# Repeat tests for C optimizations
def _getTargetClass(self):
from zope.interface.declarations import implementedBy
return implementedBy
class _ImplementsTestMixin:
FUT_SETS_PROVIDED_BY = True
def _callFUT(self, cls, iface):
# Declare that *cls* implements *iface*; return *cls*
raise NotImplementedError
def _check_implementer(self, Foo,
orig_spec=None,
spec_name=__name__ + '.Foo',
inherit="not given"):
from zope.interface.declarations import ClassProvides
from zope.interface.interface import InterfaceClass
IFoo = InterfaceClass('IFoo')
returned = self._callFUT(Foo, IFoo)
self.assertIs(returned, Foo)
spec = Foo.__implemented__
if orig_spec is not None:
self.assertIs(spec, orig_spec)
self.assertEqual(spec.__name__,
spec_name)
inherit = Foo if inherit == "not given" else inherit
self.assertIs(spec.inherit, inherit)
self.assertIs(Foo.__implemented__, spec)
if self.FUT_SETS_PROVIDED_BY:
self.assertIsInstance(Foo.__providedBy__, ClassProvides)
self.assertIsInstance(Foo.__provides__, ClassProvides)
self.assertEqual(Foo.__provides__, Foo.__providedBy__)
return Foo, IFoo
def test_class(self):
class Foo:
pass
self._check_implementer(Foo)
class Test_classImplementsOnly(_ImplementsTestMixin, unittest.TestCase):
FUT_SETS_PROVIDED_BY = False
def _callFUT(self, cls, iface):
from zope.interface.declarations import classImplementsOnly
classImplementsOnly(cls, iface)
return cls
def test_w_existing_Implements(self):
from zope.interface.declarations import Implements
from zope.interface.interface import InterfaceClass
IFoo = InterfaceClass('IFoo')
IBar = InterfaceClass('IBar')
impl = Implements(IFoo)
impl.declared = (IFoo,)
class Foo:
__implemented__ = impl
impl.inherit = Foo
self._callFUT(Foo, IBar)
# Same spec, now different values
self.assertIs(Foo.__implemented__, impl)
self.assertEqual(impl.inherit, None)
self.assertEqual(impl.declared, (IBar,))
def test_class(self):
from zope.interface.declarations import Implements
from zope.interface.interface import InterfaceClass
IBar = InterfaceClass('IBar')
old_spec = Implements(IBar)
class Foo:
__implemented__ = old_spec
self._check_implementer(Foo, old_spec, '?', inherit=None)
def test_redundant_with_super_still_implements(self):
Base, IBase = self._check_implementer(
type('Foo', (object,), {}),
inherit=None,
)
class Child(Base):
pass
self._callFUT(Child, IBase)
self.assertTrue(IBase.implementedBy(Child))
class Test_classImplements(_ImplementsTestMixin, unittest.TestCase):
def _callFUT(self, cls, iface):
from zope.interface.declarations import classImplements
result = classImplements(