• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

zopefoundation / zope.interface / 344 / 10

Source File

95.99
/src/zope/interface/tests/test_declarations.py
1
##############################################################################
2
#
3
# Copyright (c) 2003 Zope Foundation and Contributors.
4
# All Rights Reserved.
5
#
6
# This software is subject to the provisions of the Zope Public License,
7
# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
8
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
9
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
10
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
11
# FOR A PARTICULAR PURPOSE.
12
#
13
##############################################################################
14
"""Test the new API for making and checking interface declarations
1✔
15
"""
16
import unittest
1✔
17

18
from zope.interface._compat import _skip_under_py3k
1✔
19

20

21
class _Py3ClassAdvice(object):
1✔
22

23
    def _run_generated_code(self, code, globs, locs,
1✔
24
                            fails_under_py3k=True,
25
                           ):
26
        import warnings
1✔
27
        from zope.interface._compat import PYTHON3
1✔
28
        with warnings.catch_warnings(record=True) as log:
1✔
29
            warnings.resetwarnings()
1✔
30
            if not PYTHON3:
1!
31
                exec(code, globs, locs)
×
32
                self.assertEqual(len(log), 0) # no longer warn
×
33
                return True
×
34
            else:
35
                try:
1✔
36
                    exec(code, globs, locs)
1✔
37
                except TypeError:
1✔
38
                    return False
1✔
39
                else:
40
                    if fails_under_py3k:
1✔
41
                        self.fail("Didn't raise TypeError")
42

43

44
class NamedTests(unittest.TestCase):
1✔
45

46
    def test_class(self):
1✔
47
        from zope.interface.declarations import named
1✔
48

49
        @named(u'foo')
1✔
50
        class Foo(object):
1✔
51
            pass
1✔
52

53
        self.assertEqual(Foo.__component_name__, u'foo')
1✔
54

55
    def test_function(self):
1✔
56
        from zope.interface.declarations import named
1✔
57

58
        @named(u'foo')
1✔
59
        def doFoo(o):
60
            raise NotImplementedError()
61

62
        self.assertEqual(doFoo.__component_name__, u'foo')
1✔
63

64
    def test_instance(self):
1✔
65
        from zope.interface.declarations import named
1✔
66

67
        class Foo(object):
1✔
68
            pass
1✔
69
        foo = Foo()
1✔
70
        named(u'foo')(foo)
1✔
71

72
        self.assertEqual(foo.__component_name__, u'foo')
1✔
73

74

75
class DeclarationTests(unittest.TestCase):
1✔
76

77
    def _getTargetClass(self):
1✔
78
        from zope.interface.declarations import Declaration
1✔
79
        return Declaration
1✔
80

81
    def _makeOne(self, *args, **kw):
1✔
82
        return self._getTargetClass()(*args, **kw)
1✔
83

84
    def test_ctor_no_bases(self):
1✔
85
        decl = self._makeOne()
1✔
86
        self.assertEqual(list(decl.__bases__), [])
1✔
87

88
    def test_ctor_w_interface_in_bases(self):
1✔
89
        from zope.interface.interface import InterfaceClass
1✔
90
        IFoo = InterfaceClass('IFoo')
1✔
91
        decl = self._makeOne(IFoo)
1✔
92
        self.assertEqual(list(decl.__bases__), [IFoo])
1✔
93

94
    def test_ctor_w_implements_in_bases(self):
1✔
95
        from zope.interface.declarations import Implements
1✔
96
        impl = Implements()
1✔
97
        decl = self._makeOne(impl)
1✔
98
        self.assertEqual(list(decl.__bases__), [impl])
1✔
99

100
    def test_changed_wo_existing__v_attrs(self):
1✔
101
        decl = self._makeOne()
1✔
102
        decl.changed(decl) # doesn't raise
1✔
103
        self.assertFalse('_v_attrs' in decl.__dict__)
1✔
104

105
    def test_changed_w_existing__v_attrs(self):
1✔
106
        decl = self._makeOne()
1✔
107
        decl._v_attrs = object()
1✔
108
        decl.changed(decl)
1✔
109
        self.assertFalse('_v_attrs' in decl.__dict__)
1✔
110

111
    def test___contains__w_self(self):
1✔
112
        from zope.interface.interface import InterfaceClass
1✔
113
        IFoo = InterfaceClass('IFoo')
1✔
114
        decl = self._makeOne()
1✔
115
        self.assertFalse(decl in decl)
1✔
116

117
    def test___contains__w_unrelated_iface(self):
1✔
118
        from zope.interface.interface import InterfaceClass
1✔
119
        IFoo = InterfaceClass('IFoo')
1✔
120
        decl = self._makeOne()
1✔
121
        self.assertFalse(IFoo in decl)
1✔
122

123
    def test___contains__w_base_interface(self):
1✔
124
        from zope.interface.interface import InterfaceClass
1✔
125
        IFoo = InterfaceClass('IFoo')
1✔
126
        decl = self._makeOne(IFoo)
1✔
127
        self.assertTrue(IFoo in decl)
1✔
128

129
    def test___iter___empty(self):
1✔
130
        decl = self._makeOne()
1✔
131
        self.assertEqual(list(decl), [])
1✔
132

133
    def test___iter___single_base(self):
1✔
134
        from zope.interface.interface import InterfaceClass
1✔
135
        IFoo = InterfaceClass('IFoo')
1✔
136
        decl = self._makeOne(IFoo)
1✔
137
        self.assertEqual(list(decl), [IFoo])
1✔
138

139
    def test___iter___multiple_bases(self):
1✔
140
        from zope.interface.interface import InterfaceClass
1✔
141
        IFoo = InterfaceClass('IFoo')
1✔
142
        IBar = InterfaceClass('IBar')
1✔
143
        decl = self._makeOne(IFoo, IBar)
1✔
144
        self.assertEqual(list(decl), [IFoo, IBar])
1✔
145

146
    def test___iter___inheritance(self):
1✔
147
        from zope.interface.interface import InterfaceClass
1✔
148
        IFoo = InterfaceClass('IFoo')
1✔
149
        IBar = InterfaceClass('IBar', (IFoo,))
1✔
150
        decl = self._makeOne(IBar)
1✔
151
        self.assertEqual(list(decl), [IBar]) #IBar.interfaces() omits bases
1✔
152

153
    def test___iter___w_nested_sequence_overlap(self):
1✔
154
        from zope.interface.interface import InterfaceClass
1✔
155
        IFoo = InterfaceClass('IFoo')
1✔
156
        IBar = InterfaceClass('IBar')
1✔
157
        decl = self._makeOne(IBar, (IFoo, IBar))
1✔
158
        self.assertEqual(list(decl), [IBar, IFoo])
1✔
159

160
    def test_flattened_empty(self):
1✔
161
        from zope.interface.interface import Interface
1✔
162
        decl = self._makeOne()
1✔
163
        self.assertEqual(list(decl.flattened()), [Interface])
1✔
164

165
    def test_flattened_single_base(self):
1✔
166
        from zope.interface.interface import Interface
1✔
167
        from zope.interface.interface import InterfaceClass
1✔
168
        IFoo = InterfaceClass('IFoo')
1✔
169
        decl = self._makeOne(IFoo)
1✔
170
        self.assertEqual(list(decl.flattened()), [IFoo, Interface])
1✔
171

172
    def test_flattened_multiple_bases(self):
1✔
173
        from zope.interface.interface import Interface
1✔
174
        from zope.interface.interface import InterfaceClass
1✔
175
        IFoo = InterfaceClass('IFoo')
1✔
176
        IBar = InterfaceClass('IBar')
1✔
177
        decl = self._makeOne(IFoo, IBar)
1✔
178
        self.assertEqual(list(decl.flattened()), [IFoo, IBar, Interface])
1✔
179

180
    def test_flattened_inheritance(self):
1✔
181
        from zope.interface.interface import Interface
1✔
182
        from zope.interface.interface import InterfaceClass
1✔
183
        IFoo = InterfaceClass('IFoo')
1✔
184
        IBar = InterfaceClass('IBar', (IFoo,))
1✔
185
        decl = self._makeOne(IBar)
1✔
186
        self.assertEqual(list(decl.flattened()), [IBar, IFoo, Interface])
1✔
187

188
    def test_flattened_w_nested_sequence_overlap(self):
1✔
189
        from zope.interface.interface import Interface
1✔
190
        from zope.interface.interface import InterfaceClass
1✔
191
        IFoo = InterfaceClass('IFoo')
1✔
192
        IBar = InterfaceClass('IBar')
1✔
193
        decl = self._makeOne(IBar, (IFoo, IBar))
1✔
194
        # Note that decl.__iro__ has IFoo first.
195
        self.assertEqual(list(decl.flattened()), [IFoo, IBar, Interface])
1✔
196

197
    def test___sub___unrelated_interface(self):
1✔
198
        from zope.interface.interface import InterfaceClass
1✔
199
        IFoo = InterfaceClass('IFoo')
1✔
200
        IBar = InterfaceClass('IBar')
1✔
201
        before = self._makeOne(IFoo)
1✔
202
        after = before - IBar
1✔
203
        self.assertTrue(isinstance(after, self._getTargetClass()))
1✔
204
        self.assertEqual(list(after), [IFoo])
1✔
205

206
    def test___sub___related_interface(self):
1✔
207
        from zope.interface.interface import InterfaceClass
1✔
208
        IFoo = InterfaceClass('IFoo')
1✔
209
        before = self._makeOne(IFoo)
1✔
210
        after = before - IFoo
1✔
211
        self.assertEqual(list(after), [])
1✔
212

213
    def test___sub___related_interface_by_inheritance(self):
1✔
214
        from zope.interface.interface import InterfaceClass
1✔
215
        IFoo = InterfaceClass('IFoo')
1✔
216
        IBar = InterfaceClass('IBar', (IFoo,))
1✔
217
        before = self._makeOne(IBar)
1✔
218
        after = before - IBar
