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

zopefoundation / zope.component / 4696882551

pending completion
4696882551

push

github

Michael Howitz
Preparing release 6.0

460 of 475 branches covered (96.84%)

Branch coverage included in aggregate %.

4646 of 4646 relevant lines covered (100.0%)

1.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

99.82
/src/zope/component/tests/test__api.py
1
##############################################################################
2
#
3
# Copyright (c) 2012 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
""" Tests for z.c._api
1✔
15
"""
16
import unittest
1✔
17

18
from zope.component.tests import fails_if_called
1✔
19

20

21
class Test_getSiteManager(unittest.TestCase):
1✔
22

23
    from zope.component.testing import setUp
1✔
24
    from zope.component.testing import tearDown
1✔
25

26
    def _callFUT(self, *args, **kw):
1✔
27
        from zope.component._api import getSiteManager
1✔
28
        return getSiteManager(*args, **kw)
1✔
29

30
    def test_sm_is_IComponentLookup(self):
1✔
31
        from zope.interface.interfaces import IComponentLookup
1✔
32
        sm = self._callFUT()
1✔
33
        self.assertTrue(IComponentLookup.providedBy(sm))
1✔
34

35
    def test_sm_is_singleton(self):
1✔
36
        from zope.component.globalregistry import base
1✔
37
        sm = self._callFUT()
1✔
38
        self.assertTrue(sm is base)
1✔
39
        self.assertTrue(self._callFUT() is sm)
1✔
40

41
    def test_w_None(self):
1✔
42
        self.assertTrue(self._callFUT(None) is self._callFUT())
1✔
43

44
    def test_getSiteManager_w_conforming_context(self):
1✔
45
        from zope.component.tests.examples import ConformsToIComponentLookup
1✔
46
        sitemanager = object()
1✔
47
        context = ConformsToIComponentLookup(sitemanager)
1✔
48
        self.assertTrue(self._callFUT(context) is sitemanager)
1✔
49

50
    def test_getSiteManager_w_invalid_context_no_adapter(self):
1✔
51
        from zope.interface.interfaces import ComponentLookupError
1✔
52
        self.assertRaises(ComponentLookupError, self._callFUT, object())
1✔
53

54
    def test_getSiteManager_w_invalid_context_w_adapter(self):
1✔
55
        from zope.interface import Interface
1✔
56
        from zope.interface.interfaces import IComponentLookup
1✔
57

58
        from zope.component.globalregistry import getGlobalSiteManager
1✔
59
        gsm = getGlobalSiteManager()
1✔
60
        sm = object()
1✔
61

62
        def _adapt(x):
1✔
63
            return sm
1✔
64
        gsm.registerAdapter(_adapt, (Interface,), IComponentLookup, '')
1✔
65
        self.assertTrue(self._callFUT(object()) is sm)
1✔
66

67

68
class Test_getAdapterInContext(unittest.TestCase):
1✔
69

70
    from zope.component.testing import setUp
1✔
71
    from zope.component.testing import tearDown
1✔
72

73
    def _callFUT(self, *args, **kw):
1✔
74
        from zope.component import getAdapterInContext
1✔
75
        return getAdapterInContext(*args, **kw)
1✔
76

77
    def test_miss(self):
1✔
78
        from zope.interface import Interface
1✔
79
        from zope.interface.interfaces import ComponentLookupError
1✔
80

81
        class IFoo(Interface):
1✔
82
            pass
1✔
83
        self.assertRaises(ComponentLookupError,
1✔
84
                          self._callFUT, object(), IFoo, context=None)
85

86
    def test_hit_via_sm(self):
1✔
87
        from zope.interface import Interface
1✔
88
        from zope.interface import implementer
1✔
89
        from zope.interface.registry import Components
1✔
90

91
        from zope.component import getGlobalSiteManager
1✔
92
        from zope.component.tests.examples import ConformsToIComponentLookup
1✔
93

94
        class IFoo(Interface):
1✔
95
            pass
1✔
96

97
        class IBar(Interface):
1✔
98
            pass
1✔
99

100
        @implementer(IFoo)
1✔
101
        class Global:
1✔
102
            __init__ = fails_if_called(self)
1✔
103

104
        @implementer(IFoo)
1✔
105
        class Local:
1✔
106
            def __init__(self, context):
1✔
107
                self.context = context
1✔
108

109
        @implementer(IBar)
1✔
110
        class Bar:
1✔
111
            pass
1✔
112

113
        class Context(ConformsToIComponentLookup):
1✔
114
            def __init__(self, sm):
1✔
115
                self.sitemanager = sm
1✔
116
        gsm = getGlobalSiteManager()
1✔
117
        gsm.registerAdapter(Global, (IBar,), IFoo, '')
1✔
118
        sm1 = Components('sm1', bases=(gsm, ))
1✔
119
        sm1.registerAdapter(Local, (IBar,), IFoo, '')
1✔
120
        bar = Bar()
1✔
121
        adapted = self._callFUT(bar, IFoo, context=Context(sm1))
1✔
122
        self.assertTrue(adapted.__class__ is Local)
1✔
123
        self.assertTrue(adapted.context is bar)
1✔
124

125

126
class Test_queryAdapterInContext(unittest.TestCase):
1✔
127

128
    from zope.component.testing import setUp
1✔
129
    from zope.component.testing import tearDown
1✔
130

131
    def _callFUT(self, *args, **kw):
1✔
132
        from zope.component import queryAdapterInContext
1✔
133
        return queryAdapterInContext(*args, **kw)
1✔
134

135
    def test_miss(self):
1✔
136
        from zope.interface import Interface
1✔
137

138
        class IFoo(Interface):
1✔
139
            pass
1✔
140
        self.assertEqual(
1✔
141
            self._callFUT(object(), IFoo, context=None), None)
142

143
    def test_w_object_conforming(self):
1✔
144
        from zope.interface import Interface
1✔
145

146
        class IFoo(Interface):
1✔
147
            pass
1✔
148
        _adapted = object()
1✔
149

150
        class Foo:
1✔
151
            def __conform__(self, iface, default=None, _test=self):
1✔
152
                _test.assertIs(iface, IFoo)
1✔
153
                return _adapted
1✔
154

155
        self.assertTrue(
1✔
156
            self._callFUT(Foo(), IFoo, context=None) is _adapted)
157

158
    def test___conform___raises_TypeError_via_class(self):
1✔
159
        from zope.interface import Interface
1✔
160

161
        class IFoo(Interface):
1✔
162
            pass
1✔
163

164
        class Foo:
1✔
165
            __conform__ = fails_if_called(self, arguments=False)
1✔
166
        # call via class, triggering TypeError
167
        self.assertEqual(self._callFUT(Foo, IFoo, context=None), None)
1✔
168

169
    def test___conform___raises_TypeError_via_inst(self):
1✔
170
        from zope.interface import Interface
1✔
171

172
        class IFoo(Interface):
1✔
173
            pass
1✔
174

