summaryrefslogtreecommitdiff
path: root/lib/testtools/testtools/tests/matchers/test_higherorder.py
blob: c5cc44eb1d44548a7530ffd4a48468bef72ed00f (plain)
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# Copyright (c) 2008-2011 testtools developers. See LICENSE for details.

from testtools import TestCase
from testtools.matchers import (
    DocTestMatches,
    Equals,
    LessThan,
    MatchesStructure,
    Mismatch,
    NotEquals,
    )
from testtools.matchers._higherorder import (
    AfterPreprocessing,
    AllMatch,
    Annotate,
    AnnotatedMismatch,
    AnyMatch,
    MatchesAny,
    MatchesAll,
    MatchesPredicate,
    Not,
    )
from testtools.tests.helpers import FullStackRunTest
from testtools.tests.matchers.helpers import TestMatchersInterface


class TestAllMatch(TestCase, TestMatchersInterface):

    matches_matcher = AllMatch(LessThan(10))
    matches_matches = [
        [9, 9, 9],
        (9, 9),
        iter([9, 9, 9, 9, 9]),
        ]
    matches_mismatches = [
        [11, 9, 9],
        iter([9, 12, 9, 11]),
        ]

    str_examples = [
        ("AllMatch(LessThan(12))", AllMatch(LessThan(12))),
        ]

    describe_examples = [
        ('Differences: [\n'
         '10 is not > 11\n'
         '10 is not > 10\n'
         ']',
         [11, 9, 10],
         AllMatch(LessThan(10))),
        ]


class TestAnyMatch(TestCase, TestMatchersInterface):

    matches_matcher = AnyMatch(Equals('elephant'))
    matches_matches = [
        ['grass', 'cow', 'steak', 'milk', 'elephant'],
        (13, 'elephant'),
        ['elephant', 'elephant', 'elephant'],
        set(['hippo', 'rhino', 'elephant']),
        ]
    matches_mismatches = [
        [],
        ['grass', 'cow', 'steak', 'milk'],
        (13, 12, 10),
        ['element', 'hephalump', 'pachyderm'],
        set(['hippo', 'rhino', 'diplodocus']),
        ]

    str_examples = [
        ("AnyMatch(Equals('elephant'))", AnyMatch(Equals('elephant'))),
        ]

    describe_examples = [
        ('Differences: [\n'
         '7 != 11\n'
         '7 != 9\n'
         '7 != 10\n'
         ']',
         [11, 9, 10],
         AnyMatch(Equals(7))),
        ]


class TestAfterPreprocessing(TestCase, TestMatchersInterface):

    def parity(x):
        return x % 2

    matches_matcher = AfterPreprocessing(parity, Equals(1))
    matches_matches = [3, 5]
    matches_mismatches = [2]

    str_examples = [
        ("AfterPreprocessing(<function parity>, Equals(1))",
         AfterPreprocessing(parity, Equals(1))),
        ]

    describe_examples = [
        ("1 != 0: after <function parity> on 2", 2,
         AfterPreprocessing(parity, Equals(1))),
        ("1 != 0", 2,
         AfterPreprocessing(parity, Equals(1), annotate=False)),
        ]

class TestMatchersAnyInterface(TestCase, TestMatchersInterface):

    matches_matcher = MatchesAny(DocTestMatches("1"), DocTestMatches("2"))
    matches_matches = ["1", "2"]
    matches_mismatches = ["3"]

    str_examples = [(
        "MatchesAny(DocTestMatches('1\\n'), DocTestMatches('2\\n'))",
        MatchesAny(DocTestMatches("1"), DocTestMatches("2"))),
        ]

    describe_examples = [("""Differences: [
Expected:
    1
Got:
    3

Expected:
    2
Got:
    3

]""",
        "3", MatchesAny(DocTestMatches("1"), DocTestMatches("2")))]


class TestMatchesAllInterface(TestCase, TestMatchersInterface):

    matches_matcher = MatchesAll(NotEquals(1), NotEquals(2))
    matches_matches = [3, 4]
    matches_mismatches = [1, 2]

    str_examples = [
        ("MatchesAll(NotEquals(1), NotEquals(2))",
         MatchesAll(NotEquals(1), NotEquals(2)))]

    describe_examples = [
        ("""Differences: [
1 == 1
]""",
         1, MatchesAll(NotEquals(1), NotEquals(2))),
        ("1 == 1", 1,
         MatchesAll(NotEquals(2), NotEquals(1), Equals(3), first_only=True)),
        ]


class TestAnnotate(TestCase, TestMatchersInterface):

    matches_matcher = Annotate("foo", Equals(1))
    matches_matches = [1]
    matches_mismatches = [2]

    str_examples = [
        ("Annotate('foo', Equals(1))", Annotate("foo", Equals(1)))]

    describe_examples = [("1 != 2: foo", 2, Annotate('foo', Equals(1)))]

    def test_if_message_no_message(self):
        # Annotate.if_message returns the given matcher if there is no
        # message.
        matcher = Equals(1)
        not_annotated = Annotate.if_message('', matcher)
        self.assertIs(matcher, not_annotated)

    def test_if_message_given_message(self):
        # Annotate.if_message returns an annotated version of the matcher if a
        # message is provided.
        matcher = Equals(1)
        expected = Annotate('foo', matcher)
        annotated = Annotate.if_message('foo', matcher)
        self.assertThat(
            annotated,
            MatchesStructure.fromExample(expected, 'annotation', 'matcher'))


class TestAnnotatedMismatch(TestCase):

    run_tests_with = FullStackRunTest

    def test_forwards_details(self):
        x = Mismatch('description', {'foo': 'bar'})
        annotated = AnnotatedMismatch("annotation", x)
        self.assertEqual(x.get_details(), annotated.get_details())


class TestNotInterface(TestCase, TestMatchersInterface):

    matches_matcher = Not(Equals(1))
    matches_matches = [2]
    matches_mismatches = [1]

    str_examples = [
        ("Not(Equals(1))", Not(Equals(1))),
        ("Not(Equals('1'))", Not(Equals('1')))]

    describe_examples = [('1 matches Equals(1)', 1, Not(Equals(1)))]


def is_even(x):
    return x % 2 == 0


class TestMatchesPredicate(TestCase, TestMatchersInterface):

    matches_matcher = MatchesPredicate(is_even, "%s is not even")
    matches_matches = [2, 4, 6, 8]
    matches_mismatches = [3, 5, 7, 9]

    str_examples = [
        ("MatchesPredicate(%r, %r)" % (is_even, "%s is not even"),
         MatchesPredicate(is_even, "%s is not even")),
        ]

    describe_examples = [
        ('7 is not even', 7, MatchesPredicate(is_even, "%s is not even")),
        ]


def test_suite():
    from unittest import TestLoader
    return TestLoader().loadTestsFromName(__name__)