1✔
219
        self.assertEqual(list(after), [])
1✔
220

221
    def test___add___unrelated_interface(self):
1✔
222
        from zope.interface.interface import InterfaceClass
1✔
223
        IFoo = InterfaceClass('IFoo')
1✔
224
        IBar = InterfaceClass('IBar')
1✔
225
        before = self._makeOne(IFoo)
1✔
226
        after = before + IBar
1✔
227
        self.assertTrue(isinstance(after, self._getTargetClass()))
1✔
228
        self.assertEqual(list(after), [IFoo, IBar])
1✔
229

230
    def test___add___related_interface(self):
1✔
231
        from zope.interface.interface import InterfaceClass
1✔
232
        IFoo = InterfaceClass('IFoo')
1✔
233
        IBar = InterfaceClass('IBar')
1✔
234
        IBaz = InterfaceClass('IBaz')
1✔
235
        before = self._makeOne(IFoo, IBar)
1✔
236
        other = self._makeOne(IBar, IBaz)
1✔
237
        after = before + other
1✔
238
        self.assertEqual(list(after), [IFoo, IBar, IBaz])
1✔
239

240

241
class TestImplements(unittest.TestCase):
1✔
242

243
    def _getTargetClass(self):
1✔
244
        from zope.interface.declarations import Implements
1✔
245
        return Implements
1✔
246

247
    def _makeOne(self, *args, **kw):
1✔
248
        return self._getTargetClass()(*args, **kw)
1✔
249

250
    def test_ctor_no_bases(self):
1✔
251
        impl = self._makeOne()
1✔
252
        self.assertEqual(impl.inherit, None)
1✔
253
        self.assertEqual(impl.declared, ())
1✔
254
        self.assertEqual(impl.__name__, '?')
1✔
255
        self.assertEqual(list(impl.__bases__), [])
1✔
256

257
    def test___repr__(self):
1✔
258
        impl = self._makeOne()
1✔
259
        impl.__name__ = 'Testing'
1✔
260
        self.assertEqual(repr(impl), '<implementedBy Testing>')
1✔
261

262
    def test___reduce__(self):
1✔
263
        from zope.interface.declarations import implementedBy
1✔
264
        impl = self._makeOne()
1✔
265
        self.assertEqual(impl.__reduce__(), (implementedBy, (None,)))
1✔
266

267
    def test_sort(self):
1✔
268
        from zope.interface.declarations import implementedBy
1✔
269
        class A(object):
1✔
270
            pass
1✔
271
        class B(object):
1✔
272
            pass
1✔
273
        from zope.interface.interface import InterfaceClass
1✔
274
        IFoo = InterfaceClass('IFoo')
1✔
275

276
        self.assertEqual(implementedBy(A), implementedBy(A))
1✔
277
        self.assertEqual(hash(implementedBy(A)), hash(implementedBy(A)))
1✔
278
        self.assertTrue(implementedBy(A) < None)
1✔
279
        self.assertTrue(None > implementedBy(A))
1✔
280
        self.assertTrue(implementedBy(A) < implementedBy(B))
1✔
281
        self.assertTrue(implementedBy(A) > IFoo)
1✔
282
        self.assertTrue(implementedBy(A) <= implementedBy(B))
1✔
283
        self.assertTrue(implementedBy(A) >= IFoo)
1✔
284
        self.assertTrue(implementedBy(A) != IFoo)
1✔
285

286
    def test_proxy_equality(self):
1✔
287
        # https://github.com/zopefoundation/zope.interface/issues/55
288
        class Proxy(object):
1✔
289
            def __init__(self, wrapped):
1✔
290
                self._wrapped = wrapped
1✔
291

292
            def __getattr__(self, name):
1✔
293
                raise NotImplementedError()
294

295
            def __eq__(self, other):
1✔
296
                return self._wrapped == other
1✔
297

298
            def __ne__(self, other):
1✔
299
                return self._wrapped != other
1✔
300

301
        from zope.interface.declarations import implementedBy
1✔
302
        class A(object):
1✔
303
            pass
1✔
304

305
        class B(object):
1✔
306
            pass
1✔
307

308
        implementedByA = implementedBy(A)
1✔
309
        implementedByB = implementedBy(B)
1✔
310
        proxy = Proxy(implementedByA)
1✔
311

312
        # The order of arguments to the operators matters,
313
        # test both
314
        self.assertTrue(implementedByA == implementedByA)
1✔
315
        self.assertTrue(implementedByA != implementedByB)
1✔
316
        self.assertTrue(implementedByB != implementedByA)
1✔
317

318
        self.assertTrue(proxy == implementedByA)
1✔
319
        self.assertTrue(implementedByA == proxy)
1✔
320
        self.assertFalse(proxy != implementedByA)
1✔
321
        self.assertFalse(implementedByA != proxy)
1✔
322

323
        self.assertTrue(proxy != implementedByB)
1✔
324
        self.assertTrue(implementedByB != proxy)
1✔
325

326

327
class Test_implementedByFallback(unittest.TestCase):
1✔
328

329
    def _callFUT(self, *args, **kw):
1✔
330
        from zope.interface.declarations import implementedByFallback
1✔
331
        return implementedByFallback(*args, **kw)
1✔
332

333
    def test_dictless_wo_existing_Implements_wo_registrations(self):
1✔
334
        class Foo(object):
1✔
335
            __slots__ = ('__implemented__',)
1✔
336
        foo = Foo()
1✔
337
        foo.__implemented__ = None
1✔
338
        self.assertEqual(list(self._callFUT(foo)), [])
1✔
339

340
    def test_dictless_wo_existing_Implements_cant_assign___implemented__(self):
1✔
341
        class Foo(object):
1✔
342
            def _get_impl(self):
1✔
343
                raise NotImplementedError()
344
            def _set_impl(self, val):
1✔
345
                raise TypeError
1✔
346
            __implemented__ = property(_get_impl, _set_impl)
1✔
347
            def __call__(self):
1✔
348
                # act like a factory
349
                raise NotImplementedError()
350
        foo = Foo()
1✔
351
        self.assertRaises(TypeError, self._callFUT, foo)
1✔
352

353
    def test_dictless_wo_existing_Implements_w_registrations(self):
1✔
354
        from zope.interface import declarations
1✔
355
        class Foo(object):
1✔
356
            __slots__ = ('__implemented__',)
1✔
357
        foo = Foo()
1✔
358
        foo.__implemented__ = None
1✔
359
        reg = object()
1✔
360
        with _MonkeyDict(declarations,
1✔
361
                         'BuiltinImplementationSpecifications') as specs:
362
            specs[foo] = reg
1✔
363
            self.assertTrue(self._callFUT(foo) is reg)
1✔
364

365
    def test_dictless_w_existing_Implements(self):
1✔
366
        from zope.interface.declarations import Implements
1✔
367
        impl = Implements()
1✔
368
        class Foo(object):
1✔
369
            __slots__ = ('__implemented__',)
1✔
370
        foo = Foo()
1✔
371
        foo.__implemented__ = impl
1✔
372
        self.assertTrue(self._callFUT(foo) is impl)
1✔
373

374
    def test_dictless_w_existing_not_Implements(self):
1✔
375
        from zope.interface.interface import InterfaceClass
1✔
376
        class Foo(object):
1✔
377
            __slots__ = ('__implemented__',)
1✔
378
        foo = Foo()
1✔
379
        IFoo = InterfaceClass('IFoo')
1✔
380
        foo.__implemented__ = (IFoo,)
1✔
381
        self.assertEqual(list(self._callFUT(foo)), [IFoo])
1✔
382

383
    def test_w_existing_attr_as_Implements(self):
1✔
384
        from zope.interface.declarations import Implements
1✔
385
        impl = Implements()
1✔
386
        class Foo(object):
1✔
387
            __implemented__ = impl
1✔
388
        self.assertTrue(self._callFUT(Foo) is impl)
1✔
389

390
    def test_builtins_added_to_cache(self):
1✔
391
        from zope.interface import declarations
1✔
392
        from zope.interface.declarations import Implements
1✔
393
        from zope.interface._compat import _BUILTINS
1✔
394
        with _MonkeyDict(declarations,
1✔
395
                         'BuiltinImplementationSpecifications') as specs:
396
            self.assertEqual(list(self._callFUT(tuple)), [])
1✔
397
            self.assertEqual(list(self._callFUT(list)), [])
1✔
398
            self.assertEqual(list(self._callFUT(dict)), [])
1✔
399
            for typ in (tuple, list, dict):
1✔
400
                spec = specs[typ]
1✔
401
                self.assertTrue(isinstance(spec, Implements))
1✔
402
                self.assertEqual(repr(spec),
1✔
403
                                '<implementedBy %s.%s>'
404
                                    % (_BUILTINS, typ.__name__))
405

406
    def test_builtins_w_existing_cache(self):
1✔
407
        from zope.interface import declarations
1✔
408
        t_spec, l_spec, d_spec = object(), object(), object()
1✔
409
        with _MonkeyDict(declarations,
1✔
410
                         'BuiltinImplementationSpecifications') as specs:
411
            specs[tuple] = t_spec
1✔
412
            specs[list] = l_spec
1✔
413
            specs[dict] = d_spec
1✔
414
            self.assertTrue(self._callFUT(tuple) is t_spec)
1✔
415
            self.assertTrue(self._callFUT(list) is l_spec)
1✔
416
            self.assertTrue(self._callFUT(dict) is d_spec)
1✔
417

418
    def test_oldstyle_class_no_assertions(self):
1✔
419
        # TODO: Figure out P3 story
420
        class Foo:
1✔
421
            pass
1✔
422
        self.assertEqual(list(self._callFUT(Foo)), [])
1✔
423

424
    def test_no_assertions(self):
1✔
425
        # TODO: Figure out P3 story
426
        class Foo(object):
1✔
427
            pass
1✔
428
        self.assertEqual(list(self._callFUT(Foo)), [])
1✔
429