175
        class Foo:
1✔
176
            def __conform__(self, iface, default=None):
1✔
177
                raise TypeError
1✔
178
        self.assertRaises(TypeError,
1✔
179
                          self._callFUT, Foo(), IFoo, context=None)
180

181
    def test_w_object_implementing(self):
1✔
182
        from zope.interface import Interface
1✔
183
        from zope.interface import implementer
1✔
184

185
        class IFoo(Interface):
1✔
186
            pass
1✔
187

188
        @implementer(IFoo)
1✔
189
        class Foo:
1✔
190
            pass
1✔
191
        foo = Foo()
1✔
192
        self.assertIs(
1✔
193
            self._callFUT(foo, IFoo, context=None), foo)
194

195

196
class Test_getAdapter(unittest.TestCase):
1✔
197

198
    from zope.component.testing import setUp
1✔
199
    from zope.component.testing import tearDown
1✔
200

201
    def _callFUT(self, *args, **kw):
1✔
202
        from zope.component import getAdapter
1✔
203
        return getAdapter(*args, **kw)
1✔
204

205
    def test_anonymous_nonesuch(self):
1✔
206
        from zope.interface import Interface
1✔
207
        from zope.interface.interfaces import ComponentLookupError
1✔
208

209
        class IFoo(Interface):
1✔
210
            pass
1✔
211
        self.assertRaises(ComponentLookupError,
1✔
212
                          self._callFUT, object(), IFoo, '')
213

214
    def test_named_nonesuch(self):
1✔
215
        from zope.interface import Interface
1✔
216
        from zope.interface.interfaces import ComponentLookupError
1✔
217

218
        class IFoo(Interface):
1✔
219
            pass
1✔
220
        self.assertRaises(ComponentLookupError,
1✔
221
                          self._callFUT, object(), IFoo, 'bar')
222

223
    def test_anonymous_hit(self):
1✔
224
        from zope.interface import Interface
1✔
225
        from zope.interface import implementer
1✔
226

227
        from zope.component import getGlobalSiteManager
1✔
228

229
        class IFoo(Interface):
1✔
230
            pass
1✔
231

232
        class IBar(Interface):
1✔
233
            pass
1✔
234

235
        @implementer(IBar)
1✔
236
        class Bar:
1✔
237
            pass
1✔
238

239
        @implementer(IFoo)
1✔
240
        class Baz:
1✔
241
            def __init__(self, context):
1✔
242
                self.context = context
1✔
243
        getGlobalSiteManager().registerAdapter(Baz, (IBar,), IFoo, '')
1✔
244
        bar = Bar()
1✔
245
        adapted = self._callFUT(bar, IFoo, '')
1✔
246
        self.assertTrue(adapted.__class__ is Baz)
1✔
247
        self.assertTrue(adapted.context is bar)
1✔
248

249
    def test_anonymous_hit_registered_for_None(self):
1✔
250
        from zope.interface import Interface
1✔
251
        from zope.interface import implementer
1✔
252

253
        from zope.component import getGlobalSiteManager
1✔
254

255
        class IFoo(Interface):
1✔
256
            pass
1✔
257

258
        @implementer(IFoo)
1✔
259
        class Baz:
1✔
260
            def __init__(self, context):
1✔
261
                self.context = context
1✔
262
        getGlobalSiteManager().registerAdapter(Baz, (None,), IFoo, '')
1✔
263
        ctx = object()
1✔
264
        adapted = self._callFUT(ctx, IFoo, '')
1✔
265
        self.assertTrue(adapted.__class__ is Baz)
1✔
266
        self.assertTrue(adapted.context is ctx)
1✔
267

268
    def test_named_hit(self):
1✔
269
        from zope.interface import Interface
1✔
270
        from zope.interface import implementer
1✔
271

272
        from zope.component import getGlobalSiteManager
1✔
273

274
        class IFoo(Interface):
1✔
275
            pass
1✔
276

277
        class IBar(Interface):
1✔
278
            pass
1✔
279

280
        @implementer(IBar)
1✔
281
        class Bar:
1✔
282
            pass
1✔
283

284
        @implementer(IFoo)
1✔
285
        class Baz:
1✔
286
            def __init__(self, context):
1✔
287
                self.context = context
1✔
288
        getGlobalSiteManager().registerAdapter(Baz, (IBar,), IFoo, 'named')
1✔
289
        bar = Bar()
1✔
290
        adapted = self._callFUT(bar, IFoo, 'named')
1✔
291
        self.assertTrue(adapted.__class__ is Baz)
1✔
292
        self.assertTrue(adapted.context is bar)
1✔
293

294

295
class Test_queryAdapter(unittest.TestCase):
1✔
296

297
    from zope.component.testing import setUp
1✔
298
    from zope.component.testing import tearDown
1✔
299

300
    def _callFUT(self, *args, **kw):
1✔
301
        from zope.component import queryAdapter
1✔
302
        return queryAdapter(*args, **kw)
1✔
303

304
    def test_anonymous_nonesuch(self):
1✔
305
        from zope.interface import Interface
1✔
306

307
        class IFoo(Interface):
1✔
308
            pass
1✔
309
        self.assertEqual(self._callFUT(object(), IFoo, '', '<default>'),
1✔
310
                         '<default>')
311

312
    def test_named_nonesuch(self):
1✔
313
        from zope.interface import Interface
1✔
314

315
        class IFoo(Interface):
1✔
316
            pass
1✔
317
        self.assertEqual(self._callFUT(object(), IFoo, 'bar'), None)
1✔
318

319
    def test_anonymous_hit(self):
1✔
320
        from zope.interface import Interface
1✔
321
        from zope.interface import implementer
1✔
322

323
        from zope.component import getGlobalSiteManager
1✔
324

325
        class IFoo(Interface):
1✔
326
            pass
1✔
327

328
        class IBar(Interface):
1✔
329
            pass
1✔
330

331
        @implementer(IBar)
1✔
332
        class Bar:
1✔
333
            pass
1✔
334

335
        @implementer(IFoo)
1✔
336
        class Baz:
1✔
337
            def __init__(self, context):
1✔
338
                self.context = context
1✔
339
        getGlobalSiteManager().registerAdapter(Baz, (IBar,), IFoo, '')
1✔
340
        bar = Bar()
1✔
341
        adapted = self._callFUT(bar, IFoo, '')
1✔
342
        self.assertTrue(adapted.__class__ is Baz)
1✔
343
        self.assertTrue(adapted.context is bar)
1✔
344

345
    def test_named_hit(self):
1✔
346
        from zope.interface import Interface
1✔
347
        from zope.interface import implementer
1✔
348

349
        from zope.component import getGlobalSiteManager
1✔
350

351
        class IFoo(Interface):
1✔
352
            pass
1✔
353

354
        class IBar(Interface):
1✔
355
            pass
1✔
356

357
        @implementer(IBar)
1✔
358
        class Bar:
1✔
359
            pass
1✔
360

