testtools: Update to latest version.
[metze/samba/wip.git] / lib / testtools / testtools / tests / matchers / test_dict.py
1 from testtools import TestCase
2 from testtools.matchers import (
3     Equals,
4     NotEquals,
5     Not,
6     )
7 from testtools.matchers._dict import (
8     ContainedByDict,
9     ContainsDict,
10     KeysEqual,
11     MatchesAllDict,
12     MatchesDict,
13     _SubDictOf,
14     )
15 from testtools.tests.matchers.helpers import TestMatchersInterface
16
17
18 class TestMatchesAllDictInterface(TestCase, TestMatchersInterface):
19
20     matches_matcher = MatchesAllDict({'a': NotEquals(1), 'b': NotEquals(2)})
21     matches_matches = [3, 4]
22     matches_mismatches = [1, 2]
23
24     str_examples = [
25         ("MatchesAllDict({'a': NotEquals(1), 'b': NotEquals(2)})",
26          matches_matcher)]
27
28     describe_examples = [
29         ("""a: 1 == 1""", 1, matches_matcher),
30         ]
31
32
33 class TestKeysEqual(TestCase, TestMatchersInterface):
34
35     matches_matcher = KeysEqual('foo', 'bar')
36     matches_matches = [
37         {'foo': 0, 'bar': 1},
38         ]
39     matches_mismatches = [
40         {},
41         {'foo': 0},
42         {'bar': 1},
43         {'foo': 0, 'bar': 1, 'baz': 2},
44         {'a': None, 'b': None, 'c': None},
45         ]
46
47     str_examples = [
48         ("KeysEqual('foo', 'bar')", KeysEqual('foo', 'bar')),
49         ]
50
51     describe_examples = []
52
53     def test_description(self):
54         matchee = {'foo': 0, 'bar': 1, 'baz': 2}
55         mismatch = KeysEqual('foo', 'bar').match(matchee)
56         description = mismatch.describe()
57         self.assertThat(
58             description, Equals(
59                 "['bar', 'foo'] does not match %r: Keys not equal"
60                 % (matchee,)))
61
62
63 class TestSubDictOf(TestCase, TestMatchersInterface):
64
65     matches_matcher = _SubDictOf({'foo': 'bar', 'baz': 'qux'})
66
67     matches_matches = [
68         {'foo': 'bar', 'baz': 'qux'},
69         {'foo': 'bar'},
70         ]
71
72     matches_mismatches = [
73         {'foo': 'bar', 'baz': 'qux', 'cat': 'dog'},
74         {'foo': 'bar', 'cat': 'dog'},
75         ]
76
77     str_examples = []
78     describe_examples = []
79
80
81 class TestMatchesDict(TestCase, TestMatchersInterface):
82
83     matches_matcher = MatchesDict(
84         {'foo': Equals('bar'), 'baz': Not(Equals('qux'))})
85
86     matches_matches = [
87         {'foo': 'bar', 'baz': None},
88         {'foo': 'bar', 'baz': 'quux'},
89         ]
90     matches_mismatches = [
91         {},
92         {'foo': 'bar', 'baz': 'qux'},
93         {'foo': 'bop', 'baz': 'qux'},
94         {'foo': 'bar', 'baz': 'quux', 'cat': 'dog'},
95         {'foo': 'bar', 'cat': 'dog'},
96         ]
97
98     str_examples = [
99         ("MatchesDict({'baz': %s, 'foo': %s})" % (
100                 Not(Equals('qux')), Equals('bar')),
101          matches_matcher),
102         ]
103
104     describe_examples = [
105         ("Missing: {\n"
106          "  'baz': Not(Equals('qux')),\n"
107          "  'foo': Equals('bar'),\n"
108          "}",
109          {}, matches_matcher),
110         ("Differences: {\n"
111          "  'baz': 'qux' matches Equals('qux'),\n"
112          "}",
113          {'foo': 'bar', 'baz': 'qux'}, matches_matcher),
114         ("Differences: {\n"
115          "  'baz': 'qux' matches Equals('qux'),\n"
116          "  'foo': 'bar' != 'bop',\n"
117          "}",
118          {'foo': 'bop', 'baz': 'qux'}, matches_matcher),
119         ("Extra: {\n"
120          "  'cat': 'dog',\n"
121          "}",
122          {'foo': 'bar', 'baz': 'quux', 'cat': 'dog'}, matches_matcher),
123         ("Extra: {\n"
124          "  'cat': 'dog',\n"
125          "}\n"
126          "Missing: {\n"
127          "  'baz': Not(Equals('qux')),\n"
128          "}",
129          {'foo': 'bar', 'cat': 'dog'}, matches_matcher),
130         ]
131
132
133 class TestContainsDict(TestCase, TestMatchersInterface):
134
135     matches_matcher = ContainsDict(
136         {'foo': Equals('bar'), 'baz': Not(Equals('qux'))})
137
138     matches_matches = [
139         {'foo': 'bar', 'baz': None},
140         {'foo': 'bar', 'baz': 'quux'},
141         {'foo': 'bar', 'baz': 'quux', 'cat': 'dog'},
142         ]
143     matches_mismatches = [
144         {},
145         {'foo': 'bar', 'baz': 'qux'},
146         {'foo': 'bop', 'baz': 'qux'},
147         {'foo': 'bar', 'cat': 'dog'},
148         {'foo': 'bar'},
149         ]
150
151     str_examples = [
152         ("ContainsDict({'baz': %s, 'foo': %s})" % (
153                 Not(Equals('qux')), Equals('bar')),
154          matches_matcher),
155         ]
156
157     describe_examples = [
158         ("Missing: {\n"
159          "  'baz': Not(Equals('qux')),\n"
160          "  'foo': Equals('bar'),\n"
161          "}",
162          {}, matches_matcher),
163         ("Differences: {\n"
164          "  'baz': 'qux' matches Equals('qux'),\n"
165          "}",
166          {'foo': 'bar', 'baz': 'qux'}, matches_matcher),
167         ("Differences: {\n"
168          "  'baz': 'qux' matches Equals('qux'),\n"
169          "  'foo': 'bar' != 'bop',\n"
170          "}",
171          {'foo': 'bop', 'baz': 'qux'}, matches_matcher),
172         ("Missing: {\n"
173          "  'baz': Not(Equals('qux')),\n"
174          "}",
175          {'foo': 'bar', 'cat': 'dog'}, matches_matcher),
176         ]
177
178
179 class TestContainedByDict(TestCase, TestMatchersInterface):
180
181     matches_matcher = ContainedByDict(
182         {'foo': Equals('bar'), 'baz': Not(Equals('qux'))})
183
184     matches_matches = [
185         {},
186         {'foo': 'bar'},
187         {'foo': 'bar', 'baz': 'quux'},
188         {'baz': 'quux'},
189         ]
190     matches_mismatches = [
191         {'foo': 'bar', 'baz': 'quux', 'cat': 'dog'},
192         {'foo': 'bar', 'baz': 'qux'},
193         {'foo': 'bop', 'baz': 'qux'},
194         {'foo': 'bar', 'cat': 'dog'},
195         ]
196
197     str_examples = [
198         ("ContainedByDict({'baz': %s, 'foo': %s})" % (
199                 Not(Equals('qux')), Equals('bar')),
200          matches_matcher),
201         ]
202
203     describe_examples = [
204         ("Differences: {\n"
205          "  'baz': 'qux' matches Equals('qux'),\n"
206          "}",
207          {'foo': 'bar', 'baz': 'qux'}, matches_matcher),
208         ("Differences: {\n"
209          "  'baz': 'qux' matches Equals('qux'),\n"
210          "  'foo': 'bar' != 'bop',\n"
211          "}",
212          {'foo': 'bop', 'baz': 'qux'}, matches_matcher),
213         ("Extra: {\n"
214          "  'cat': 'dog',\n"
215          "}",
216          {'foo': 'bar', 'cat': 'dog'}, matches_matcher),
217         ]
218
219
220 def test_suite():
221     from unittest import TestLoader
222     return TestLoader().loadTestsFromName(__name__)