430
    def test_w_None_no_bases_not_factory(self):
1✔
431
        class Foo(object):
1✔
432
            __implemented__ = None
1✔
433
        foo = Foo()
1✔
434
        self.assertRaises(TypeError, self._callFUT, foo)
1✔
435

436
    def test_w_None_no_bases_w_factory(self):
1✔
437
        from zope.interface.declarations import objectSpecificationDescriptor
1✔
438
        class Foo(object):
1✔
439
            __implemented__ = None
1✔
440
            def __call__(self):
1✔
441
                raise NotImplementedError()
442

443
        foo = Foo()
1✔
444
        foo.__name__ = 'foo'
1✔
445
        spec = self._callFUT(foo)
1✔
446
        self.assertEqual(spec.__name__,
1✔
447
                         'zope.interface.tests.test_declarations.foo')
448
        self.assertTrue(spec.inherit is foo)
1✔
449
        self.assertTrue(foo.__implemented__ is spec)
1✔
450
        self.assertTrue(foo.__providedBy__ is objectSpecificationDescriptor)
1✔
451
        self.assertFalse('__provides__' in foo.__dict__)
1✔
452

453
    def test_w_None_no_bases_w_class(self):
1✔
454
        from zope.interface.declarations import ClassProvides
1✔
455
        class Foo(object):
1✔
456
            __implemented__ = None
1✔
457
        spec = self._callFUT(Foo)
1✔
458
        self.assertEqual(spec.__name__,
1✔
459
                         'zope.interface.tests.test_declarations.Foo')
460
        self.assertTrue(spec.inherit is Foo)
1✔
461
        self.assertTrue(Foo.__implemented__ is spec)
1✔
462
        self.assertTrue(isinstance(Foo.__providedBy__, ClassProvides))
1✔
463
        self.assertTrue(isinstance(Foo.__provides__, ClassProvides))
1✔
464
        self.assertEqual(Foo.__provides__, Foo.__providedBy__)
1✔
465

466
    def test_w_existing_Implements(self):
1✔
467
        from zope.interface.declarations import Implements
1✔
468
        impl = Implements()
1✔
469
        class Foo(object):
1✔
470
            __implemented__ = impl
1✔
471
        self.assertTrue(self._callFUT(Foo) is impl)
1✔
472

473

474
class Test_implementedBy(Test_implementedByFallback):
1✔
475
    # Repeat tests for C optimizations
476

477
    def _callFUT(self, *args, **kw):
1✔
478
        from zope.interface.declarations import implementedBy
1✔
479
        return implementedBy(*args, **kw)
1✔
480

481
    def test_optimizations(self):
1✔
482
        from zope.interface.declarations import implementedByFallback
1✔
483
        from zope.interface.declarations import implementedBy
1✔
484
        try:
1✔
485
            import zope.interface._zope_interface_coptimizations
1✔
486
        except ImportError:
×
487
            self.assertIs(implementedBy, implementedByFallback)
×
488
        else:
489
            self.assertIsNot(implementedBy, implementedByFallback)
1✔
490

491

492
class Test_classImplementsOnly(unittest.TestCase):
1✔
493

494
    def _callFUT(self, *args, **kw):
1✔
495
        from zope.interface.declarations import classImplementsOnly
1✔
496
        return classImplementsOnly(*args, **kw)
1✔
497

498
    def test_no_existing(self):
1✔
499
        from zope.interface.declarations import ClassProvides
1✔
500
        from zope.interface.interface import InterfaceClass
1✔
501
        class Foo(object):
1✔
502
            pass
1✔
503
        ifoo = InterfaceClass('IFoo')
1✔
504
        self._callFUT(Foo, ifoo)
1✔
505
        spec = Foo.__implemented__
1✔
506
        self.assertEqual(spec.__name__,
1✔
507
                         'zope.interface.tests.test_declarations.Foo')
508
        self.assertTrue(spec.inherit is None)
1✔
509
        self.assertTrue(Foo.__implemented__ is spec)
1✔
510
        self.assertTrue(isinstance(Foo.__providedBy__, ClassProvides))
1✔
511
        self.assertTrue(isinstance(Foo.__provides__, ClassProvides))
1✔
512
        self.assertEqual(Foo.__provides__, Foo.__providedBy__)
1✔
513

514
    def test_w_existing_Implements(self):
1✔
515
        from zope.interface.declarations import Implements
1✔
516
        from zope.interface.interface import InterfaceClass
1✔
517
        IFoo = InterfaceClass('IFoo')
1✔
518
        IBar = InterfaceClass('IBar')
1✔
519
        impl = Implements(IFoo)
1✔
520
        impl.declared = (IFoo,)
1✔
521
        class Foo(object):
1✔
522
            __implemented__ = impl
1✔
523
        impl.inherit = Foo
1✔
524
        self._callFUT(Foo, IBar)
1✔
525
        # Same spec, now different values
526
        self.assertTrue(Foo.__implemented__ is impl)
1✔
527
        self.assertEqual(impl.inherit, None)
1✔
528
        self.assertEqual(impl.declared, (IBar,))
1✔
529

530

531
class Test_classImplements(unittest.TestCase):
1✔
532

533
    def _callFUT(self, *args, **kw):
1✔
534
        from zope.interface.declarations import classImplements
1✔
535
        return classImplements(*args, **kw)
1✔
536

537
    def test_no_existing(self):
1✔
538
        from zope.interface.declarations import ClassProvides
1✔
539
        from zope.interface.interface import InterfaceClass
1✔
540
        class Foo(object):
1✔
541
            pass
1✔
542
        IFoo = InterfaceClass('IFoo')
1✔
543
        self._callFUT(Foo, IFoo)
1✔
544
        spec = Foo.__implemented__
1✔
545
        self.assertEqual(spec.__name__,
1✔
546
                         'zope.interface.tests.test_declarations.Foo')
547
        self.assertTrue(spec.inherit is Foo)
1✔
548
        self.assertTrue(Foo.__implemented__ is spec)
1✔
549
        self.assertTrue(isinstance(Foo.__providedBy__, ClassProvides))
1✔
550
        self.assertTrue(isinstance(Foo.__provides__, ClassProvides))
1✔
551
        self.assertEqual(Foo.__provides__, Foo.__providedBy__)
1✔
552

553
    def test_w_existing_Implements(self):
1✔
554
        from zope.interface.declarations import Implements
1✔
555
        from zope.interface.interface import InterfaceClass
1✔
556
        IFoo = InterfaceClass('IFoo')
1✔
557
        IBar = InterfaceClass('IBar')
1✔
558
        impl = Implements(IFoo)
1✔
559
        impl.declared = (IFoo,)
1✔
560
        class Foo(object):
1✔
561
            __implemented__ = impl
1✔
562
        impl.inherit = Foo
1✔
563
        self._callFUT(Foo, IBar)
1✔
564
        # Same spec, now different values
565
        self.assertTrue(Foo.__implemented__ is impl)
1✔
566
        self.assertEqual(impl.inherit, Foo)
1✔
567
        self.assertEqual(impl.declared, (IFoo, IBar,))
1✔
568

569
    def test_w_existing_Implements_w_bases(self):
1✔
570
        from zope.interface.declarations import Implements
1✔
571
        from zope.interface.interface import InterfaceClass
1✔
572
        IFoo = InterfaceClass('IFoo')
1✔
573
        IBar = InterfaceClass('IBar')
1✔
574
        IBaz = InterfaceClass('IBaz', IFoo)
1✔
575
        b_impl = Implements(IBaz)
1✔
576
        impl = Implements(IFoo)
1✔
577
        impl.declared = (IFoo,)
1✔
578
        class Base1(object):
1✔
579
            __implemented__ = b_impl
1✔
580
        class Base2(object):
1✔
581
            __implemented__ = b_impl
1✔
582
        class Foo(Base1, Base2):
1✔
583
            __implemented__ = impl
1✔
584
        impl.inherit = Foo
1✔
585
        self._callFUT(Foo, IBar)
1✔
586
        # Same spec, now different values
587
        self.assertTrue(Foo.__implemented__ is impl)
1✔
588
        self.assertEqual(impl.inherit, Foo)
1✔
589
        self.assertEqual(impl.declared, (IFoo, IBar,))
1✔
590
        self.assertEqual(impl.__bases__, (IFoo, IBar, b_impl))
1✔
591

592

593
class Test__implements_advice(unittest.TestCase):
1✔
594

595
    def _callFUT(self, *args, **kw):
1✔
596
        from zope.interface.declarations import _implements_advice
1✔
597
        return _implements_advice(*args, **kw)
1✔
598

599
    def test_no_existing_implements(self):
1✔
600
        from zope.interface.declarations import classImplements
1✔
601
        from zope.interface.declarations import Implements
1✔
602
        from zope.interface.interface import InterfaceClass
1✔
603
        IFoo = InterfaceClass('IFoo')
1✔
604
        class Foo(object):
1✔
605
            __implements_advice_data__ = ((IFoo,), classImplements)
1✔
606
        self._callFUT(Foo)
1✔
607
        self.assertFalse('__implements_advice_data__' in Foo.__dict__)
1✔
608
        self.assertTrue(isinstance(Foo.__implemented__, Implements))
1✔
609
        self.assertEqual(list(Foo.__implemented__), [IFoo])
1✔
610

611

612
class Test_implementer(unittest.TestCase):
1✔
613

614
    def _getTargetClass(self):
1✔
615
        from zope.interface.declarations import implementer
1✔
616
        return implementer
1✔
617

618
    def _makeOne(self, *args, **kw):
1✔
619
        return self._getTargetClass()(*args, **kw)
1✔
620

621
    def test_oldstyle_class(self):
1✔
622
        # TODO Py3 story
623
        from zope.interface.declarations import ClassProvides
1✔
624
        from zope.interface.interface import InterfaceClass
1✔
625
        IFoo = InterfaceClass('IFoo')
1✔
626
        class Foo:
1✔
627
            pass
