~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/testing/tests/test_matchers.py

  • Committer: Launchpad Patch Queue Manager
  • Date: 2010-08-04 16:44:19 UTC
  • mfrom: (11265.1.15 no-more-sampledata-0)
  • Revision ID: launchpad@pqm.canonical.com-20100804164419-5e6x1beuy6f2kzto
[r=jml][ui=none][no-qa] Some cleanup of soyuz test code,
        including use of lp.testing.sampledata,
        factory improvements and addition of some matchers.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2010 Canonical Ltd.  This software is licensed under the
 
2
# GNU Affero General Public License version 3 (see the file LICENSE).
 
3
 
 
4
__metaclass__ = type
 
5
 
 
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
 
11
 
 
12
from lp.testing import TestCase
 
13
from lp.testing.matchers import (
 
14
    DoesNotCorrectlyProvide, DoesNotProvide, IsNotProxied, IsProxied,
 
15
    Provides, ProvidesAndIsProxied)
 
16
 
 
17
 
 
18
class ITestInterface(Interface):
 
19
    """A dummy interface for testing."""
 
20
 
 
21
    def doFoo():
 
22
        """Dummy method for interface compliance testing."""
 
23
 
 
24
 
 
25
class Implementor:
 
26
    """Dummy class that implements ITestInterface for testing."""
 
27
 
 
28
    implements(ITestInterface)
 
29
 
 
30
    def doFoo(self):
 
31
        pass
 
32
 
 
33
 
 
34
class DoesNotProvideTests(TestCase):
 
35
 
 
36
    def test_describe(self):
 
37
        obj = object()
 
38
        mismatch = DoesNotProvide(obj, ITestInterface)
 
39
        self.assertEqual(
 
40
            "%r does not provide %r." % (obj, ITestInterface),
 
41
            mismatch.describe())
 
42
 
 
43
 
 
44
class DoesNotCorrectlyProvideMismatchTests(TestCase):
 
45
 
 
46
    def test_describe(self):
 
47
        obj = object()
 
48
        mismatch = DoesNotCorrectlyProvide(obj, ITestInterface)
 
49
        self.assertEqual(
 
50
            "%r claims to provide %r, but does not do so correctly."
 
51
                % (obj, ITestInterface),
 
52
            mismatch.describe())
 
53
 
 
54
    def test_describe_with_extra(self):
 
55
        obj = object()
 
56
        mismatch = DoesNotCorrectlyProvide(
 
57
            obj, ITestInterface, extra="foo")
 
58
        self.assertEqual(
 
59
            "%r claims to provide %r, but does not do so correctly: foo"
 
60
                % (obj, ITestInterface),
 
61
            mismatch.describe())
 
62
 
 
63
 
 
64
class ProvidesTests(TestCase):
 
65
 
 
66
    def test_str(self):
 
67
        matcher = Provides(ITestInterface)
 
68
        self.assertEqual("provides %r." % ITestInterface, str(matcher))
 
69
 
 
70
    def test_matches(self):
 
71
        matcher = Provides(ITestInterface)
 
72
        self.assertEqual(None, matcher.match(Implementor()))
 
73
 
 
74
    def match_does_not_provide(self):
 
75
        obj = object()
 
76
        matcher = Provides(ITestInterface)
 
77
        return obj, matcher.match(obj)
 
78
 
 
79
    def test_mismatches_not_implements(self):
 
80
        obj, mismatch = self.match_does_not_provide()
 
81
        self.assertIsInstance(mismatch, DoesNotProvide)
 
82
 
 
83
    def test_does_not_provide_sets_object(self):
 
84
        obj, mismatch = self.match_does_not_provide()
 
85
        self.assertEqual(obj, mismatch.obj)
 
86
 
 
87
    def test_does_not_provide_sets_interface(self):
 
88
        obj, mismatch = self.match_does_not_provide()
 
89
        self.assertEqual(ITestInterface, mismatch.interface)
 
90
 
 
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)
 
97
 
 
98
    def test_mismatch_does_not_verify(self):
 
99
        obj, mismatch = self.match_does_not_verify()
 
100
        self.assertIsInstance(mismatch, DoesNotCorrectlyProvide)
 
101
 
 
102
    def test_does_not_verify_sets_object(self):
 
103
        obj, mismatch = self.match_does_not_verify()
 
104
        self.assertEqual(obj, mismatch.obj)
 
105
 
 
106
    def test_does_not_verify_sets_interface(self):
 
107
        obj, mismatch = self.match_does_not_verify()
 
108
        self.assertEqual(ITestInterface, mismatch.interface)
 
109
 
 
110
    def test_does_not_verify_sets_extra(self):
 
111
        obj, mismatch = self.match_does_not_verify()
 
112
        try:
 
113
            verifyObject(ITestInterface, obj)
 
114
            self.assert_("verifyObject did not raise an exception.")
 
115
        except BrokenImplementation, e:
 
116
            extra = str(e)
 
117
        self.assertEqual(extra, mismatch.extra)
 
118
 
 
119
 
 
120
class IsNotProxiedTests(TestCase):
 
121
 
 
122
    def test_describe(self):
 
123
        obj = object()
 
124
        mismatch = IsNotProxied(obj)
 
125
        self.assertEqual("%r is not proxied." % obj, mismatch.describe())
 
126
 
 
127
 
 
128
class IsProxiedTests(TestCase):
 
129
 
 
130
    def test_str(self):
 
131
        matcher = IsProxied()
 
132
        self.assertEqual("Is proxied.", str(matcher))
 
133
 
 
134
    def test_match(self):
 
135
        obj = ProxyFactory(object(), checker=NamesChecker())
 
136
        self.assertEqual(None, IsProxied().match(obj))
 
137
 
 
138
    def test_mismatch(self):
 
139
        obj = object()
 
140
        self.assertIsInstance(IsProxied().match(obj), IsNotProxied)
 
141
 
 
142
    def test_mismatch_sets_object(self):
 
143
        obj = object()
 
144
        mismatch = IsProxied().match(obj)
 
145
        self.assertEqual(obj, mismatch.obj)
 
146
 
 
147
 
 
148
class ProvidesAndIsProxiedTests(TestCase):
 
149
 
 
150
    def test_str(self):
 
151
        matcher = ProvidesAndIsProxied(ITestInterface)
 
152
        self.assertEqual(
 
153
            "Provides %r and is proxied." % ITestInterface,
 
154
            str(matcher))
 
155
 
 
156
    def test_match(self):
 
157
        obj = ProxyFactory(
 
158
            Implementor(), checker=NamesChecker(names=("doFoo",)))
 
159
        matcher = ProvidesAndIsProxied(ITestInterface)
 
160
        self.assertThat(obj, matcher)
 
161
        self.assertEqual(None, matcher.match(obj))
 
162
 
 
163
    def test_mismatch_unproxied(self):
 
164
        obj = Implementor()
 
165
        matcher = ProvidesAndIsProxied(ITestInterface)
 
166
        self.assertIsInstance(matcher.match(obj), IsNotProxied)
 
167
 
 
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)