-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathtest_context.py
More file actions
98 lines (75 loc) · 3 KB
/
test_context.py
File metadata and controls
98 lines (75 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
from node.behaviors import BoundContext
from node.interfaces import IBoundContext
from node.tests import NodeTestCase
from plumber import plumbing
from zope.interface import implementer
from zope.interface import Interface
class IBoundInterface(Interface):
pass
class BoundClass(object):
pass
@plumbing(BoundContext)
class ContextAware(object):
@classmethod
def unbind_context(cls):
cls.__bound_context_interfaces__ = ()
cls.__bound_context_classes__ = ()
class TestContext(NodeTestCase):
def test_BoundContext_bind_context(self):
self.assertEqual(ContextAware.__bound_context_interfaces__, ())
self.assertEqual(ContextAware.__bound_context_classes__, ())
ContextAware.bind_context(None)
self.assertEqual(ContextAware.__bound_context_interfaces__, ())
self.assertEqual(ContextAware.__bound_context_classes__, ())
ca = ContextAware()
self.assertTrue(IBoundContext.providedBy(ca))
ContextAware.bind_context(IBoundInterface)
self.assertEqual(
ContextAware.__bound_context_interfaces__,
(IBoundInterface,)
)
self.assertEqual(ContextAware.__bound_context_classes__, ())
ContextAware.unbind_context()
ContextAware.bind_context(BoundClass)
self.assertEqual(ContextAware.__bound_context_interfaces__, ())
self.assertEqual(
ContextAware.__bound_context_classes__,
(BoundClass,)
)
ContextAware.unbind_context()
ContextAware.bind_context(IBoundInterface, BoundClass)
self.assertEqual(
ContextAware.__bound_context_interfaces__,
(IBoundInterface,)
)
self.assertEqual(
ContextAware.__bound_context_classes__,
(BoundClass,)
)
with self.assertRaises(RuntimeError):
ContextAware.bind_context(object)
ContextAware.unbind_context()
with self.assertRaises(ValueError):
ContextAware.bind_context(lambda: 1)
def test_BoundContext_context_matches(self):
@implementer(IBoundInterface)
class BoundInterface(object):
pass
ContextAware.unbind_context()
inst = ContextAware()
self.assertTrue(inst.context_matches(object()))
ContextAware.bind_context(BoundClass)
inst = ContextAware()
self.assertFalse(inst.context_matches(object()))
self.assertTrue(inst.context_matches(BoundClass()))
ContextAware.unbind_context()
ContextAware.bind_context(IBoundInterface)
inst = ContextAware()
self.assertFalse(inst.context_matches(object()))
self.assertTrue(inst.context_matches(BoundInterface()))
ContextAware.unbind_context()
ContextAware.bind_context(IBoundInterface, BoundClass)
inst = ContextAware()
self.assertFalse(inst.context_matches(object()))
self.assertTrue(inst.context_matches(BoundClass()))
self.assertTrue(inst.context_matches(BoundInterface()))