1✔
628
        decorator = self._makeOne(IFoo)
1✔
629
        returned = decorator(Foo)
1✔
630
        self.assertTrue(returned is Foo)
1✔
631
        spec = Foo.__implemented__
1✔
632
        self.assertEqual(spec.__name__,
1✔
633
                         'zope.interface.tests.test_declarations.Foo')
634
        self.assertTrue(spec.inherit is Foo)
1✔
635
        self.assertTrue(Foo.__implemented__ is spec)
1✔
636
        self.assertTrue(isinstance(Foo.__providedBy__, ClassProvides))
1✔
637
        self.assertTrue(isinstance(Foo.__provides__, ClassProvides))
1✔
638
        self.assertEqual(Foo.__provides__, Foo.__providedBy__)
1✔
639

640
    def test_newstyle_class(self):
1✔
641
        from zope.interface.declarations import ClassProvides
1✔
642
        from zope.interface.interface import InterfaceClass
1✔
643
        IFoo = InterfaceClass('IFoo')
1✔
644
        class Foo(object):
1✔
645
            pass
1✔
646
        decorator = self._makeOne(IFoo)
1✔
647
        returned = decorator(Foo)
1✔
648
        self.assertTrue(returned is Foo)
1✔
649
        spec = Foo.__implemented__
1✔
650
        self.assertEqual(spec.__name__,
1✔
651
                         'zope.interface.tests.test_declarations.Foo')
652
        self.assertTrue(spec.inherit is Foo)
1✔
653
        self.assertTrue(Foo.__implemented__ is spec)
1✔
654
        self.assertTrue(isinstance(Foo.__providedBy__, ClassProvides))
1✔
655
        self.assertTrue(isinstance(Foo.__provides__, ClassProvides))
1✔
656
        self.assertEqual(Foo.__provides__, Foo.__providedBy__)
1✔
657

658
    def test_nonclass_cannot_assign_attr(self):
1✔
659
        from zope.interface.interface import InterfaceClass
1✔
660
        IFoo = InterfaceClass('IFoo')
1✔
661
        decorator = self._makeOne(IFoo)
1✔
662
        self.assertRaises(TypeError, decorator, object())
1✔
663

664
    def test_nonclass_can_assign_attr(self):
1✔
665
        from zope.interface.interface import InterfaceClass
1✔
666
        IFoo = InterfaceClass('IFoo')
1✔
667
        class Foo(object):
1✔
668
            pass
1✔
669
        foo = Foo()
1✔
670
        decorator = self._makeOne(IFoo)
1✔
671
        returned = decorator(foo)
1✔
672
        self.assertTrue(returned is foo)
1✔
673
        spec = foo.__implemented__
1✔
674
        self.assertEqual(spec.__name__, 'zope.interface.tests.test_declarations.?')
1✔
675
        self.assertTrue(spec.inherit is None)
1✔
676
        self.assertTrue(foo.__implemented__ is spec)
1✔
677

678

679
class Test_implementer_only(unittest.TestCase):
1✔
680

681
    def _getTargetClass(self):
1✔
682
        from zope.interface.declarations import implementer_only
1✔
683
        return implementer_only
1✔
684

685
    def _makeOne(self, *args, **kw):
1✔
686
        return self._getTargetClass()(*args, **kw)
1✔
687

688
    def test_function(self):
1✔
689
        from zope.interface.interface import InterfaceClass
1✔
690
        IFoo = InterfaceClass('IFoo')
1✔
691
        decorator = self._makeOne(IFoo)
1✔
692
        def _function():
1✔
693
            raise NotImplementedError()
694
        self.assertRaises(ValueError, decorator, _function)
1✔
695

696
    def test_method(self):
1✔
697
        from zope.interface.interface import InterfaceClass
1✔
698
        IFoo = InterfaceClass('IFoo')
1✔
699
        decorator = self._makeOne(IFoo)
1✔
700
        class Bar:
1✔
701
            def _method():
1✔
702
                raise NotImplementedError()
703
        self.assertRaises(ValueError, decorator, Bar._method)
1✔
704

705
    def test_oldstyle_class(self):
1✔
706
        # TODO Py3 story
707
        from zope.interface.declarations import Implements
1✔
708
        from zope.interface.interface import InterfaceClass
1✔
709
        IFoo = InterfaceClass('IFoo')
1✔
710
        IBar = InterfaceClass('IBar')
1✔
711
        old_spec = Implements(IBar)
1✔
712
        class Foo:
1✔
713
            __implemented__ = old_spec
1✔
714
        decorator = self._makeOne(IFoo)
1✔
715
        returned = decorator(Foo)
1✔
716
        self.assertTrue(returned is Foo)
1✔
717
        spec = Foo.__implemented__
1✔
718
        self.assertEqual(spec.__name__, '?')
1✔
719
        self.assertTrue(spec.inherit is None)
1✔
720
        self.assertTrue(Foo.__implemented__ is spec)
1✔
721

722
    def test_newstyle_class(self):
1✔
723
        from zope.interface.declarations import Implements
1✔
724
        from zope.interface.interface import InterfaceClass
1✔
725
        IFoo = InterfaceClass('IFoo')
1✔
726
        IBar = InterfaceClass('IBar')
1✔
727
        old_spec = Implements(IBar)
1✔
728
        class Foo(object):
1✔
729
            __implemented__ = old_spec
1✔
730
        decorator = self._makeOne(IFoo)
1✔
731
        returned = decorator(Foo)
1✔
732
        self.assertTrue(returned is Foo)
1✔
733
        spec = Foo.__implemented__
1✔
734
        self.assertEqual(spec.__name__, '?')
1✔
735
        self.assertTrue(spec.inherit is None)
1✔
736
        self.assertTrue(Foo.__implemented__ is spec)
1✔
737

738

739
# Test '_implements' by way of 'implements{,Only}', its only callers.
740

741
class Test_implementsOnly(unittest.TestCase, _Py3ClassAdvice):
1✔
742

743
    def test_simple(self):
1✔
744
        import warnings
1✔
745
        from zope.interface.declarations import implementsOnly
1✔
746
        from zope.interface._compat import PYTHON3
1✔
747
        from zope.interface.interface import InterfaceClass
1✔
748
        IFoo = InterfaceClass("IFoo")
1✔
749
        globs = {'implementsOnly': implementsOnly,
1✔
750
                 'IFoo': IFoo,
751
                }
752
        locs = {}
1✔
753
        CODE = "\n".join([
1✔
754
            'class Foo(object):'
755
            '    implementsOnly(IFoo)',
756
            ])
757
        with warnings.catch_warnings(record=True) as log:
1✔
758
            warnings.resetwarnings()
1✔
759
            try:
1✔
760
                exec(CODE, globs, locs)
1✔
761
            except TypeError:
1✔
762
                self.assertTrue(PYTHON3, "Must be Python 3")
1✔
763
            else:
764
                if PYTHON3:
×
765
                    self.fail("Didn't raise TypeError")
766
                Foo = locs['Foo']
×
767
                spec = Foo.__implemented__
×
768
                self.assertEqual(list(spec), [IFoo])
×
769
                self.assertEqual(len(log), 0) # no longer warn
×
770

771
    def test_called_once_from_class_w_bases(self):
1✔
772
        from zope.interface.declarations import implements
1✔
773
        from zope.interface.declarations import implementsOnly
1✔
774
        from zope.interface.interface import InterfaceClass
1✔
775
        IFoo = InterfaceClass("IFoo")
1✔
776
        IBar = InterfaceClass("IBar")
1✔
777
        globs = {'implements': implements,
1✔
778
                 'implementsOnly': implementsOnly,
779
                 'IFoo': IFoo,
780
                 'IBar': IBar,
781
                }
782
        locs = {}
1✔
783
        CODE = "\n".join([
1✔
784
            'class Foo(object):',
785
            '    implements(IFoo)',
786
            'class Bar(Foo):'
787
            '    implementsOnly(IBar)',
788
            ])
789
        if self._run_generated_code(CODE, globs, locs):
1!
790
            Bar = locs['Bar']
×
791
            spec = Bar.__implemented__
×
792
            self.assertEqual(list(spec), [IBar])
×
793

794

795
class Test_implements(unittest.TestCase, _Py3ClassAdvice):
1✔
796

797
    def test_called_from_function(self):
1✔
798
        import warnings
1✔
799
        from zope.interface.declarations import implements
1✔
800
        from zope.interface.interface import InterfaceClass
1✔
801
        IFoo = InterfaceClass("IFoo")
1✔
802
        globs = {'implements': implements, 'IFoo': IFoo}
1✔
803
        locs = {}
1✔
804
        CODE = "\n".join([
1✔
805
            'def foo():',
806
            '    implements(IFoo)'
807
            ])
808
        if self._run_generated_code(CODE, globs, locs, False):
1!
809
            foo = locs['foo']
×
810
            with warnings.catch_warnings(record=True) as log:
×
811
                warnings.resetwarnings()
×
812
                self.assertRaises(TypeError, foo)
×
813
                self.assertEqual(len(log), 0) # no longer warn
×
814

815
    def test_called_twice_from_class(self):
1✔
816
        import warnings
1✔
817
        from zope.interface.declarations import implements
1✔
818
        from zope.interface.interface import InterfaceClass
1✔
819
        from zope.interface._compat import PYTHON3
1✔
820
        IFoo = InterfaceClass("IFoo")
1✔
821
        IBar = InterfaceClass("IBar")
1✔
822
        globs = {'implements': implements, 'IFoo': IFoo, 'IBar': IBar}
1✔
823
        locs = {}
1✔
824
        CODE = "\n".join([
1✔
825
            'class Foo(object):',
826
            '    implements(IFoo)',
827
            '    implements(IBar)',
828
            ])
829
        with warnings.catch_warnings(record=True) as log:
1✔
830
            warnings.resetwarnings()
1✔
831
            try:
1✔
832
                exec(CODE, globs, locs)
1✔
833
            except TypeError:
