1 from testtools import TestCase
2 from testtools.matchers import (
7 from testtools.matchers._dict import (
15 from testtools.tests.matchers.helpers import TestMatchersInterface
18 class TestMatchesAllDictInterface(TestCase, TestMatchersInterface):
20 matches_matcher = MatchesAllDict({'a': NotEquals(1), 'b': NotEquals(2)})
21 matches_matches = [3, 4]
22 matches_mismatches = [1, 2]
25 ("MatchesAllDict({'a': NotEquals(1), 'b': NotEquals(2)})",
29 ("""a: 1 == 1""", 1, matches_matcher),
33 class TestKeysEqual(TestCase, TestMatchersInterface):
35 matches_matcher = KeysEqual('foo', 'bar')
39 matches_mismatches = [
43 {'foo': 0, 'bar': 1, 'baz': 2},
44 {'a': None, 'b': None, 'c': None},
48 ("KeysEqual('foo', 'bar')", KeysEqual('foo', 'bar')),
51 describe_examples = []
53 def test_description(self):
54 matchee = {'foo': 0, 'bar': 1, 'baz': 2}
55 mismatch = KeysEqual('foo', 'bar').match(matchee)
56 description = mismatch.describe()
59 "['bar', 'foo'] does not match %r: Keys not equal"
63 class TestSubDictOf(TestCase, TestMatchersInterface):
65 matches_matcher = _SubDictOf({'foo': 'bar', 'baz': 'qux'})
68 {'foo': 'bar', 'baz': 'qux'},
72 matches_mismatches = [
73 {'foo': 'bar', 'baz': 'qux', 'cat': 'dog'},
74 {'foo': 'bar', 'cat': 'dog'},
78 describe_examples = []
81 class TestMatchesDict(TestCase, TestMatchersInterface):
83 matches_matcher = MatchesDict(
84 {'foo': Equals('bar'), 'baz': Not(Equals('qux'))})
87 {'foo': 'bar', 'baz': None},
88 {'foo': 'bar', 'baz': 'quux'},
90 matches_mismatches = [
92 {'foo': 'bar', 'baz': 'qux'},
93 {'foo': 'bop', 'baz': 'qux'},
94 {'foo': 'bar', 'baz': 'quux', 'cat': 'dog'},
95 {'foo': 'bar', 'cat': 'dog'},
99 ("MatchesDict({'baz': %s, 'foo': %s})" % (
100 Not(Equals('qux')), Equals('bar')),
104 describe_examples = [
106 " 'baz': Not(Equals('qux')),\n"
107 " 'foo': Equals('bar'),\n"
109 {}, matches_matcher),
111 " 'baz': 'qux' matches Equals('qux'),\n"
113 {'foo': 'bar', 'baz': 'qux'}, matches_matcher),
115 " 'baz': 'qux' matches Equals('qux'),\n"
116 " 'foo': 'bar' != 'bop',\n"
118 {'foo': 'bop', 'baz': 'qux'}, matches_matcher),
122 {'foo': 'bar', 'baz': 'quux', 'cat': 'dog'}, matches_matcher),
127 " 'baz': Not(Equals('qux')),\n"
129 {'foo': 'bar', 'cat': 'dog'}, matches_matcher),
133 class TestContainsDict(TestCase, TestMatchersInterface):
135 matches_matcher = ContainsDict(
136 {'foo': Equals('bar'), 'baz': Not(Equals('qux'))})
139 {'foo': 'bar', 'baz': None},
140 {'foo': 'bar', 'baz': 'quux'},
141 {'foo': 'bar', 'baz': 'quux', 'cat': 'dog'},
143 matches_mismatches = [
145 {'foo': 'bar', 'baz': 'qux'},
146 {'foo': 'bop', 'baz': 'qux'},
147 {'foo': 'bar', 'cat': 'dog'},
152 ("ContainsDict({'baz': %s, 'foo': %s})" % (
153 Not(Equals('qux')), Equals('bar')),
157 describe_examples = [
159 " 'baz': Not(Equals('qux')),\n"
160 " 'foo': Equals('bar'),\n"
162 {}, matches_matcher),
164 " 'baz': 'qux' matches Equals('qux'),\n"
166 {'foo': 'bar', 'baz': 'qux'}, matches_matcher),
168 " 'baz': 'qux' matches Equals('qux'),\n"
169 " 'foo': 'bar' != 'bop',\n"
171 {'foo': 'bop', 'baz': 'qux'}, matches_matcher),
173 " 'baz': Not(Equals('qux')),\n"
175 {'foo': 'bar', 'cat': 'dog'}, matches_matcher),
179 class TestContainedByDict(TestCase, TestMatchersInterface):
181 matches_matcher = ContainedByDict(
182 {'foo': Equals('bar'), 'baz': Not(Equals('qux'))})
187 {'foo': 'bar', 'baz': 'quux'},
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'},
198 ("ContainedByDict({'baz': %s, 'foo': %s})" % (
199 Not(Equals('qux')), Equals('bar')),
203 describe_examples = [
205 " 'baz': 'qux' matches Equals('qux'),\n"
207 {'foo': 'bar', 'baz': 'qux'}, matches_matcher),
209 " 'baz': 'qux' matches Equals('qux'),\n"
210 " 'foo': 'bar' != 'bop',\n"
212 {'foo': 'bop', 'baz': 'qux'}, matches_matcher),
216 {'foo': 'bar', 'cat': 'dog'}, matches_matcher),
221 from unittest import TestLoader
222 return TestLoader().loadTestsFromName(__name__)