1
# Copyright 2010 Canonical Ltd. This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
6
from zope.interface import implements, Interface
7
from zope.interface.verify import verifyObject
8
from zope.interface.exceptions import BrokenImplementation
9
from zope.security.checker import NamesChecker
10
from zope.security.proxy import ProxyFactory
12
from lp.testing import TestCase
13
from lp.testing.matchers import (
14
DoesNotCorrectlyProvide, DoesNotProvide, IsNotProxied, IsProxied,
15
Provides, ProvidesAndIsProxied)
18
class ITestInterface(Interface):
19
"""A dummy interface for testing."""
22
"""Dummy method for interface compliance testing."""
26
"""Dummy class that implements ITestInterface for testing."""
28
implements(ITestInterface)
34
class DoesNotProvideTests(TestCase):
36
def test_describe(self):
38
mismatch = DoesNotProvide(obj, ITestInterface)
40
"%r does not provide %r." % (obj, ITestInterface),
44
class DoesNotCorrectlyProvideMismatchTests(TestCase):
46
def test_describe(self):
48
mismatch = DoesNotCorrectlyProvide(obj, ITestInterface)
50
"%r claims to provide %r, but does not do so correctly."
51
% (obj, ITestInterface),
54
def test_describe_with_extra(self):
56
mismatch = DoesNotCorrectlyProvide(
57
obj, ITestInterface, extra="foo")
59
"%r claims to provide %r, but does not do so correctly: foo"
60
% (obj, ITestInterface),
64
class ProvidesTests(TestCase):
67
matcher = Provides(ITestInterface)
68
self.assertEqual("provides %r." % ITestInterface, str(matcher))
70
def test_matches(self):
71
matcher = Provides(ITestInterface)
72
self.assertEqual(None, matcher.match(Implementor()))
74
def match_does_not_provide(self):
76
matcher = Provides(ITestInterface)
77
return obj, matcher.match(obj)
79
def test_mismatches_not_implements(self):
80
obj, mismatch = self.match_does_not_provide()
81
self.assertIsInstance(mismatch, DoesNotProvide)
83
def test_does_not_provide_sets_object(self):
84
obj, mismatch = self.match_does_not_provide()
85
self.assertEqual(obj, mismatch.obj)
87
def test_does_not_provide_sets_interface(self):
88
obj, mismatch = self.match_does_not_provide()
89
self.assertEqual(ITestInterface, mismatch.interface)
91
def match_does_not_verify(self):
92
class BadlyImplementedClass:
93
implements(ITestInterface)
94
obj = BadlyImplementedClass()
95
matcher = Provides(ITestInterface)
96
return obj, matcher.match(obj)
98
def test_mismatch_does_not_verify(self):
99
obj, mismatch = self.match_does_not_verify()
100
self.assertIsInstance(mismatch, DoesNotCorrectlyProvide)
102
def test_does_not_verify_sets_object(self):
103
obj, mismatch = self.match_does_not_verify()
104
self.assertEqual(obj, mismatch.obj)
106
def test_does_not_verify_sets_interface(self):
107
obj, mismatch = self.match_does_not_verify()
108
self.assertEqual(ITestInterface, mismatch.interface)
110
def test_does_not_verify_sets_extra(self):
111
obj, mismatch = self.match_does_not_verify()
113
verifyObject(ITestInterface, obj)
114
self.assert_("verifyObject did not raise an exception.")
115
except BrokenImplementation, e:
117
self.assertEqual(extra, mismatch.extra)
120
class IsNotProxiedTests(TestCase):
122
def test_describe(self):
124
mismatch = IsNotProxied(obj)
125
self.assertEqual("%r is not proxied." % obj, mismatch.describe())
128
class IsProxiedTests(TestCase):
131
matcher = IsProxied()
132
self.assertEqual("Is proxied.", str(matcher))
134
def test_match(self):
135
obj = ProxyFactory(object(), checker=NamesChecker())
136
self.assertEqual(None, IsProxied().match(obj))
138
def test_mismatch(self):
140
self.assertIsInstance(IsProxied().match(obj), IsNotProxied)
142
def test_mismatch_sets_object(self):
144
mismatch = IsProxied().match(obj)
145
self.assertEqual(obj, mismatch.obj)
148
class ProvidesAndIsProxiedTests(TestCase):
151
matcher = ProvidesAndIsProxied(ITestInterface)
153
"Provides %r and is proxied." % ITestInterface,
156
def test_match(self):
158
Implementor(), checker=NamesChecker(names=("doFoo",)))
159
matcher = ProvidesAndIsProxied(ITestInterface)
160
self.assertThat(obj, matcher)
161
self.assertEqual(None, matcher.match(obj))
163
def test_mismatch_unproxied(self):
165
matcher = ProvidesAndIsProxied(ITestInterface)
166
self.assertIsInstance(matcher.match(obj), IsNotProxied)
168
def test_mismatch_does_not_implement(self):
169
obj = ProxyFactory(object(), checker=NamesChecker())
170
matcher = ProvidesAndIsProxied(ITestInterface)
171
self.assertIsInstance(matcher.match(obj), DoesNotProvide)