1✔
834
                if not PYTHON3:
1!
835
                    self.assertEqual(len(log), 0) # no longer warn
×
836
            else:
837
                self.fail("Didn't raise TypeError")
838

839
    def test_called_once_from_class(self):
1✔
840
        from zope.interface.declarations import implements
1✔
841
        from zope.interface.interface import InterfaceClass
1✔
842
        IFoo = InterfaceClass("IFoo")
1✔
843
        globs = {'implements': implements, 'IFoo': IFoo}
1✔
844
        locs = {}
1✔
845
        CODE = "\n".join([
1✔
846
            'class Foo(object):',
847
            '    implements(IFoo)',
848
            ])
849
        if self._run_generated_code(CODE, globs, locs):
1!
850
            Foo = locs['Foo']
×
851
            spec = Foo.__implemented__
×
852
            self.assertEqual(list(spec), [IFoo])
×
853

854

855
class ProvidesClassTests(unittest.TestCase):
1✔
856

857
    def _getTargetClass(self):
1✔
858
        from zope.interface.declarations import ProvidesClass
1✔
859
        return ProvidesClass
1✔
860

861
    def _makeOne(self, *args, **kw):
1✔
862
        return self._getTargetClass()(*args, **kw)
1✔
863

864
    def test_simple_class_one_interface(self):
1✔
865
        from zope.interface.interface import InterfaceClass
1✔
866
        IFoo = InterfaceClass("IFoo")
1✔
867
        class Foo(object):
1✔
868
            pass
1✔
869
        spec = self._makeOne(Foo, IFoo)
1✔
870
        self.assertEqual(list(spec), [IFoo])
1✔
871

872
    def test___reduce__(self):
1✔
873
        from zope.interface.declarations import Provides # the function
1✔
874
        from zope.interface.interface import InterfaceClass
1✔
875
        IFoo = InterfaceClass("IFoo")
1✔
876
        class Foo(object):
1✔
877
            pass
1✔
878
        spec = self._makeOne(Foo, IFoo)
1✔
879
        klass, args = spec.__reduce__()
1✔
880
        self.assertTrue(klass is Provides)
1✔
881
        self.assertEqual(args, (Foo, IFoo))
1✔
882

883
    def test___get___class(self):
1✔
884
        from zope.interface.interface import InterfaceClass
1✔
885
        IFoo = InterfaceClass("IFoo")
1✔
886
        class Foo(object):
1✔
887
            pass
1✔
888
        spec = self._makeOne(Foo, IFoo)
1✔
889
        Foo.__provides__ = spec
1✔
890
        self.assertTrue(Foo.__provides__ is spec)
1✔
891

892
    def test___get___instance(self):
1✔
893
        from zope.interface.interface import InterfaceClass
1✔
894
        IFoo = InterfaceClass("IFoo")
1✔
895
        class Foo(object):
1✔
896
            pass
1✔
897
        spec = self._makeOne(Foo, IFoo)
1✔
898
        Foo.__provides__ = spec
1✔
899
        def _test():
1✔
900
            foo = Foo()
1✔
901
            return foo.__provides__
1✔
902
        self.assertRaises(AttributeError, _test)
1✔
903

904

905
class Test_Provides(unittest.TestCase):
1✔
906

907
    def _callFUT(self, *args, **kw):
1✔
908
        from zope.interface.declarations import Provides
1✔
909
        return Provides(*args, **kw)
1✔
910

911
    def test_no_cached_spec(self):
1✔
912
        from zope.interface import declarations
1✔
913
        from zope.interface.interface import InterfaceClass
1✔
914
        IFoo = InterfaceClass("IFoo")
1✔
915
        cache = {}
1✔
916
        class Foo(object):
1✔
917
            pass
1✔
918
        with _Monkey(declarations, InstanceDeclarations=cache):
1✔
919
            spec = self._callFUT(Foo, IFoo)
1✔
920
        self.assertEqual(list(spec), [IFoo])
1✔
921
        self.assertTrue(cache[(Foo, IFoo)] is spec)
1✔
922

923
    def test_w_cached_spec(self):
1✔
924
        from zope.interface import declarations
1✔
925
        from zope.interface.interface import InterfaceClass
1✔
926
        IFoo = InterfaceClass("IFoo")
1✔
927
        prior = object()
1✔
928
        class Foo(object):
1✔
929
            pass
1✔
930
        cache = {(Foo, IFoo): prior}
1✔
931
        with _Monkey(declarations, InstanceDeclarations=cache):
1✔
932
            spec = self._callFUT(Foo, IFoo)
1✔
933
        self.assertTrue(spec is prior)
1✔
934

935

936
class Test_directlyProvides(unittest.TestCase):
1✔
937

938
    def _callFUT(self, *args, **kw):
1✔
939
        from zope.interface.declarations import directlyProvides
1✔
940
        return directlyProvides(*args, **kw)
1✔
941

942
    def test_w_normal_object(self):
1✔
943
        from zope.interface.declarations import ProvidesClass
1✔
944
        from zope.interface.interface import InterfaceClass
1✔
945
        IFoo = InterfaceClass("IFoo")
1✔
946
        class Foo(object):
1✔
947
            pass
1✔
948
        obj = Foo()
1✔
949
        self._callFUT(obj, IFoo)
1✔
950
        self.assertTrue(isinstance(obj.__provides__, ProvidesClass))
1✔
951
        self.assertEqual(list(obj.__provides__), [IFoo])
1✔
952

953
    def test_w_class(self):
1✔
954
        from zope.interface.declarations import ClassProvides
1✔
955
        from zope.interface.interface import InterfaceClass
1✔
956
        IFoo = InterfaceClass("IFoo")
1✔
957
        class Foo(object):
1✔
958
            pass
1✔
959
        self._callFUT(Foo, IFoo)
1✔
960
        self.assertTrue(isinstance(Foo.__provides__, ClassProvides))
1✔
961
        self.assertEqual(list(Foo.__provides__), [IFoo])
1✔
962

963
    @_skip_under_py3k
1✔
964
    def test_w_non_descriptor_aware_metaclass(self):
965
        # There are no non-descriptor-aware types in Py3k
966
        from zope.interface.interface import InterfaceClass
×
967
        IFoo = InterfaceClass("IFoo")
×
968
        class MetaClass(type):
×
969
            def __getattribute__(cls, name):
×
970
                # Emulate metaclass whose base is not the type object.
971
                if name == '__class__':
×
972
                    return cls
×
973
                # Under certain circumstances, the implementedByFallback
974
                # can get here for __dict__
975
                return type.__getattribute__(cls, name) # pragma: no cover
976

977
        class Foo(object):
×
978
            __metaclass__ = MetaClass
×
979
        obj = Foo()
×
980
        self.assertRaises(TypeError, self._callFUT, obj, IFoo)
×
981

982
    def test_w_classless_object(self):
1✔
983
        from zope.interface.declarations import ProvidesClass
1✔
984
        from zope.interface.interface import InterfaceClass
1✔
985
        IFoo = InterfaceClass("IFoo")
1✔
986
        the_dict = {}
1✔
987
        class Foo(object):
1✔
988
            def __getattribute__(self, name):
1✔
989
                # Emulate object w/o any class
990
                if name == '__class__':
1✔
991
                    return None
1✔
992
                raise NotImplementedError(name)
993
            def __setattr__(self, name, value):
1✔
994
                the_dict[name] = value
1✔
995
        obj = Foo()
1✔
996
        self._callFUT(obj, IFoo)
1✔
997
        self.assertTrue(isinstance(the_dict['__provides__'], ProvidesClass))
1✔
998
        self.assertEqual(list(the_dict['__provides__']), [IFoo])
1✔
999

1000

1001
class Test_alsoProvides(unittest.TestCase):
1✔
1002

1003
    def _callFUT(self, *args, **kw):
1✔
1004
        from zope.interface.declarations import alsoProvides
1✔
1005
        return alsoProvides(*args, **kw)
1✔
1006

1007
    def test_wo_existing_provides(self):
1✔
1008
        from zope.interface.declarations import ProvidesClass
1✔
1009
        from zope.interface.interface import InterfaceClass
1✔
1010
        IFoo = InterfaceClass("IFoo")
1✔
1011
        class Foo(object):
1✔
1012
            pass
1✔
1013
        obj = Foo()
1✔
1014
        self._callFUT(obj, IFoo)
1✔
1015
        self.assertTrue(isinstance(obj.__provides__, ProvidesClass))
1✔
1016
        self.assertEqual(list(obj.__provides__), [IFoo])
1✔
1017

1018
    def test_w_existing_provides(self):
1✔
1019
        from zope.interface.declarations import directlyProvides
1✔
1020
        from zope.interface.declarations import ProvidesClass
1✔
1021
        from zope.interface.interface import InterfaceClass
1✔
1022
        IFoo = InterfaceClass("IFoo")
1✔
1023
        IBar = InterfaceClass("IBar")
1✔
1024
        class Foo(object):
1✔
1025
            pass
1✔
1026
        obj = Foo()
1✔
1027
        directlyProvides(obj, IFoo)
1✔
1028
        self._callFUT(obj, IBar)
1✔
1029
        self.assertTrue(isinstance(obj.__provides__, ProvidesClass))
1✔
1030
        self.assertEqual(list(obj.__provides__), [IFoo, IBar])
1✔
1031

1032

1033
class Test_noLongerProvides(unittest.TestCase):
1✔
1034

1035
    def _callFUT(self, *args, **kw):
1✔
1036
        from zope.interface.declarations import noLongerProvides
1✔
1037
        return noLongerProvides(*args, **kw)
1✔
1038

1039
    def test_wo_existing_provides(self):
1✔
1040
        from zope.interface.interface import InterfaceClass
1✔
1041
        IFoo = InterfaceClass("IFoo")
1✔
1042
        class Foo(object):
1✔
1043
            pass
1✔
1044
        obj = Foo()
1✔
1045
        self._callFUT(obj, IFoo)