361
        @implementer(IFoo)
1✔
362
        class Baz:
1✔
363
            def __init__(self, context):
1✔
364
                self.context = context
1✔
365
        getGlobalSiteManager().registerAdapter(Baz, (IBar,), IFoo, 'named')
1✔
366
        bar = Bar()
1✔
367
        adapted = self._callFUT(bar, IFoo, 'named')
1✔
368
        self.assertTrue(adapted.__class__ is Baz)
1✔
369
        self.assertTrue(adapted.context is bar)
1✔
370

371
    def test_nested(self):
1✔
372
        from zope.interface import Interface
1✔
373
        from zope.interface import implementer
1✔
374
        from zope.interface.registry import Components
1✔
375

376
        from zope.component import getGlobalSiteManager
1✔
377
        from zope.component.tests.examples import ConformsToIComponentLookup
1✔
378

379
        class IFoo(Interface):
1✔
380
            pass
1✔
381

382
        class IBar(Interface):
1✔
383
            pass
1✔
384

385
        @implementer(IFoo)
1✔
386
        class Global:
1✔
387
            __init__ = fails_if_called(self)
1✔
388

389
        @implementer(IFoo)
1✔
390
        class Local:
1✔
391
            def __init__(self, context):
1✔
392
                self.context = context
1✔
393

394
        @implementer(IBar)
1✔
395
        class Bar:
1✔
396
            pass
1✔
397

398
        class Context(ConformsToIComponentLookup):
1✔
399
            def __init__(self, sm):
1✔
400
                self.sitemanager = sm
1✔
401
        gsm = getGlobalSiteManager()
1✔
402
        gsm.registerAdapter(Global, (IBar,), IFoo, '')
1✔
403
        sm1 = Components('sm1', bases=(gsm, ))
1✔
404
        sm1.registerAdapter(Local, (IBar,), IFoo, '')
1✔
405
        bar = Bar()
1✔
406
        adapted = self._callFUT(bar, IFoo, '', context=Context(sm1))
1✔
407
        self.assertTrue(adapted.__class__ is Local)
1✔
408
        self.assertTrue(adapted.context is bar)
1✔
409

410

411
class Test_getMultiAdapter(unittest.TestCase):
1✔
412

413
    from zope.component.testing import setUp
1✔
414
    from zope.component.testing import tearDown
1✔
415

416
    def _callFUT(self, *args, **kw):
1✔
417
        from zope.component import getMultiAdapter
1✔
418
        return getMultiAdapter(*args, **kw)
1✔
419

420
    def test_anonymous_nonesuch(self):
1✔
421
        from zope.interface import Interface
1✔
422
        from zope.interface.interfaces import ComponentLookupError
1✔
423

424
        class IFoo(Interface):
1✔
425
            pass
1✔
426
        self.assertRaises(ComponentLookupError,
1✔
427
                          self._callFUT, (object(), object()), IFoo, '')
428

429
    def test_named_nonesuch(self):
1✔
430
        from zope.interface import Interface
1✔
431
        from zope.interface.interfaces import ComponentLookupError
1✔
432

433
        class IFoo(Interface):
1✔
434
            pass
1✔
435
        self.assertRaises(ComponentLookupError,
1✔
436
                          self._callFUT, (object(), object()), IFoo, 'bar')
437

438
    def test_anonymous_hit(self):
1✔
439
        from zope.interface import Interface
1✔
440
        from zope.interface import implementer
1✔
441

442
        from zope.component import getGlobalSiteManager
1✔
443

444
        class IFoo(Interface):
1✔
445
            pass
1✔
446

447
        class IBar(Interface):
1✔
448
            pass
1✔
449

450
        class IBaz(Interface):
1✔
451
            pass
1✔
452

453
        @implementer(IBar)
1✔
454
        class Bar:
1✔
455
            pass
1✔
456

457
        @implementer(IBaz)
1✔
458
        class Baz:
1✔
459
            pass
1✔
460

461
        @implementer(IFoo)
1✔
462
        class FooAdapter:
1✔
463
            def __init__(self, first, second):
1✔
464
                self.first, self.second = first, second
1✔
465
        getGlobalSiteManager().registerAdapter(
1✔
466
            FooAdapter, (IBar, IBaz), IFoo, '')
467
        bar = Bar()
1✔
468
        baz = Baz()
1✔
469
        adapted = self._callFUT((bar, baz), IFoo, '')
1✔
470
        self.assertTrue(adapted.__class__ is FooAdapter)
1✔
471
        self.assertTrue(adapted.first is bar)
1✔
472
        self.assertTrue(adapted.second is baz)
1✔
473

474
    def test_anonymous_hit_registered_for_None(self):
1✔
475
        from zope.interface import Interface
1✔
476
        from zope.interface import implementer
1✔
477

478
        from zope.component import getGlobalSiteManager
1✔
479

480
        class IFoo(Interface):
1✔
481
            pass
1✔
482

483
        class IBar(Interface):
1✔
484
            pass
1✔
485

486
        class IBaz(Interface):
1✔
487
            pass
1✔
488

489
        @implementer(IBar)
1✔
490
        class Bar:
1✔
491
            pass
1✔
492

493
        @implementer(IFoo)
1✔
494
        class FooAdapter:
1✔
495
            def __init__(self, first, second):
1✔
496
                self.first, self.second = first, second
1✔
497
        getGlobalSiteManager().registerAdapter(
1✔
498
            FooAdapter, (IBar, None), IFoo, '')
499
        bar = Bar()
1✔
500
        baz = object()
1✔
501
        adapted = self._callFUT((bar, baz), IFoo, '')
1✔
502
        self.assertTrue(adapted.__class__ is FooAdapter)
1✔
503
        self.assertTrue(adapted.first is bar)
1✔
504
        self.assertTrue(adapted.second is baz)
1✔
505

506
    def test_named_hit(self):
1✔
507
        from zope.interface import Interface
1✔
508
        from zope.interface import implementer
1✔
509

510
        from zope.component import getGlobalSiteManager
1✔
511

512
        class IFoo(Interface):
1✔
513
            pass
1✔
514

515
        class IBar(Interface):
1✔
516
            pass
1✔
517

518
        class IBaz(Interface):
1✔
519
            pass
1✔
520

521
        @implementer(IBar)
1✔
522
        class Bar:
1✔
523
            pass
1✔
524

525
        @implementer(IBaz)
1✔
526
        class Baz:
1✔
527
            pass
1✔
528

529
        @implementer(IFoo)
1✔
530
        class FooAdapter:
1✔
531
            def __init__(self, first, second):
1✔
532
                self.first, self.second = first, second
1✔
533
        getGlobalSiteManager().registerAdapter(
1✔
534
            FooAdapter, (IBar, IBaz), IFoo, 'named')
535
        bar = Bar()
1✔
536
        baz = Baz()
1✔
537
        adapted = self._callFUT((bar, baz), IFoo, 'named')
1✔
538
        self.assertTrue(adapted.__class__ is FooAdapter)
1✔
539
        self.assertTrue(adapted.first is bar)
1✔
540
        self.assertTrue(adapted.second is baz)
1✔
541

542

543
class Test_queryMultiAdapter(unittest.TestCase):
1✔
544

545
    from zope.component.testing import setUp
1✔
546
    from zope.component.testing import tearDown
1✔
547

548
    def _callFUT(self, *args, **kw):
1✔
549
        from zope.component import queryMultiAdapter
1✔
550
        return queryMultiAdapter(*args, **kw)
1✔
551

552
    def test_anonymous_nonesuch(self):
1✔
553
        from zope.interface import Interface
1✔
554

555
        class IFoo(Interface):
1✔
556
            pass
1✔
557
        self.assertEqual(self._callFUT((object(), object()), IFoo, '',
1✔
558
                                       '<default>'),
559
                         '<default>')
560

561
    def test_named_nonesuch(self):
1✔
562
        from zope.interface import Interface
1✔
563

564
        class IFoo(Interface):
1✔
565
            pass
1✔
566
        self.assertEqual(self._callFUT((object(), object()), IFoo, 'bar'),
1✔
567
                         None)
568

569
    def test_anonymous_hit(self):
1✔
570
        from zope.interface import Interface
1✔
571
        from zope.interface import implementer
1✔
572

573
        from zope.component import getGlobalSiteManager
1✔
574

575
        class IFoo(Interface):
1✔
576
            pass
1✔
577

578
        class IBar(Interface):
1✔
579
            pass
1✔
580

581
        class IBaz(Interface):
1✔
582
            pass
1✔
583

584
        @implementer(IBar)
1✔
585
        class Bar:
1✔
586
            pass
1✔
587

588
        @implementer(IBaz)
1✔
589
        class Baz:
1✔
590
            pass
1✔
591

592
        @implementer(IFoo)
1✔
593
        class FooAdapter:
1✔
594
            def __init__(self, first, second):
1✔
595
                self.first, self.second = first, second
1✔
596
        getGlobalSiteManager().registerAdapter(
1✔
597
            FooAdapter, (IBar, IBaz), IFoo, '')
598
        bar = Bar()
1✔
599
        baz = Baz()
1✔
600
        adapted = self._callFUT((bar, baz), IFoo, '')
1✔
601
        self.assertTrue(adapted.__class__ is FooAdapter)
1✔
602
        self.assertTrue(adapted.first is bar)
1✔
603
        self.assertTrue(adapted.second is baz)
1✔
604

605
    def test_named_hit(self):
1✔
606
        from zope.interface import Interface
1✔
607
        from zope.interface import implementer
1✔
608

609
        from zope.component import getGlobalSiteManager
1✔
610

611
        class IFoo(Interface):
1✔
612
            pass
1✔
613

614
        class IBar(Interface):
1✔
615
            pass
1✔
616

617
        class IBaz(Interface):
1✔
618
            pass
1✔
619

620
        @implementer(IBar)
1✔
621
        class Bar:
1✔
622
            pass
1✔
623

624
        @implementer(IBaz)
1✔
625
        class Baz:
1✔
626
            pass
1✔
627

628
        @implementer(IFoo)
1✔
629
        class FooAdapter:
1✔
630
            def __init__(self, first, second):
1✔
631
                self.first, self.second = first, second
1✔
632
        getGlobalSiteManager().registerAdapter(
1✔
633
            FooAdapter, (IBar, IBaz), IFoo, 'named')
634
        bar = Bar()
1✔
635
        baz = Baz()
1✔
636
        adapted = self._callFUT((bar, baz), IFoo, 'named')
1✔
637
        self.assertTrue(adapted.__class__ is FooAdapter)
1✔
638
        self.assertTrue(adapted.first is bar)
1✔
639
        self.assertTrue(adapted.second is baz)
1✔
640

641
    def test_nested(self):
1✔
642
        from zope.interface import Interface
1✔
643
        from zope.interface import implementer
1✔
644
        from zope.interface.registry import Components
1✔
645

646
        from zope.component import getGlobalSiteManager
1✔
647
        from zope.component.tests.examples import ConformsToIComponentLookup
1✔
648

649
        class IFoo(Interface):
1✔
650
            pass
1✔
651

652
        class IBar(Interface):
1✔
653
            pass
1✔
654

655
        class IBaz(Interface):
1✔
656
            pass
1✔
657

658
        @implementer(IBar)
1✔
659
        class Bar:
1✔
660
            pass
1✔
661

662
        @implementer(IBaz)
1✔
663
        class Baz:
1✔
664
            pass
1✔
665

666
        @implementer(IFoo)
1✔
667
        class Global:
1✔
668
            __init__ = fails_if_called(self)
1✔
669

670
        @implementer(IFoo)
1✔
671
        class Local:
1✔
672
            def __init__(self, first, second):
1✔
673
                self.first, self.second = first, second
1✔
674

675
        class Context(ConformsToIComponentLookup):
1✔
676
            def __init__(self, sm):
1✔
677
                self.sitemanager = sm
1✔
678
        gsm = getGlobalSiteManager()
1✔
679
        gsm.registerAdapter(Global, (IBar, IBaz), IFoo, '')
1✔
680
        sm1 = Components('sm1', bases=(gsm, ))
1✔
681
        sm1.registerAdapter(Local, (IBar, IBaz), IFoo, '')
1✔
682
        bar = Bar()
1✔
683
        baz = Baz()
1✔
684
        adapted = self._callFUT((bar, baz), IFoo, '', context=Context(sm1))
1✔
685
        self.assertTrue(adapted.__class__ is Local)
1✔
686
        self.assertTrue(adapted.first is bar)
1✔
687
        self.assertTrue(adapted.second is baz)
1✔
688

689
    def test_wo_sitemanager(self):
1✔
690
        from zope.interface import Interface
1✔
691
        from zope.interface import implementer
1✔
692
        from zope.interface.interfaces import ComponentLookupError
1✔
693

694
        class IFoo(Interface):
1✔
695
            pass
1✔
696

697
        class IBar(Interface):
1✔
698
            pass
1✔
699

700
        class IBaz(Interface):
1✔
701
            pass
1✔
702

703
        @implementer(IBar)
1✔
704
        class Bar:
1✔
705
            pass
1✔
706

707
        @implementer(IBaz)
1✔
708
        class Baz:
1✔
709
            pass
1✔
710

711
        class Context:
1✔
712
            def __conform__(self, iface):
1✔
713
                raise ComponentLookupError
1✔
714
        bar = Bar()
1✔
715
        baz = Baz()
1✔
716
        adapted = self._callFUT((bar, baz), IFoo, '', context=Context())
1✔
717
        self.assertTrue(adapted is None)
1✔
718

719

720
class Test_getAdapters(unittest.TestCase):
1✔
721

722
    from zope.component.testing import setUp
1✔
723
    from zope.component.testing import tearDown
1✔
724

725
    def _callFUT(self, *args, **kw):
1✔
726
        from zope.component import getAdapters
1✔
727
        return getAdapters(*args, **kw)
1✔
728

729
    def test_nonesuch(self):
1✔
730
        from zope.interface import Interface
1✔
731

732
        class IFoo(Interface):
1✔
733
            pass
1✔
734
        self.assertEqual(list(self._callFUT((object(),), IFoo)), [])
1✔
735

736
    def test_hit(self):
1✔
737
        from zope.interface import Interface
1✔
738

739
        from zope.component import getGlobalSiteManager
1✔
740

741
        class IFoo(Interface):
1✔
742
            pass
1✔
743

744
        class BarAdapter:
1✔
745
            def __init__(self, context):
1✔
746
                self.context = context
1✔
747

748
        class BazAdapter:
1✔
749
            def __init__(self, context):
1✔
750
                self.context = context
1✔
751
        gsm = getGlobalSiteManager()
1✔
752
        gsm.registerAdapter(BarAdapter, (None,), IFoo)
1✔
753
        gsm.registerAdapter(BazAdapter, (None,), IFoo, name='bar')
1✔
754
        tuples = list(self._callFUT((object(),), IFoo))
1✔
755
        self.assertEqual(len(tuples), 2)
1✔
756
        names = [(x, y.__class__.__name__) for x, y in tuples]
1✔
757
        self.assertTrue(('', 'BarAdapter') in names)
1✔
758
        self.assertTrue(('bar', 'BazAdapter') in names)
1✔
759

760
    def test_wo_sitemanager(self):
1✔
761
        from zope.interface import Interface
1✔
762
        from zope.interface import implementer
1✔
763
        from zope.interface.interfaces import ComponentLookupError
1✔
764

765
        class IFoo(Interface):
1✔
766
            pass
1✔
767

768
        class IBar(Interface):
1✔
769
            pass
1✔
770

771
        class IBaz(Interface):
1✔
772
            pass
1✔
773

774
        @implementer(IBar)
1✔
775
        class Bar:
1✔
776
            pass
1✔
777

778
        @implementer(IBaz)
1✔
779
        class Baz:
1✔
780
            pass
1✔
781

782
        class Context:
1✔
783
            def __conform__(self, iface):
1✔
784
                raise ComponentLookupError
1✔
785
        bar = Bar()
1✔
786
        baz = Baz()
1✔
787
        adapted = self._callFUT((bar, baz), IFoo, context=Context())
1✔
788
        self.assertEqual(adapted, [])
1✔
789

790

791
class Test_subscribers(unittest.TestCase):
1✔
792

793
    from zope.component.testing import setUp
1✔
794
    from zope.component.testing import tearDown
1✔
795

796
    def _callFUT(self, *args, **kw):
1✔
797
        from zope.component import subscribers
1✔
798
        return subscribers(*args, **kw)
1✔
799

800
    def test_nonesuch(self):
1✔
801
        from zope.interface import Interface
1✔
802

803
        class IFoo(Interface):
1✔
804
            pass
1✔
805
        subscribers = self._callFUT((object,), IFoo)
1✔
806
        self.assertEqual(subscribers, [])
1✔
807

808
    def test_hit(self):
1✔
809
        from zope.interface import Interface
1✔
810

811
        from zope.component import getGlobalSiteManager
1✔
812

813
        class IFoo(Interface):
1✔
814
            pass
1✔
815

816
        class BarAdapter:
1✔
817
            def __init__(self, context):
1✔
818
                self.context = context
1✔
819

820
        class BazAdapter:
1✔
821
            def __init__(self, context):
1✔
822
                self.context = context
1✔
823
        gsm = getGlobalSiteManager()
1✔
824
        gsm.registerSubscriptionAdapter(BarAdapter, (None,), IFoo)
1✔
825
        gsm.registerSubscriptionAdapter(BazAdapter, (None,), IFoo)
1✔
826
        subscribers = self._callFUT((object(),), IFoo)
1✔
827
        self.assertEqual(len(subscribers), 2)
1✔
828
        names = [(x.__class__.__name__) for x in subscribers]
1✔
829
        self.assertTrue('BarAdapter' in names)
1✔
830
        self.assertTrue('BazAdapter' in names)
1✔
831

832
    def test_wo_sitemanager(self):
1✔
833
        from zope.interface import Interface
1✔
834
        from zope.interface.interfaces import ComponentLookupError
1✔
835

836
        class IFoo(Interface):
1✔
837
            pass
1✔
838

839
        class Context:
1✔
840
            def __conform__(self, iface):
1✔
841
                raise ComponentLookupError
1✔
842
        subscribers = self._callFUT((object,), IFoo, context=Context())
1✔
843
        self.assertEqual(subscribers, [])
1✔
844

845

846
class Test_handle(unittest.TestCase):
1✔
847

848
    from zope.component.testing import setUp
1✔
849
    from zope.component.testing import tearDown
1✔
850

851
    def _callFUT(self, *args, **kw):
1✔
852
        from zope.component import handle
1✔
853
        return handle(*args, **kw)
1✔
854

855
    def test_nonesuch(self):
1✔
856
        from zope.interface import Interface
1✔
857

858
        class IFoo(Interface):
1✔
859
            pass
1✔
860
        self._callFUT((object,), IFoo)  # doesn't raise
1✔
861

862
    def test_hit(self):
1✔
863
        from zope.interface import Interface
1✔
864
        from zope.interface import implementer
1✔
865

866
        from zope.component import getGlobalSiteManager
1✔
867

868
        class IFoo(Interface):
1✔
869
            pass
1✔
870

871
        @implementer(IFoo)
1✔
872
        class Foo:
1✔
873
            pass
1✔
874
        _called = []
1✔
875

876
        def _bar(context):
1✔
877
            _called.append('_bar')
1✔
878

879
        def _baz(context):
1✔
880
            _called.append('_baz')
1✔
881
        gsm = getGlobalSiteManager()
1✔
882
        gsm.registerHandler(_bar, (IFoo,))
1✔
883
        gsm.registerHandler(_baz, (IFoo,))
1✔
884
        self._callFUT(Foo())
1✔
885
        self.assertEqual(len(_called), 2, _called)
1✔
886
        self.assertTrue('_bar' in _called)
1✔
887
        self.assertTrue('_baz' in _called)
1✔
888

889

890
class Test_getUtility(unittest.TestCase):
1✔
891

892
    from zope.component.testing import setUp
1✔
893
    from zope.component.testing import tearDown
1✔
894

895
    def _callFUT(self, *args, **kw):
1✔
896
        from zope.component._api import getUtility
1✔
897
        return getUtility(*args, **kw)
1✔
898

899
    def test_anonymous_nonesuch(self):
1✔
900
        from zope.interface import Interface
1✔
901
        from zope.interface.interfaces import ComponentLookupError
1✔
902

903
        class IFoo(Interface):
1✔
904
            pass
1✔
905
        self.assertRaises(ComponentLookupError, self._callFUT, IFoo)
1✔
906

907
    def test_named_nonesuch(self):
1✔
908
        from zope.interface import Interface
1✔
909
        from zope.interface.interfaces import ComponentLookupError
1✔
910

911
        class IFoo(Interface):
1✔
912
            pass
1✔
913
        self.assertRaises(ComponentLookupError,
1✔
914
                          self._callFUT, IFoo, name='bar')
915

916
    def test_anonymous_hit(self):
1✔
917
        from zope.interface import Interface
1✔
918

919
        from zope.component import getGlobalSiteManager
1✔
920

921
        class IFoo(Interface):
1✔
922
            pass
1✔
923
        obj = object()
1✔
924
        getGlobalSiteManager().registerUtility(obj, IFoo)
1✔
925
        self.assertTrue(self._callFUT(IFoo) is obj)
1✔
926

927
    def test_named_hit(self):
1✔
928
        from zope.interface import Interface
1✔
929

930
        from zope.component import getGlobalSiteManager
1✔
931

932
        class IFoo(Interface):
1✔
933
            pass
1✔
934
        obj = object()
1✔
935
        getGlobalSiteManager().registerUtility(obj, IFoo, name='bar')
1✔
936
        self.assertTrue(self._callFUT(IFoo, name='bar') is obj)
1✔
937

938
    def test_w_conforming_context(self):
1✔
939
        from zope.interface import Interface
1✔
940

941
        from zope.component import getGlobalSiteManager
1✔
942
        from zope.component.tests.examples import ConformsToIComponentLookup
1✔
943

944
        class SM:
1✔
945
            def __init__(self, obj):
1✔
946
                self._obj = obj
1✔
947

948
            def queryUtility(self, interface, name, default):
1✔
949
                return self._obj
1✔
950

951
        class IFoo(Interface):
1✔
952
            pass
1✔
953
        obj1 = object()
1✔
954
        obj2 = object()
1✔
955
        sm = SM(obj2)
1✔
956
        context = ConformsToIComponentLookup(sm)
1✔
957
        getGlobalSiteManager().registerUtility(obj1, IFoo)
1✔
958
        self.assertTrue(self._callFUT(IFoo, context=context) is obj2)
1✔
959

960

961
class Test_queryUtility(unittest.TestCase):
1✔
962

963
    from zope.component.testing import setUp
1✔
964
    from zope.component.testing import tearDown
1✔
965

966
    def _callFUT(self, *args, **kw):
1✔
967
        from zope.component._api import queryUtility
1✔
968
        return queryUtility(*args, **kw)
1✔
969

970
    def test_anonymous_nonesuch(self):
1✔
971
        from zope.interface import Interface
1✔
972

973
        class IFoo(Interface):
1✔
974
            pass
1✔
975
        self.assertEqual(self._callFUT(IFoo), None)
1✔
976

977
    def test_anonymous_nonesuch_w_default(self):
1✔
978
        from zope.interface import Interface
1✔
979

980
        class IFoo(Interface):
1✔
981
            pass
1✔
982
        obj = object()
1✔
983
        self.assertTrue(self._callFUT(IFoo, default=obj) is obj)
1✔
984

985
    def test_named_nonesuch(self):
1✔
986
        from zope.interface import Interface
1✔
987

988
        class IFoo(Interface):
1✔
989
            pass
1✔
990
        self.assertEqual(self._callFUT(IFoo, name='bar'), None)
1✔
991

992
    def test_named_nonesuch_w_default(self):
1✔
993
        from zope.interface import Interface
1✔
994

995
        class IFoo(Interface):
1✔
996
            pass
1✔
997
        obj = object()
1✔
998
        self.assertTrue(self._callFUT(IFoo, name='bar', default=obj) is obj)
1✔
999

1000
    def test_anonymous_hit(self):
1✔
1001
        from zope.interface import Interface
1✔
1002

1003
        from zope.component import getGlobalSiteManager
1✔
1004

1005
        class IFoo(Interface):
1✔
1006
            pass
1✔
1007
        obj = object()
1✔
1008
        getGlobalSiteManager().registerUtility(obj, IFoo)
1✔
1009
        self.assertTrue(self._callFUT(IFoo) is obj)
1✔
1010

1011
    def test_named_hit(self):
1✔
1012
        from zope.interface import Interface
1✔
1013

1014
        from zope.component import getGlobalSiteManager
1✔
1015

1016
        class IFoo(Interface):
1✔
1017
            pass
1✔
1018
        obj = object()
1✔
1019
        getGlobalSiteManager().registerUtility(obj, IFoo, name='bar')
1✔
1020
        self.assertTrue(self._callFUT(IFoo, name='bar') is obj)
1✔
1021

1022
    def test_w_conforming_context(self):
1✔
1023
        from zope.interface import Interface
1✔
1024

1025
        from zope.component import getGlobalSiteManager
1✔
1026
        from zope.component.tests.examples import ConformsToIComponentLookup
1✔
1027

1028
        class SM:
1✔
1029
            def __init__(self, obj):
1✔
1030
                self._obj = obj
1✔
1031

1032
            def queryUtility(self, interface, name, default):
1✔
1033
                return self._obj
1✔
1034

1035
        class IFoo(Interface):
1✔
1036
            pass
1✔
1037
        obj1 = object()
1✔
1038
        obj2 = object()
1✔
1039
        sm = SM(obj2)
1✔
1040
        context = ConformsToIComponentLookup(sm)
1✔
1041
        getGlobalSiteManager().registerUtility(obj1, IFoo)
1✔
1042
        self.assertTrue(self._callFUT(IFoo, context=context) is obj2)
1✔
1043

1044

1045
class Test_getUtilitiesFor(unittest.TestCase):
1✔
1046

1047
    from zope.component.testing import setUp
1✔
1048
    from zope.component.testing import tearDown
1✔
1049

1050
    def _callFUT(self, *args, **kw):
1✔
1051
        from zope.component._api import getUtilitiesFor
1✔
1052
        return getUtilitiesFor(*args, **kw)
1✔
1053

1054
    def test_nonesuch(self):
1✔
1055
        from zope.interface import Interface
1✔
1056

1057
        class IFoo(Interface):
1✔
1058
            pass
1✔
1059
        self.assertEqual(list(self._callFUT(IFoo)), [])
1✔
1060

1061
    def test_hit(self):
1✔
1062
        from zope.interface import Interface
1✔
1063

1064
        from zope.component import getGlobalSiteManager
1✔
1065

1066
        class IFoo(Interface):
1✔
1067
            pass
1✔
1068
        obj = object()
1✔
1069
        obj1 = object()
1✔
1070
        getGlobalSiteManager().registerUtility(obj, IFoo)
1✔
1071
        getGlobalSiteManager().registerUtility(obj1, IFoo, name='bar')
1✔
1072
        tuples = list(self._callFUT(IFoo))
1✔
1073
        self.assertEqual(len(tuples), 2)
1✔
1074
        self.assertTrue(('', obj) in tuples)
1✔
1075
        self.assertTrue(('bar', obj1) in tuples)
1✔
1076

1077

1078
class Test_getAllUtilitiesRegisteredFor(unittest.TestCase):
1✔
1079

1080
    from zope.component.testing import setUp
1✔
1081
    from zope.component.testing import tearDown
1✔
1082

1083
    def _callFUT(self, *args, **kw):
1✔
1084
        from zope.component import getAllUtilitiesRegisteredFor
1✔
1085
        return getAllUtilitiesRegisteredFor(*args, **kw)
1✔
1086

1087
    def test_nonesuch(self):
1✔
1088
        from zope.interface import Interface
1✔
1089

1090
        class IFoo(Interface):
1✔
1091
            pass
1✔
1092
        self.assertEqual(list(self._callFUT(IFoo)), [])
1✔
1093

1094
    def test_hit(self):
1✔
1095
        from zope.interface import Interface
1✔
1096

1097
        from zope.component import getGlobalSiteManager
1✔
1098

1099
        class IFoo(Interface):
1✔
1100
            pass
1✔
1101

1102
        class IBar(IFoo):
1✔
1103
            pass
1✔
1104
        obj = object()
1✔
1105
        obj1 = object()
1✔
1106
        obj2 = object()
1✔
1107
        getGlobalSiteManager().registerUtility(obj, IFoo)
1✔
1108
        getGlobalSiteManager().registerUtility(obj1, IFoo, name='bar')
1✔
1109
        getGlobalSiteManager().registerUtility(obj2, IBar)
1✔
1110
        uts = list(self._callFUT(IFoo))
1✔
1111
        self.assertEqual(len(uts), 3)
1✔
1112
        self.assertTrue(obj in uts)
1✔
1113
        self.assertTrue(obj1 in uts)
1✔
1114
        self.assertTrue(obj2 in uts)
1✔
1115

1116

1117
class Test_getNextUtility(unittest.TestCase):
1✔
1118

1119
    from zope.component.testing import setUp
1✔
1120
    from zope.component.testing import tearDown
1✔
1121

1122
    def _callFUT(self, *args, **kw):
1✔
1123
        from zope.component import getNextUtility
1✔
1124
        return getNextUtility(*args, **kw)
1✔
1125

1126
    def test_global(self):
1✔
1127
        from zope.component import getGlobalSiteManager
1✔
1128
        from zope.component.interface import ComponentLookupError
1✔
1129
        gsm = getGlobalSiteManager()
1✔
1130
        gutil = _makeMyUtility('global', gsm)
1✔
1131
        gsm.registerUtility(gutil, IMyUtility, 'myutil')
1✔
1132
        self.assertRaises(ComponentLookupError,
1✔
1133
                          self._callFUT, gutil, IMyUtility, 'myutil')
1134

1135
    def test_nested(self):
1✔
1136
        from zope.interface.interfaces import IComponentLookup
1✔
1137
        from zope.interface.registry import Components
1✔
1138

1139
        from zope.component import getGlobalSiteManager
1✔
1140
        gsm = getGlobalSiteManager()
1✔
1141
        gutil = _makeMyUtility('global', gsm)
1✔
1142
        gsm.registerUtility(gutil, IMyUtility, 'myutil')
1✔
1143
        sm1 = Components('sm1', bases=(gsm, ))
1✔
1144
        sm1_1 = Components('sm1_1', bases=(sm1, ))
1✔
1145
        util1 = _makeMyUtility('one', sm1)
1✔
1146
        sm1.registerUtility(util1, IMyUtility, 'myutil')
1✔
1147
        self.assertTrue(IComponentLookup(util1) is sm1)
1✔
1148
        self.assertTrue(self._callFUT(util1, IMyUtility, 'myutil') is gutil)
1✔
1149
        util1_1 = _makeMyUtility('one-one', sm1_1)
1✔
1150
        sm1_1.registerUtility(util1_1, IMyUtility, 'myutil')
1✔
1151
        self.assertTrue(IComponentLookup(util1_1) is sm1_1)
1✔
1152
        self.assertTrue(self._callFUT(util1_1, IMyUtility, 'myutil') is util1)
1✔
1153

1154

1155
class Test_queryNextUtility(unittest.TestCase):
1✔
1156

1157
    from zope.component.testing import setUp
1✔
1158
    from zope.component.testing import tearDown
1✔
1159

1160
    def _callFUT(self, *args, **kw):
1✔
1161
        from zope.component import queryNextUtility
1✔
1162
        return queryNextUtility(*args, **kw)
1✔
1163

1164
    def test_global(self):
1✔
1165
        from zope.component import getGlobalSiteManager
1✔
1166
        gsm = getGlobalSiteManager()
1✔
1167
        gutil = _makeMyUtility('global', gsm)
1✔
1168
        gsm.registerUtility(gutil, IMyUtility, 'myutil')
1✔
1169
        self.assertEqual(self._callFUT(gutil, IMyUtility, 'myutil'), None)
1✔
1170

1171
    def test_nested(self):
1✔
1172
        from zope.interface.registry import Components
1✔
1173

1174
        from zope.component import getGlobalSiteManager
1✔
1175
        gsm = getGlobalSiteManager()
1✔
1176
        gutil = _makeMyUtility('global', gsm)
1✔
1177
        gsm.registerUtility(gutil, IMyUtility, 'myutil')
1✔
1178
        sm1 = Components('sm1', bases=(gsm, ))
1✔
1179
        sm1_1 = Components('sm1_1', bases=(sm1, ))
1✔
1180
        util1 = _makeMyUtility('one', sm1)
1✔
1181
        sm1.registerUtility(util1, IMyUtility, 'myutil')
1✔
1182
        util1_1 = _makeMyUtility('one-one', sm1_1)
1✔
1183
        sm1_1.registerUtility(util1_1, IMyUtility, 'myutil')
1✔
1184
        myregistry = Components()
1✔
1185
        custom_util = _makeMyUtility('my_custom_util', myregistry)
1✔
1186
        myregistry.registerUtility(custom_util, IMyUtility, 'my_custom_util')
1✔
1187
        sm1.__bases__ = (myregistry,) + sm1.__bases__
1✔
1188
        # Both the ``myregistry`` and global utilities should be available:
1189
        self.assertTrue(self._callFUT(sm1, IMyUtility, 'my_custom_util')
1✔
1190
                        is custom_util)
1191
        self.assertTrue(self._callFUT(sm1, IMyUtility, 'myutil') is gutil)
1✔
1192

1193
    def test_wo_sitemanager(self):
1✔
1194
        from zope.interface import Interface
1✔
1195
        from zope.interface.interfaces import ComponentLookupError
1✔
1196

1197
        class IFoo(Interface):
1✔
1198
            pass
1✔
1199

1200
        class Context:
1✔
1201
            def __conform__(self, iface):
1✔
1202
                raise ComponentLookupError
1✔
1203
        self.assertEqual(self._callFUT(Context(), IFoo, 'myutil'), None)
1✔
1204

1205

1206
class Test_createObject(unittest.TestCase):
1✔
1207

1208
    from zope.component.testing import setUp
1✔
1209
    from zope.component.testing import tearDown
1✔
1210

1211
    def _callFUT(self, *args, **kw):
1✔
1212
        from zope.component import createObject
1✔
1213
        return createObject(*args, **kw)
1✔
1214

1215
    def test_miss(self):
1✔
1216
        from zope.interface.interfaces import ComponentLookupError
1✔
1217
        self.assertRaises(ComponentLookupError, self._callFUT, 'nonesuch')
1✔
1218

1219
    def test_hit(self):
1✔
1220
        from zope.component.interfaces import IFactory
1✔
1221
        _object = object()
1✔
1222
        _factory_called = []
1✔
1223

1224
        def _factory(*args, **kw):
1✔
1225
            _factory_called.append((args, kw))
1✔
1226
            return _object
1✔
1227

1228
        class Context:
1✔
1229
            def __conform__(self, iface):
1✔
1230
                return self
1✔
1231

1232
            def queryUtility(self, iface, name, default, _test=self):
1✔
1233
                _test.assertIs(iface, IFactory)
1✔
1234
                _test.assertEqual(name, 'test')
1✔
1235
                return _factory
1✔
1236

1237
        context = Context()
1✔
1238
        self.assertTrue(self._callFUT('test', context=context) is _object)
1✔
1239
        self.assertEqual(_factory_called, [((), {})])
1✔
1240

1241

1242
class Test_getFactoryInterfaces(unittest.TestCase):
1✔
1243

1244
    from zope.component.testing import setUp
1✔
1245
    from zope.component.testing import tearDown
1✔
1246

1247
    def _callFUT(self, *args, **kw):
1✔
1248
        from zope.component import getFactoryInterfaces
1✔
1249
        return getFactoryInterfaces(*args, **kw)
1✔
1250

1251
    def test_miss(self):
1✔
1252
        from zope.interface.interfaces import ComponentLookupError
1✔
1253
        self.assertRaises(ComponentLookupError, self._callFUT, 'nonesuch')
1✔
1254

1255
    def test_hit(self):
1✔
1256
        from zope.interface import Interface
1✔
1257

1258
        from zope.component.interfaces import IFactory
1✔
1259

1260
        class IFoo(Interface):
1✔
1261
            pass
1✔
1262

1263
        class _Factory:
1✔
1264
            def getInterfaces(self):
1✔
1265
                return [IFoo]
1✔
1266

1267
        class Context:
1✔
1268
            def __conform__(self, iface):
1✔
1269
                return self
1✔
1270

1271
            def queryUtility(self, iface, name, default, _test=self):
1✔
1272
                _test.assertIs(iface, IFactory)
1✔
1273
                _test.assertEqual(name, 'test')
1✔
1274
                return _Factory()
1✔
1275

1276
        context = Context()
1✔
1277
        self.assertEqual(self._callFUT('test', context=context), [IFoo])
1✔
1278

1279

1280
class Test_getFactoriesFor(unittest.TestCase):
1✔
1281

1282
    from zope.component.testing import setUp
1✔
1283
    from zope.component.testing import tearDown
1✔
1284

1285
    def _callFUT(self, *args, **kw):
1✔
1286
        from zope.component import getFactoriesFor
1✔
1287
        return getFactoriesFor(*args, **kw)
1✔
1288

1289
    def test_no_factories_registered(self):
1✔
1290
        from zope.interface import Interface
1✔
1291

1292
        class IFoo(Interface):
1✔
1293
            pass
1✔
1294
        self.assertEqual(list(self._callFUT(IFoo)), [])
1✔
1295

1296
    def test_w_factory_returning_spec(self):
1✔
1297
        from zope.interface import Interface
1✔
1298
        from zope.interface import implementer
1✔
1299
        from zope.interface import providedBy
1✔
1300

1301
        from zope.component.interfaces import IFactory
1✔
1302

1303
        class IFoo(Interface):
1✔
1304
            pass
1✔
1305

1306
        class IBar(Interface):
1✔
1307
            pass
1✔
1308

1309
        @implementer(IFoo, IBar)
1✔
1310
        class _Factory:
1✔
1311
            def getInterfaces(self):
1✔
1312
                return providedBy(self)
1✔
1313
        _factory = _Factory()
1✔
1314

1315
        class Context:
1✔
1316
            def __conform__(self, iface):
1✔
1317
                return self
1✔
1318

1319
            def getUtilitiesFor(self, iface):
1✔
1320
                if iface is IFactory:
1!
1321
                    return [('test', _factory)]
1✔
1322
        self.assertEqual(list(self._callFUT(IFoo, context=Context())),
1✔
1323
                         [('test', _factory)])
1324
        self.assertEqual(list(self._callFUT(IBar, context=Context())),
1✔
1325
                         [('test', _factory)])
1326

1327
    def test_w_factory_returning_list_of_interfaces(self):
1✔
1328
        from zope.interface import Interface
1✔
1329

1330
        from zope.component.interfaces import IFactory
1✔
1331

1332
        class IFoo(Interface):
1✔
1333
            pass
1✔
1334

1335
        class IBar(Interface):
1✔
1336
            pass
1✔
1337

1338
        class _Factory:
1✔
1339
            def getInterfaces(self):
1✔
1340
                return [IFoo, IBar]
1✔
1341
        _factory = _Factory()
1✔
1342

1343
        class Context:
1✔
1344
            def __conform__(self, iface):
1✔
1345
                return self
1✔
1346

1347
            def getUtilitiesFor(self, iface):
1✔
1348
                if iface is IFactory:
1!
1349
                    return [('test', _factory)]
1✔
1350
        self.assertEqual(list(self._callFUT(IFoo, context=Context())),
1✔
1351
                         [('test', _factory)])
1352
        self.assertEqual(list(self._callFUT(IBar, context=Context())),
1✔
1353
                         [('test', _factory)])
1354

1355

1356
IMyUtility = None
1✔
1357

1358

1359
def _makeMyUtility(name, sm):
1✔
1360
    global IMyUtility
1361
    from zope.interface import Interface
1✔
1362
    from zope.interface import implementer
1✔
1363

1364
    from zope.component.tests.examples import ConformsToIComponentLookup
1✔
1365

1366
    if IMyUtility is None:
1✔
1367
        class IMyUtility(Interface):
1✔
1368
            pass
1✔
1369

1370
    @implementer(IMyUtility)
1✔
1371
    class MyUtility(ConformsToIComponentLookup):
1✔
1372
        def __init__(self, id, sm):
1✔
1373
            self.id = id
1✔
1374
            self.sitemanager = sm
1✔
1375

1376
    return MyUtility(name, sm)
1✔
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