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

zopefoundation / zope.component / 16248888290

25 Jun 2025 07:38AM UTC coverage: 99.774%. Remained the same
16248888290

push

github

web-flow
Merge pull request #77 from adrian-zon/patch-1

Fix typo

241 of 252 branches covered (95.63%)

Branch coverage included in aggregate %.

4615 of 4615 relevant lines covered (100.0%)

1.0 hits per line

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

99.8
/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
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.assertIs(sm, base)
1✔
39
        self.assertIs(self._callFUT(), sm)
1✔
40

41
    def test_w_None(self):
1✔
42
        self.assertIs(self._callFUT(None), 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.assertIs(self._callFUT(context), 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.assertIs(self._callFUT(object()), 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.assertIs(adapted.__class__, Local)
1✔
123
        self.assertIs(adapted.context, 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.assertIs(
1✔
156
            self._callFUT(Foo(), IFoo, context=None),
157
            _adapted
158
        )
159

160
    def test___conform___raises_TypeError_via_class(self):
1✔
161
        from zope.interface import Interface
1✔
162

163
        class IFoo(Interface):
1✔
164
            pass
1✔
165

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

171
    def test___conform___raises_TypeError_via_inst(self):
1✔
172
        from zope.interface import Interface
1✔
173

174
        class IFoo(Interface):
1✔
175
            pass
1✔
176

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

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

187
        class IFoo(Interface):
1✔
188
            pass
1✔
189

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

197

198
class Test_getAdapter(unittest.TestCase):
1✔
199

200
    from zope.component.testing import setUp
1✔
201
    from zope.component.testing import tearDown
1✔
202

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

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

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

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

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

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

229
        from zope.component import getGlobalSiteManager
1✔
230

231
        class IFoo(Interface):
1✔
232
            pass
1✔
233

234
        class IBar(Interface):
1✔
235
            pass
1✔
236

237
        @implementer(IBar)
1✔
238
        class Bar:
1✔
239
            pass
1✔
240

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

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

255
        from zope.component import getGlobalSiteManager
1✔
256

257
        class IFoo(Interface):
1✔
258
            pass
1✔
259

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

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

274
        from zope.component import getGlobalSiteManager
1✔
275

276
        class IFoo(Interface):
1✔
277
            pass
1✔
278

279
        class IBar(Interface):
1✔
280
            pass
1✔
281

282
        @implementer(IBar)
1✔
283
        class Bar:
1✔
284
            pass
1✔
285

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

296

297
class Test_queryAdapter(unittest.TestCase):
1✔
298

299
    from zope.component.testing import setUp
1✔
300
    from zope.component.testing import tearDown
1✔
301

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

306
    def test_anonymous_nonesuch(self):
1✔
307
        from zope.interface import Interface
1✔
308

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

314
    def test_named_nonesuch(self):
1✔
315
        from zope.interface import Interface
1✔
316

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

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

325
        from zope.component import getGlobalSiteManager
1✔
326

327
        class IFoo(Interface):
1✔
328
            pass
1✔
329

330
        class IBar(Interface):
1✔
331
            pass
1✔
332

333
        @implementer(IBar)
1✔
334
        class Bar:
1✔
335
            pass
1✔
336

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

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

351
        from zope.component import getGlobalSiteManager
1✔
352

353
        class IFoo(Interface):
1✔
354
            pass
1✔
355

356
        class IBar(Interface):
1✔
357
            pass
1✔
358

359
        @implementer(IBar)
1✔
360
        class Bar:
1✔
361
            pass
1✔
362

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

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

378
        from zope.component import getGlobalSiteManager
1✔
379
        from zope.component.tests.examples import ConformsToIComponentLookup
1✔
380

381
        class IFoo(Interface):
1✔
382
            pass
1✔
383

384
        class IBar(Interface):
1✔
385
            pass
1✔
386

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

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

396
        @implementer(IBar)
1✔
397
        class Bar:
1✔
398
            pass
1✔
399

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

412

413
class Test_getMultiAdapter(unittest.TestCase):
1✔
414

415
    from zope.component.testing import setUp
1✔
416
    from zope.component.testing import tearDown
1✔
417

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

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

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

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

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

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

444
        from zope.component import getGlobalSiteManager
1✔
445

446
        class IFoo(Interface):
1✔
447
            pass
1✔
448

449
        class IBar(Interface):
1✔
450
            pass
1✔
451

452
        class IBaz(Interface):
1✔
453
            pass
1✔
454

455
        @implementer(IBar)
1✔
456
        class Bar:
1✔
457
            pass
1✔
458

459
        @implementer(IBaz)
1✔
460
        class Baz:
1✔
461
            pass
1✔
462

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

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

480
        from zope.component import getGlobalSiteManager
1✔
481

482
        class IFoo(Interface):
1✔
483
            pass
1✔
484

485
        class IBar(Interface):
1✔
486
            pass
1✔
487

488
        class IBaz(Interface):
1✔
489
            pass
1✔
490

491
        @implementer(IBar)
1✔
492
        class Bar:
1✔
493
            pass
1✔
494

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

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

512
        from zope.component import getGlobalSiteManager
1✔
513

514
        class IFoo(Interface):
1✔
515
            pass
1✔
516

517
        class IBar(Interface):
1✔
518
            pass
1✔
519

520
        class IBaz(Interface):
1✔
521
            pass
1✔
522

523
        @implementer(IBar)
1✔
524
        class Bar:
1✔
525
            pass
1✔
526

527
        @implementer(IBaz)
1✔
528
        class Baz:
1✔
529
            pass
1✔
530

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

544

545
class Test_queryMultiAdapter(unittest.TestCase):
1✔
546

547
    from zope.component.testing import setUp
1✔
548
    from zope.component.testing import tearDown
1✔
549

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

554
    def test_anonymous_nonesuch(self):
1✔
555
        from zope.interface import Interface
1✔
556

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

563
    def test_named_nonesuch(self):
1✔
564
        from zope.interface import Interface
1✔
565

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

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

575
        from zope.component import getGlobalSiteManager
1✔
576

577
        class IFoo(Interface):
1✔
578
            pass
1✔
579

580
        class IBar(Interface):
1✔
581
            pass
1✔
582

583
        class IBaz(Interface):
1✔
584
            pass
1✔
585

586
        @implementer(IBar)
1✔
587
        class Bar:
1✔
588
            pass
1✔
589

590
        @implementer(IBaz)
1✔
591
        class Baz:
1✔
592
            pass
1✔
593

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

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

611
        from zope.component import getGlobalSiteManager
1✔
612

613
        class IFoo(Interface):
1✔
614
            pass
1✔
615

616
        class IBar(Interface):
1✔
617
            pass
1✔
618

619
        class IBaz(Interface):
1✔
620
            pass
1✔
621

622
        @implementer(IBar)
1✔
623
        class Bar:
1✔
624
            pass
1✔
625

626
        @implementer(IBaz)
1✔
627
        class Baz:
1✔
628
            pass
1✔
629

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

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

648
        from zope.component import getGlobalSiteManager
1✔
649
        from zope.component.tests.examples import ConformsToIComponentLookup
1✔
650

651
        class IFoo(Interface):
1✔
652
            pass
1✔
653

654
        class IBar(Interface):
1✔
655
            pass
1✔
656

657
        class IBaz(Interface):
1✔
658
            pass
1✔
659

660
        @implementer(IBar)
1✔
661
        class Bar:
1✔
662
            pass
1✔
663

664
        @implementer(IBaz)
1✔
665
        class Baz:
1✔
666
            pass
1✔
667

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

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

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

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

696
        class IFoo(Interface):
1✔
697
            pass
1✔
698

699
        class IBar(Interface):
1✔
700
            pass
1✔
701

702
        class IBaz(Interface):
1✔
703
            pass
1✔
704

705
        @implementer(IBar)
1✔
706
        class Bar:
1✔
707
            pass
1✔
708

709
        @implementer(IBaz)
1✔
710
        class Baz:
1✔
711
            pass
1✔
712

713
        class Context:
1✔
714
            def __conform__(self, iface):
1✔
715
                raise ComponentLookupError
1✔
716
        bar = Bar()
1✔
717
        baz = Baz()
1✔
718
        adapted = self._callFUT((bar, baz), IFoo, '', context=Context())
1✔
719
        self.assertIsNone(adapted)
1✔
720

721

722
class Test_getAdapters(unittest.TestCase):
1✔
723

724
    from zope.component.testing import setUp
1✔
725
    from zope.component.testing import tearDown
1✔
726

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

731
    def test_nonesuch(self):
1✔
732
        from zope.interface import Interface
1✔
733

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

738
    def test_hit(self):
1✔
739
        from zope.interface import Interface
1✔
740

741
        from zope.component import getGlobalSiteManager
1✔
742

743
        class IFoo(Interface):
1✔
744
            pass
1✔
745

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

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

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

767
        class IFoo(Interface):
1✔
768
            pass
1✔
769

770
        class IBar(Interface):
1✔
771
            pass
1✔
772

773
        class IBaz(Interface):
1✔
774
            pass
1✔
775

776
        @implementer(IBar)
1✔
777
        class Bar:
1✔
778
            pass
1✔
779

780
        @implementer(IBaz)
1✔
781
        class Baz:
1✔
782
            pass
1✔
783

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

792

793
class Test_subscribers(unittest.TestCase):
1✔
794

795
    from zope.component.testing import setUp
1✔
796
    from zope.component.testing import tearDown
1✔
797

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

802
    def test_nonesuch(self):
1✔
803
        from zope.interface import Interface
1✔
804

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

810
    def test_hit(self):
1✔
811
        from zope.interface import Interface
1✔
812

813
        from zope.component import getGlobalSiteManager
1✔
814

815
        class IFoo(Interface):
1✔
816
            pass
1✔
817

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

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

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

838
        class IFoo(Interface):
1✔
839
            pass
1✔
840

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

847

848
class Test_handle(unittest.TestCase):
1✔
849

850
    from zope.component.testing import setUp
1✔
851
    from zope.component.testing import tearDown
1✔
852

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

857
    def test_nonesuch(self):
1✔
858
        from zope.interface import Interface
1✔
859

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

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

868
        from zope.component import getGlobalSiteManager
1✔
869

870
        class IFoo(Interface):
1✔
871
            pass
1✔
872

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

878
        def _bar(context):
1✔
879
            _called.append('_bar')
1✔
880

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

891

892
class Test_getUtility(unittest.TestCase):
1✔
893

894
    from zope.component.testing import setUp
1✔
895
    from zope.component.testing import tearDown
1✔
896

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

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

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

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

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

918
    def test_anonymous_hit(self):
1✔
919
        from zope.interface import Interface
1✔
920

921
        from zope.component import getGlobalSiteManager
1✔
922

923
        class IFoo(Interface):
1✔
924
            pass
1✔
925
        obj = object()
1✔
926
        getGlobalSiteManager().registerUtility(obj, IFoo)
1✔
927
        self.assertIs(self._callFUT(IFoo), obj)
1✔
928

929
    def test_named_hit(self):
1✔
930
        from zope.interface import Interface
1✔
931

932
        from zope.component import getGlobalSiteManager
1✔
933

934
        class IFoo(Interface):
1✔
935
            pass
1✔
936
        obj = object()
1✔
937
        getGlobalSiteManager().registerUtility(obj, IFoo, name='bar')
1✔
938
        self.assertIs(self._callFUT(IFoo, name='bar'), obj)
1✔
939

940
    def test_w_conforming_context(self):
1✔
941
        from zope.interface import Interface
1✔
942

943
        from zope.component import getGlobalSiteManager
1✔
944
        from zope.component.tests.examples import ConformsToIComponentLookup
1✔
945

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

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

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

962

963
class Test_queryUtility(unittest.TestCase):
1✔
964

965
    from zope.component.testing import setUp
1✔
966
    from zope.component.testing import tearDown
1✔
967

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

972
    def test_anonymous_nonesuch(self):
1✔
973
        from zope.interface import Interface
1✔
974

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

979
    def test_anonymous_nonesuch_w_default(self):
1✔
980
        from zope.interface import Interface
1✔
981

982
        class IFoo(Interface):
1✔
983
            pass
1✔
984
        obj = object()
1✔
985
        self.assertIs(self._callFUT(IFoo, default=obj), obj)
1✔
986

987
    def test_named_nonesuch(self):
1✔
988
        from zope.interface import Interface
1✔
989

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

994
    def test_named_nonesuch_w_default(self):
1✔
995
        from zope.interface import Interface
1✔
996

997
        class IFoo(Interface):
1✔
998
            pass
1✔
999
        obj = object()
1✔
1000
        self.assertIs(self._callFUT(IFoo, name='bar', default=obj), obj)
1✔
1001

1002
    def test_anonymous_hit(self):
1✔
1003
        from zope.interface import Interface
1✔
1004

1005
        from zope.component import getGlobalSiteManager
1✔
1006

1007
        class IFoo(Interface):
1✔
1008
            pass
1✔
1009
        obj = object()
1✔
1010
        getGlobalSiteManager().registerUtility(obj, IFoo)
1✔
1011
        self.assertIs(self._callFUT(IFoo), obj)
1✔
1012

1013
    def test_named_hit(self):
1✔
1014
        from zope.interface import Interface
1✔
1015

1016
        from zope.component import getGlobalSiteManager
1✔
1017

1018
        class IFoo(Interface):
1✔
1019
            pass
1✔
1020
        obj = object()
1✔
1021
        getGlobalSiteManager().registerUtility(obj, IFoo, name='bar')
1✔
1022
        self.assertIs(self._callFUT(IFoo, name='bar'), obj)
1✔
1023

1024
    def test_w_conforming_context(self):
1✔
1025
        from zope.interface import Interface
1✔
1026

1027
        from zope.component import getGlobalSiteManager
1✔
1028
        from zope.component.tests.examples import ConformsToIComponentLookup
1✔
1029

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

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

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

1046

1047
class Test_getUtilitiesFor(unittest.TestCase):
1✔
1048

1049
    from zope.component.testing import setUp
1✔
1050
    from zope.component.testing import tearDown
1✔
1051

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

1056
    def test_nonesuch(self):
1✔
1057
        from zope.interface import Interface
1✔
1058

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

1063
    def test_hit(self):
1✔
1064
        from zope.interface import Interface
1✔
1065

1066
        from zope.component import getGlobalSiteManager
1✔
1067

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

1079

1080
class Test_getAllUtilitiesRegisteredFor(unittest.TestCase):
1✔
1081

1082
    from zope.component.testing import setUp
1✔
1083
    from zope.component.testing import tearDown
1✔
1084

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

1089
    def test_nonesuch(self):
1✔
1090
        from zope.interface import Interface
1✔
1091

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

1096
    def test_hit(self):
1✔
1097
        from zope.interface import Interface
1✔
1098

1099
        from zope.component import getGlobalSiteManager
1✔
1100

1101
        class IFoo(Interface):
1✔
1102
            pass
1✔
1103

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

1118

1119
class Test_getNextUtility(unittest.TestCase):
1✔
1120

1121
    from zope.component.testing import setUp
1✔
1122
    from zope.component.testing import tearDown
1✔
1123

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

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

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

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

1156

1157
class Test_queryNextUtility(unittest.TestCase):
1✔
1158

1159
    from zope.component.testing import setUp
1✔
1160
    from zope.component.testing import tearDown
1✔
1161

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

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

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

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

1197
    def test_wo_sitemanager(self):
1✔
1198
        from zope.interface import Interface
1✔
1199
        from zope.interface.interfaces import ComponentLookupError
1✔
1200

1201
        class IFoo(Interface):
1✔
1202
            pass
1✔
1203

1204
        class Context:
1✔
1205
            def __conform__(self, iface):
1✔
1206
                raise ComponentLookupError
1✔
1207
        self.assertEqual(self._callFUT(Context(), IFoo, 'myutil'), None)
1✔
1208

1209

1210
class Test_createObject(unittest.TestCase):
1✔
1211

1212
    from zope.component.testing import setUp
1✔
1213
    from zope.component.testing import tearDown
1✔
1214

1215
    def _callFUT(self, *args, **kw):
1✔
1216
        from zope.component import createObject
1✔
1217
        return createObject(*args, **kw)
1✔
1218

1219
    def test_miss(self):
1✔
1220
        from zope.interface.interfaces import ComponentLookupError
1✔
1221
        self.assertRaises(ComponentLookupError, self._callFUT, 'nonesuch')
1✔
1222

1223
    def test_hit(self):
1✔
1224
        from zope.component.interfaces import IFactory
1✔
1225
        _object = object()
1✔
1226
        _factory_called = []
1✔
1227

1228
        def _factory(*args, **kw):
1✔
1229
            _factory_called.append((args, kw))
1✔
1230
            return _object
1✔
1231

1232
        class Context:
1✔
1233
            def __conform__(self, iface):
1✔
1234
                return self
1✔
1235

1236
            def queryUtility(self, iface, name, default, _test=self):
1✔
1237
                _test.assertIs(iface, IFactory)
1✔
1238
                _test.assertEqual(name, 'test')
1✔
1239
                return _factory
1✔
1240

1241
        context = Context()
1✔
1242
        self.assertIs(self._callFUT('test', context=context), _object)
1✔
1243
        self.assertEqual(_factory_called, [((), {})])
1✔
1244

1245

1246
class Test_getFactoryInterfaces(unittest.TestCase):
1✔
1247

1248
    from zope.component.testing import setUp
1✔
1249
    from zope.component.testing import tearDown
1✔
1250

1251
    def _callFUT(self, *args, **kw):
1✔
1252
        from zope.component import getFactoryInterfaces
1✔
1253
        return getFactoryInterfaces(*args, **kw)
1✔
1254

1255
    def test_miss(self):
1✔
1256
        from zope.interface.interfaces import ComponentLookupError
1✔
1257
        self.assertRaises(ComponentLookupError, self._callFUT, 'nonesuch')
1✔
1258

1259
    def test_hit(self):
1✔
1260
        from zope.interface import Interface
1✔
1261

1262
        from zope.component.interfaces import IFactory
1✔
1263

1264
        class IFoo(Interface):
1✔
1265
            pass
1✔
1266

1267
        class _Factory:
1✔
1268
            def getInterfaces(self):
1✔
1269
                return [IFoo]
1✔
1270

1271
        class Context:
1✔
1272
            def __conform__(self, iface):
1✔
1273
                return self
1✔
1274

1275
            def queryUtility(self, iface, name, default, _test=self):
1✔
1276
                _test.assertIs(iface, IFactory)
1✔
1277
                _test.assertEqual(name, 'test')
1✔
1278
                return _Factory()
1✔
1279

1280
        context = Context()
1✔
1281
        self.assertEqual(self._callFUT('test', context=context), [IFoo])
1✔
1282

1283

1284
class Test_getFactoriesFor(unittest.TestCase):
1✔
1285

1286
    from zope.component.testing import setUp
1✔
1287
    from zope.component.testing import tearDown
1✔
1288

1289
    def _callFUT(self, *args, **kw):
1✔
1290
        from zope.component import getFactoriesFor
1✔
1291
        return getFactoriesFor(*args, **kw)
1✔
1292

1293
    def test_no_factories_registered(self):
1✔
1294
        from zope.interface import Interface
1✔
1295

1296
        class IFoo(Interface):
1✔
1297
            pass
1✔
1298
        self.assertEqual(list(self._callFUT(IFoo)), [])
1✔
1299

1300
    def test_w_factory_returning_spec(self):
1✔
1301
        from zope.interface import Interface
1✔
1302
        from zope.interface import implementer
1✔
1303
        from zope.interface import providedBy
1✔
1304

1305
        from zope.component.interfaces import IFactory
1✔
1306

1307
        class IFoo(Interface):
1✔
1308
            pass
1✔
1309

1310
        class IBar(Interface):
1✔
1311
            pass
1✔
1312

1313
        @implementer(IFoo, IBar)
1✔
1314
        class _Factory:
1✔
1315
            def getInterfaces(self):
1✔
1316
                return providedBy(self)
1✔
1317
        _factory = _Factory()
1✔
1318

1319
        class Context:
1✔
1320
            def __conform__(self, iface):
1✔
1321
                return self
1✔
1322

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

1331
    def test_w_factory_returning_list_of_interfaces(self):
1✔
1332
        from zope.interface import Interface
1✔
1333

1334
        from zope.component.interfaces import IFactory
1✔
1335

1336
        class IFoo(Interface):
1✔
1337
            pass
1✔
1338

1339
        class IBar(Interface):
1✔
1340
            pass
1✔
1341

1342
        class _Factory:
1✔
1343
            def getInterfaces(self):
1✔
1344
                return [IFoo, IBar]
1✔
1345
        _factory = _Factory()
1✔
1346

1347
        class Context:
1✔
1348
            def __conform__(self, iface):
1✔
1349
                return self
1✔
1350

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

1359

1360
IMyUtility = None
1✔
1361

1362

1363
def _makeMyUtility(name, sm):
1✔
1364
    global IMyUtility
1365
    from zope.interface import Interface
1✔
1366
    from zope.interface import implementer
1✔
1367

1368
    from zope.component.tests.examples import ConformsToIComponentLookup
1✔
1369

1370
    if IMyUtility is None:
1✔
1371
        class IMyUtility(Interface):
1✔
1372
            pass
1✔
1373

1374
    @implementer(IMyUtility)
1✔
1375
    class MyUtility(ConformsToIComponentLookup):
1✔
1376
        def __init__(self, id, sm):
1✔
1377
            self.id = id
1✔
1378
            self.sitemanager = sm
1✔
1379

1380
    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