1✔
1046
        self.assertEqual(list(obj.__provides__), [])
1✔
1047

1048
    def test_w_existing_provides_hit(self):
1✔
1049
        from zope.interface.declarations import directlyProvides
1✔
1050
        from zope.interface.interface import InterfaceClass
1✔
1051
        IFoo = InterfaceClass("IFoo")
1✔
1052
        class Foo(object):
1✔
1053
            pass
1✔
1054
        obj = Foo()
1✔
1055
        directlyProvides(obj, IFoo)
1✔
1056
        self._callFUT(obj, IFoo)
1✔
1057
        self.assertEqual(list(obj.__provides__), [])
1✔
1058

1059
    def test_w_existing_provides_miss(self):
1✔
1060
        from zope.interface.declarations import directlyProvides
1✔
1061
        from zope.interface.interface import InterfaceClass
1✔
1062
        IFoo = InterfaceClass("IFoo")
1✔
1063
        IBar = InterfaceClass("IBar")
1✔
1064
        class Foo(object):
1✔
1065
            pass
1✔
1066
        obj = Foo()
1✔
1067
        directlyProvides(obj, IFoo)
1✔
1068
        self._callFUT(obj, IBar)
1✔
1069
        self.assertEqual(list(obj.__provides__), [IFoo])
1✔
1070

1071
    def test_w_iface_implemented_by_class(self):
1✔
1072
        from zope.interface.declarations import implementer
1✔
1073
        from zope.interface.interface import InterfaceClass
1✔
1074
        IFoo = InterfaceClass("IFoo")
1✔
1075
        @implementer(IFoo)
1✔
1076
        class Foo(object):
1✔
1077
            pass
1✔
1078
        obj = Foo()
1✔
1079
        self.assertRaises(ValueError, self._callFUT, obj, IFoo)
1✔
1080

1081

1082
class ClassProvidesBaseFallbackTests(unittest.TestCase):
1✔
1083

1084
    def _getTargetClass(self):
1✔
1085
        from zope.interface.declarations import ClassProvidesBaseFallback
1✔
1086
        return ClassProvidesBaseFallback
1✔
1087

1088
    def _makeOne(self, klass, implements):
1✔
1089
        # Don't instantiate directly:  the C version can't have attributes
1090
        # assigned.
1091
        class Derived(self._getTargetClass()):
1✔
1092
            def __init__(self, k, i):
1✔
1093
                self._cls = k
1✔
1094
                self._implements = i
1✔
1095
        return Derived(klass, implements)
1✔
1096

1097
    def test_w_same_class_via_class(self):
1✔
1098
        from zope.interface.interface import InterfaceClass
1✔
1099
        IFoo = InterfaceClass("IFoo")
1✔
1100
        class Foo(object):
1✔
1101
            pass
1✔
1102
        cpbp = Foo.__provides__ = self._makeOne(Foo, IFoo)
1✔
1103
        self.assertTrue(Foo.__provides__ is cpbp)
1✔
1104

1105
    def test_w_same_class_via_instance(self):
1✔
1106
        from zope.interface.interface import InterfaceClass
1✔
1107
        IFoo = InterfaceClass("IFoo")
1✔
1108
        class Foo(object):
1✔
1109
            pass
1✔
1110
        foo = Foo()
1✔
1111
        cpbp = Foo.__provides__ = self._makeOne(Foo, IFoo)
1✔
1112
        self.assertTrue(foo.__provides__ is IFoo)
1✔
1113

1114
    def test_w_different_class(self):
1✔
1115
        from zope.interface.interface import InterfaceClass
1✔
1116
        IFoo = InterfaceClass("IFoo")
1✔
1117
        class Foo(object):
1✔
1118
            pass
1✔
1119
        class Bar(Foo):
1✔
1120
            pass
1✔
1121
        bar = Bar()
1✔
1122
        cpbp = Foo.__provides__ = self._makeOne(Foo, IFoo)
1✔
1123
        self.assertRaises(AttributeError, getattr, Bar, '__provides__')
1✔
1124
        self.assertRaises(AttributeError, getattr, bar, '__provides__')
1✔
1125

1126

1127
class ClassProvidesBaseTests(ClassProvidesBaseFallbackTests):
1✔
1128
    # Repeat tests for C optimizations
1129

1130
    def _getTargetClass(self):
1✔
1131
        from zope.interface.declarations import ClassProvidesBase
1✔
1132
        return ClassProvidesBase
1✔
1133

1134
    def test_optimizations(self):
1✔
1135
        from zope.interface.declarations import ClassProvidesBaseFallback
1✔
1136
        try:
1✔
1137
            import zope.interface._zope_interface_coptimizations
1✔
1138
        except ImportError:
×
1139
            self.assertIs(self._getTargetClass(), ClassProvidesBaseFallback)
×
1140
        else:
1141
            self.assertIsNot(self._getTargetClass(), ClassProvidesBaseFallback)
1✔
1142

1143

1144
class ClassProvidesTests(unittest.TestCase):
1✔
1145

1146
    def _getTargetClass(self):
1✔
1147
        from zope.interface.declarations import ClassProvides
1✔
1148
        return ClassProvides
1✔
1149

1150
    def _makeOne(self, *args, **kw):
1✔
1151
        return self._getTargetClass()(*args, **kw)
1✔
1152

1153
    def test_w_simple_metaclass(self):
1✔
1154
        from zope.interface.declarations import implementer
1✔
1155
        from zope.interface.interface import InterfaceClass
1✔
1156
        IFoo = InterfaceClass("IFoo")
1✔
1157
        IBar = InterfaceClass("IBar")
1✔
1158
        @implementer(IFoo)
1✔
1159
        class Foo(object):
1✔
1160
            pass
1✔
1161
        cp = Foo.__provides__ = self._makeOne(Foo, type(Foo), IBar)
1✔
1162
        self.assertTrue(Foo.__provides__ is cp)
1✔
1163
        self.assertEqual(list(Foo().__provides__), [IFoo])
1✔
1164

1165
    def test___reduce__(self):
1✔
1166
        from zope.interface.declarations import implementer
1✔
1167
        from zope.interface.interface import InterfaceClass
1✔
1168
        IFoo = InterfaceClass("IFoo")
1✔
1169
        IBar = InterfaceClass("IBar")
1✔
1170
        @implementer(IFoo)
1✔
1171
        class Foo(object):
1✔
1172
            pass
1✔
1173
        cp = Foo.__provides__ = self._makeOne(Foo, type(Foo), IBar)
1✔
1174
        self.assertEqual(cp.__reduce__(),
1✔
1175
                         (self._getTargetClass(), (Foo, type(Foo), IBar)))
1176

1177

1178
class Test_directlyProvidedBy(unittest.TestCase):
1✔
1179

1180
    def _callFUT(self, *args, **kw):
1✔
1181
        from zope.interface.declarations import directlyProvidedBy
1✔
1182
        return directlyProvidedBy(*args, **kw)
1✔
1183

1184
    def test_wo_declarations_in_class_or_instance(self):
1✔
1185
        class Foo(object):
1✔
1186
            pass
1✔
1187
        foo = Foo()
1✔
1188
        self.assertEqual(list(self._callFUT(foo)), [])
1✔
1189

1190
    def test_w_declarations_in_class_but_not_instance(self):
1✔
1191
        from zope.interface.declarations import implementer
1✔
1192
        from zope.interface.interface import InterfaceClass
1✔
1193
        IFoo = InterfaceClass("IFoo")
1✔
1194
        @implementer(IFoo)
1✔
1195
        class Foo(object):
1✔
1196
            pass
1✔
1197
        foo = Foo()
1✔
1198
        self.assertEqual(list(self._callFUT(foo)), [])
1✔
1199

1200
    def test_w_declarations_in_instance_but_not_class(self):
1✔
1201
        from zope.interface.declarations import directlyProvides
1✔
1202
        from zope.interface.interface import InterfaceClass
1✔
1203
        IFoo = InterfaceClass("IFoo")
1✔
1204
        class Foo(object):
1✔
1205
            pass
1✔
1206
        foo = Foo()
1✔
1207
        directlyProvides(foo, IFoo)
1✔
1208
        self.assertEqual(list(self._callFUT(foo)), [IFoo])
1✔
1209

1210
    def test_w_declarations_in_instance_and_class(self):
1✔
1211
        from zope.interface.declarations import directlyProvides
1✔
1212
        from zope.interface.declarations import implementer
1✔
1213
        from zope.interface.interface import InterfaceClass
1✔
1214
        IFoo = InterfaceClass("IFoo")
1✔
1215
        IBar = InterfaceClass("IBar")
1✔
1216
        @implementer(IFoo)
1✔
1217
        class Foo(object):
1✔
1218
            pass
1✔
1219
        foo = Foo()
1✔
1220
        directlyProvides(foo, IBar)
1✔
1221
        self.assertEqual(list(self._callFUT(foo)), [IBar])
1✔
1222

1223

1224
class Test_classProvides(unittest.TestCase, _Py3ClassAdvice):
1✔
1225

1226
    def test_called_from_function(self):
1✔
1227
        import warnings
1✔
1228
        from zope.interface.declarations import classProvides
1✔
1229
        from zope.interface.interface import InterfaceClass
1✔
1230
        from zope.interface._compat import PYTHON3
1✔
1231
        IFoo = InterfaceClass("IFoo")
1✔
1232
        globs = {'classProvides': classProvides, 'IFoo': IFoo}
1✔
1233
        locs = {}
1✔
1234
        CODE = "\n".join([
1✔
1235
            'def foo():',
1236
            '    classProvides(IFoo)'
1237
            ])
1238
        exec(CODE, globs, locs)
1✔
1239
        foo = locs['foo']
1✔
1240
        with warnings.catch_warnings(record=True) as log:
1✔
1241
            warnings.resetwarnings()
1✔
1242
            self.assertRaises(TypeError, foo)
1✔
1243
            if not PYTHON3:
1!
1244
                self.assertEqual(len(log), 0) # no longer warn
×
1245

1246
    def test_called_twice_from_class(self):
1✔
1247
        import warnings
1✔
1248
        from zope.interface.declarations import classProvides
1✔
1249
        from zope.interface.interface import InterfaceClass
1✔
1250
        from zope.interface._compat import PYTHON3
1✔
1251
        IFoo = InterfaceClass("IFoo")
1✔
1252
        IBar = InterfaceClass("IBar")
1✔
1253
        globs = {'classProvides': classProvides, 'IFoo': IFoo, 'IBar': IBar}
1✔
1254
        locs = {}
1✔
1255
        CODE = "\n".join([
1✔
1256
            'class Foo(object):',
1257
            '    classProvides(IFoo)',
1258
            '    classProvides(IBar)',
1259
            ])
1260
        with warnings.catch_warnings(record=True) as log:
1✔
1261
            warnings.resetwarnings()
1✔
1262
            try:
1✔
1263
                exec(CODE, globs, locs)
1✔
1264
            except TypeError:
1✔
1265
                if not PYTHON3:
1!
1266
                    self.assertEqual(len(log), 0) # no longer warn
×
1267
            else:
1268
                self.fail("Didn't raise TypeError")
1269

1270
    def test_called_once_from_class(self):
1✔
1271
        from zope.interface.declarations import classProvides
1✔
1272
        from zope.interface.interface import InterfaceClass
1✔
1273
        IFoo = InterfaceClass("IFoo")
1✔
1274
        globs = {'classProvides': classProvides, 'IFoo': IFoo}
1✔
1275
        locs = {}
1✔
1276
        CODE = "\n".join([
1✔
1277
            'class Foo(object):',
1278
            '    classProvides(IFoo)',
1279
            ])
1280
        if self._run_generated_code(CODE, globs, locs):
1!
1281
            Foo = locs['Foo']
×
1282
            spec = Foo.__providedBy__
×
1283
            self.assertEqual(list(spec), [IFoo])
×
1284

1285
# Test _classProvides_advice through classProvides, its only caller.
1286

1287

1288
class Test_provider(unittest.TestCase):
1✔
1289

1290
    def _getTargetClass(self):
1✔
1291
        from zope.interface.declarations import provider
1✔
1292
        return provider
1✔
1293

1294
    def _makeOne(self, *args, **kw):
1✔
1295
        return self._getTargetClass()(*args, **kw)
1✔
1296

1297
    def test_w_class(self):
1✔
1298
        from zope.interface.declarations import ClassProvides
1✔
1299
        from zope.interface.interface import InterfaceClass
1✔
1300
        IFoo = InterfaceClass("IFoo")
1✔
1301
        @self._makeOne(IFoo)
1✔
1302
        class Foo(object):
1✔
1303
            pass
1✔
1304
        self.assertTrue(isinstance(Foo.__provides__, ClassProvides))
1✔
1305
        self.assertEqual(list(Foo.__provides__), [IFoo])
1✔
1306

1307

1308
class Test_moduleProvides(unittest.TestCase):
1✔
1309

1310
    def test_called_from_function(self):
1✔
1311
        from zope.interface.declarations import moduleProvides
1✔
1312
        from zope.interface.interface import InterfaceClass
1✔
1313
        IFoo = InterfaceClass("IFoo")
1✔
1314
        globs = {'__name__': 'zope.interface.tests.foo',
1✔
1315
                 'moduleProvides': moduleProvides, 'IFoo': IFoo}
1316
        locs = {}
1✔
1317
        CODE = "\n".join([
1✔
1318
            'def foo():',
1319
            '    moduleProvides(IFoo)'
1320
            ])
1321
        exec(CODE, globs, locs)
1✔
1322
        foo = locs['foo']
1✔
1323
        self.assertRaises(TypeError, foo)
1✔
1324

1325
    def test_called_from_class(self):
1✔
1326
        from zope.interface.declarations import moduleProvides
1✔
1327
        from zope.interface.interface import InterfaceClass
1✔
1328
        IFoo = InterfaceClass("IFoo")
1✔
1329
        globs = {'__name__': 'zope.interface.tests.foo',
1✔
1330
                 'moduleProvides': moduleProvides, 'IFoo': IFoo}
1331
        locs = {}
1✔
1332
        CODE = "\n".join([
1✔
1333
            'class Foo(object):',
1334
            '    moduleProvides(IFoo)',
1335
            ])
1336
        with self.assertRaises(TypeError):
1✔
1337
            exec(CODE, globs, locs)
1✔
1338

1339
    def test_called_once_from_module_scope(self):
1✔
1340
        from zope.interface.declarations import moduleProvides
1✔
1341
        from zope.interface.interface import InterfaceClass
1✔
1342
        IFoo = InterfaceClass("IFoo")
1✔
1343
        globs = {'__name__': 'zope.interface.tests.foo',
1✔
1344
                 'moduleProvides': moduleProvides, 'IFoo': IFoo}
1345
        CODE = "\n".join([
1✔
1346
            'moduleProvides(IFoo)',
1347
            ])
1348
        exec(CODE, globs)
1✔
1349
        spec = globs['__provides__']
1✔
1350
        self.assertEqual(list(spec), [IFoo])
1✔
1351

1352
    def test_called_twice_from_module_scope(self):
1✔
1353
        from zope.interface.declarations import moduleProvides
1✔
1354
        from zope.interface.interface import InterfaceClass
1✔
1355
        IFoo = InterfaceClass("IFoo")
1✔
1356
        globs = {'__name__': 'zope.interface.tests.foo',
1✔
1357
                 'moduleProvides': moduleProvides, 'IFoo': IFoo}
1358
        locs = {}
1✔
1359
        CODE = "\n".join([
1✔
1360
            'moduleProvides(IFoo)',
1361
            'moduleProvides(IFoo)',
1362
            ])
1363
        with self.assertRaises(TypeError):
1✔
1364
            exec(CODE, globs)
1✔
1365

1366

1367
class Test_getObjectSpecificationFallback(unittest.TestCase):
1✔
1368

1369
    def _callFUT(self, *args, **kw):
1✔
1370
        from zope.interface.declarations import getObjectSpecificationFallback
1✔
1371
        return getObjectSpecificationFallback(*args, **kw)
1✔
1372

1373
    def test_wo_existing_provides_classless(self):
1✔
1374
        the_dict = {}
1✔
1375
        class Foo(object):
1✔
1376
            def __getattribute__(self, name):
1✔
1377
                # Emulate object w/o any class
1378
                if name == '__class__':
1✔
1379
                    raise AttributeError(name)
1✔
1380
                try:
1✔
1381
                    return the_dict[name]
1✔
1382
                except KeyError:
1✔
1383
                    raise AttributeError(name)
1✔
1384
            def __setattr__(self, name, value):
1✔
1385
                raise NotImplementedError()
1386
        foo = Foo()
1✔
1387
        spec = self._callFUT(foo)
1✔
1388
        self.assertEqual(list(spec), [])
1✔
1389

1390
    def test_existing_provides_is_spec(self):
1✔
1391
        from zope.interface.declarations import directlyProvides
1✔
1392
        from zope.interface.interface import InterfaceClass
1✔
1393
        IFoo = InterfaceClass("IFoo")
1✔
1394
        def foo():
1✔
1395
            raise NotImplementedError()
1396
        directlyProvides(foo, IFoo)
1✔
1397
        spec = self._callFUT(foo)
1✔
1398
        self.assertTrue(spec is foo.__provides__)
1✔
1399

1400
    def test_existing_provides_is_not_spec(self):
1✔
1401
        def foo():
1✔
1402
            raise NotImplementedError()
1403
        foo.__provides__ = object() # not a valid spec
1✔
1404
        spec = self._callFUT(foo)
1✔
1405
        self.assertEqual(list(spec), [])
1✔
1406

1407
    def test_existing_provides(self):
1✔
1408
        from zope.interface.declarations import directlyProvides
1✔
1409
        from zope.interface.interface import InterfaceClass
1✔
1410
        IFoo = InterfaceClass("IFoo")
1✔
1411
        class Foo(object):
1✔
1412
            pass
1✔
1413
        foo = Foo()
1✔
1414
        directlyProvides(foo, IFoo)
1✔
1415
        spec = self._callFUT(foo)
1✔
1416
        self.assertEqual(list(spec), [IFoo])
1✔
1417

1418
    def test_wo_provides_on_class_w_implements(self):
1✔
1419
        from zope.interface.declarations import implementer
1✔
1420
        from zope.interface.interface import InterfaceClass
1✔
1421
        IFoo = InterfaceClass("IFoo")
1✔
1422
        @implementer(IFoo)
1✔
1423
        class Foo(object):
1✔
1424
            pass
1✔
1425
        foo = Foo()
1✔
1426
        spec = self._callFUT(foo)
1✔
1427
        self.assertEqual(list(spec), [IFoo])
1✔
1428

1429
    def test_wo_provides_on_class_wo_implements(self):
1✔
1430
        class Foo(object):
1✔
1431
            pass
1✔
1432
        foo = Foo()
1✔
1433
        spec = self._callFUT(foo)
1✔
1434
        self.assertEqual(list(spec), [])
1✔
1435

1436

1437
class Test_getObjectSpecification(Test_getObjectSpecificationFallback):
1✔
1438
    # Repeat tests for C optimizations
1439

1440
    def _callFUT(self, *args, **kw):
1✔
1441
        from zope.interface.declarations import getObjectSpecification
1✔
1442
        return getObjectSpecification(*args, **kw)
1✔
1443

1444
    def test_optimizations(self):
1✔
1445
        from zope.interface.declarations import getObjectSpecificationFallback
1✔
1446
        from zope.interface.declarations import getObjectSpecification
1✔
1447
        try:
1✔
1448
            import zope.interface._zope_interface_coptimizations
1✔
1449
        except ImportError:
×
1450
            self.assertIs(getObjectSpecification,
×
1451
                          getObjectSpecificationFallback)
1452
        else:
1453
            self.assertIsNot(getObjectSpecification,
1✔
1454
                             getObjectSpecificationFallback)
1455

1456

1457
class Test_providedByFallback(unittest.TestCase):
1✔
1458

1459
    def _callFUT(self, *args, **kw):
1✔
1460
        from zope.interface.declarations import providedByFallback
1✔
1461
        return providedByFallback(*args, **kw)
1✔
1462

1463
    def test_wo_providedBy_on_class_wo_implements(self):
1✔
1464
        class Foo(object):
1✔
1465
            pass
1✔
1466
        foo = Foo()
1✔
1467
        spec = self._callFUT(foo)
1✔
1468
        self.assertEqual(list(spec), [])
1✔
1469

1470
    def test_w_providedBy_valid_spec(self):
1✔
1471
        from zope.interface.declarations import Provides
1✔
1472
        from zope.interface.interface import InterfaceClass
1✔
1473
        IFoo = InterfaceClass("IFoo")
1✔
1474
        class Foo(object):
1✔
1475
            pass
1✔
1476
        foo = Foo()
1✔
1477
        foo.__providedBy__ = Provides(Foo, IFoo)
1✔
1478
        spec = self._callFUT(foo)
1✔
1479
        self.assertEqual(list(spec), [IFoo])
1✔
1480

1481
    def test_w_providedBy_invalid_spec(self):
1✔
1482
        class Foo(object):
1✔
1483
            pass
1✔
1484
        foo = Foo()
1✔
1485
        foo.__providedBy__ = object()
1✔
1486
        spec = self._callFUT(foo)
1✔
1487
        self.assertEqual(list(spec), [])
1✔
1488

1489
    def test_w_providedBy_invalid_spec_class_w_implements(self):
1✔
1490
        from zope.interface.declarations import implementer
1✔
1491
        from zope.interface.interface import InterfaceClass
1✔
1492
        IFoo = InterfaceClass("IFoo")
1✔
1493
        @implementer(IFoo)
1✔
1494
        class Foo(object):
1✔
1495
            pass
1✔
1496
        foo = Foo()
1✔
1497
        foo.__providedBy__ = object()
1✔
1498
        spec = self._callFUT(foo)
1✔
1499
        self.assertEqual(list(spec), [IFoo])
1✔
1500

1501
    def test_w_providedBy_invalid_spec_w_provides_no_provides_on_class(self):
1✔
1502
        class Foo(object):
1✔
1503
            pass
1✔
1504
        foo = Foo()
1✔
1505
        foo.__providedBy__ = object()
1✔
1506
        expected = foo.__provides__ = object()
1✔
1507
        spec = self._callFUT(foo)
1✔
1508
        self.assertTrue(spec is expected)
1✔
1509

1510
    def test_w_providedBy_invalid_spec_w_provides_diff_provides_on_class(self):
1✔
1511
        class Foo(object):
1✔
1512
            pass
1✔
1513
        foo = Foo()
1✔
1514
        foo.__providedBy__ = object()
1✔
1515
        expected = foo.__provides__ = object()
1✔
1516
        Foo.__provides__ = object()
1✔
1517
        spec = self._callFUT(foo)
1✔
1518
        self.assertTrue(spec is expected)
1✔
1519

1520
    def test_w_providedBy_invalid_spec_w_provides_same_provides_on_class(self):
1✔
1521
        from zope.interface.declarations import implementer
1✔
1522
        from zope.interface.interface import InterfaceClass
1✔
1523
        IFoo = InterfaceClass("IFoo")
1✔
1524
        @implementer(IFoo)
1✔
1525
        class Foo(object):
1✔
1526
            pass
1✔
1527
        foo = Foo()
1✔
1528
        foo.__providedBy__ = object()
1✔
1529
        foo.__provides__ = Foo.__provides__ = object()
1✔
1530
        spec = self._callFUT(foo)
1✔
1531
        self.assertEqual(list(spec), [IFoo])
1✔
1532

1533

1534
class Test_providedBy(Test_providedByFallback):
1✔
1535
    # Repeat tests for C optimizations
1536

1537
    def _callFUT(self, *args, **kw):
1✔
1538
        from zope.interface.declarations import providedBy
1✔
1539
        return providedBy(*args, **kw)
1✔
1540

1541
    def test_optimizations(self):
1✔
1542
        from zope.interface.declarations import providedByFallback
1✔
1543
        from zope.interface.declarations import providedBy
1✔
1544
        try:
1✔
1545
            import zope.interface._zope_interface_coptimizations
1✔
1546
        except ImportError:
×
1547
            self.assertIs(providedBy, providedByFallback)
×
1548
        else:
1549
            self.assertIsNot(providedBy, providedByFallback)
1✔
1550

1551

1552
class ObjectSpecificationDescriptorFallbackTests(unittest.TestCase):
1✔
1553

1554
    def _getTargetClass(self):
1✔
1555
        from zope.interface.declarations \
1✔
1556
            import ObjectSpecificationDescriptorFallback
1557
        return ObjectSpecificationDescriptorFallback
1✔
1558

1559
    def _makeOne(self, *args, **kw):
1✔
1560
        return self._getTargetClass()(*args, **kw)
1✔
1561

1562
    def test_accessed_via_class(self):
1✔
1563
        from zope.interface.declarations import Provides
1✔
1564
        from zope.interface.interface import InterfaceClass
1✔
1565
        IFoo = InterfaceClass("IFoo")
1✔
1566
        class Foo(object):
1✔
1567
            pass
1✔
1568
        Foo.__provides__ = Provides(Foo, IFoo)
1✔
1569
        Foo.__providedBy__ = self._makeOne()
1✔
1570
        self.assertEqual(list(Foo.__providedBy__), [IFoo])
1✔
1571

1572
    def test_accessed_via_inst_wo_provides(self):
1✔
1573
        from zope.interface.declarations import implementer
1✔
1574
        from zope.interface.declarations import Provides
1✔
1575
        from zope.interface.interface import InterfaceClass
1✔
1576
        IFoo = InterfaceClass("IFoo")
1✔
1577
        IBar = InterfaceClass("IBar")
1✔
1578
        @implementer(IFoo)
1✔
1579
        class Foo(object):
1✔
1580
            pass
1✔
1581
        Foo.__provides__ = Provides(Foo, IBar)
1✔
1582
        Foo.__providedBy__ = self._makeOne()
1✔
1583
        foo = Foo()
1✔
1584
        self.assertEqual(list(foo.__providedBy__), [IFoo])
1✔
1585

1586
    def test_accessed_via_inst_w_provides(self):
1✔
1587
        from zope.interface.declarations import directlyProvides
1✔
1588
        from zope.interface.declarations import implementer
1✔
1589
        from zope.interface.declarations import Provides
1✔
1590
        from zope.interface.interface import InterfaceClass
1✔
1591
        IFoo = InterfaceClass("IFoo")
1✔
1592
        IBar = InterfaceClass("IBar")
1✔
1593
        IBaz = InterfaceClass("IBaz")
1✔
1594
        @implementer(IFoo)
1✔
1595
        class Foo(object):
1✔
1596
            pass
1✔
1597
        Foo.__provides__ = Provides(Foo, IBar)
1✔
1598
        Foo.__providedBy__ = self._makeOne()
1✔
1599
        foo = Foo()
1✔
1600
        directlyProvides(foo, IBaz)
1✔
1601
        self.assertEqual(list(foo.__providedBy__), [IBaz, IFoo])
1✔
1602

1603

1604
class ObjectSpecificationDescriptorTests(
1✔
1605
                ObjectSpecificationDescriptorFallbackTests):
1606
    # Repeat tests for C optimizations
1607

1608
    def _getTargetClass(self):
1✔
1609
        from zope.interface.declarations import ObjectSpecificationDescriptor
1✔
1610
        return ObjectSpecificationDescriptor
1✔
1611

1612
    def test_optimizations(self):
1✔
1613
        from zope.interface.declarations import (
1✔
1614
            ObjectSpecificationDescriptorFallback)
1615
        try:
1✔
1616
            import zope.interface._zope_interface_coptimizations
1✔
1617
        except ImportError:
×
1618
            self.assertIs(self._getTargetClass(),
×
1619
                          ObjectSpecificationDescriptorFallback)
1620
        else:
1621
            self.assertIsNot(self._getTargetClass(),
1✔
1622
                             ObjectSpecificationDescriptorFallback)
1623

1624

1625
# Test _normalizeargs through its callers.
1626

1627

1628
class _Monkey(object):
1✔
1629
    # context-manager for replacing module names in the scope of a test.
1630
    def __init__(self, module, **kw):
1✔
1631
        self.module = module
1✔
1632
        self.to_restore = dict([(key, getattr(module, key)) for key in kw])
1✔
1633
        for key, value in kw.items():
1✔
1634
            setattr(module, key, value)
1✔
1635

1636
    def __enter__(self):
1✔
1637
        return self
1✔
1638

1639
    def __exit__(self, exc_type, exc_val, exc_tb):
1✔
1640
        for key, value in self.to_restore.items():
1✔
1641
            setattr(self.module, key, value)
1✔
1642

1643

1644
class _MonkeyDict(object):
1✔
1645
    # context-manager for restoring a dict w/in a module in the scope of a test.
1646
    def __init__(self, module, attrname, **kw):
1✔
1647
        self.module = module
1✔
1648
        self.target = getattr(module, attrname)
1✔
1649
        self.to_restore = self.target.copy()
1✔
1650
        self.target.clear()
1✔
1651
        self.target.update(kw)
1✔
1652

1653
    def __enter__(self):
1✔
1654
        return self.target
1✔
1655

1656
    def __exit__(self, exc_type, exc_val, exc_tb):
1✔
1657
        self.target.clear()
1✔
1658
        self.target.update(self.to_restore)
1✔
  • Back to Build 338
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc