1
|
|
|
if __name__ == '__main__': |
2
|
|
|
import sys |
3
|
|
|
sys.path.insert(0, '..') |
4
|
|
|
sys.path.insert(0, '../..') |
5
|
|
|
|
6
|
|
|
from hamcrest.library.collection.isdict_containingentries import * |
7
|
|
|
|
8
|
|
|
import platform |
9
|
|
|
|
10
|
|
|
from hamcrest.core.core.isequal import equal_to |
11
|
|
|
from hamcrest_unit_test.matcher_test import MatcherTest |
12
|
|
|
try: |
13
|
|
|
from unittest import skipIf |
14
|
|
|
import unittest |
15
|
|
|
except ImportError: |
16
|
|
|
import unittest2 as unittest |
17
|
|
|
|
18
|
|
|
__author__ = "Jon Reid" |
19
|
|
|
__copyright__ = "Copyright 2011 hamcrest.org" |
20
|
|
|
__license__ = "BSD, see License.txt" |
21
|
|
|
|
22
|
|
|
|
23
|
|
|
class IsDictContainingEntriesTest(MatcherTest): |
24
|
|
|
|
25
|
|
|
def testMatcherCreationRequiresEvenNumberOfPositionalArgs(self): |
26
|
|
|
self.assertRaises(ValueError, has_entries, 'a', 'b', 'c') |
27
|
|
|
|
28
|
|
|
def testDoesNotMatchNonDictionary(self): |
29
|
|
|
self.assert_does_not_match('non-dictionary', |
30
|
|
|
has_entries('a', equal_to(1)), object()) |
31
|
|
|
|
32
|
|
|
def testMatchesDictLike(self): |
33
|
|
|
class DictLike(object): |
34
|
|
|
def __getitem__(self, key): |
35
|
|
|
return 'value: ' + str(key) |
36
|
|
|
def __contains__(self, key): |
37
|
|
|
return True |
38
|
|
|
self.assert_matches('matches a dictionary-like object', |
39
|
|
|
has_entries('a', equal_to('value: a')), |
40
|
|
|
DictLike()) |
41
|
|
|
|
42
|
|
|
def testMatchesUsingSingleDictionaryArgument(self): |
43
|
|
|
target = {'a': 1, 'b': 2, 'c': 3} |
44
|
|
|
self.assert_matches('has a & b', |
45
|
|
|
has_entries({'a':equal_to(1), 'b':equal_to(2)}), target) |
46
|
|
|
self.assert_matches('has c & a', |
47
|
|
|
has_entries({'c':equal_to(3), 'a':equal_to(1)}), target) |
48
|
|
|
self.assert_does_not_match('no d:3', |
49
|
|
|
has_entries({'b':equal_to(2), 'd':equal_to(3)}), target) |
50
|
|
|
|
51
|
|
|
def testMatcheSingleDictionaryArgumentWithImplicitEqualTo(self): |
52
|
|
|
target = {'a': 1, 'b': 2, 'c': 3} |
53
|
|
|
self.assert_matches('has a & b', |
54
|
|
|
has_entries({'a':1, 'b':2}), target) |
55
|
|
|
self.assert_matches('has c & a', |
56
|
|
|
has_entries({'c':3, 'a':1}), target) |
57
|
|
|
self.assert_does_not_match('no d:3', |
58
|
|
|
has_entries({'b':2, 'd': 3}), target) |
59
|
|
|
|
60
|
|
|
def testMatchesUsingKwargs(self): |
61
|
|
|
target = {'a': 1, 'b': 2, 'c': 3} |
62
|
|
|
self.assert_matches('has a & b', |
63
|
|
|
has_entries(a=equal_to(1), b=equal_to(2)), target) |
64
|
|
|
self.assert_matches('has c & a', |
65
|
|
|
has_entries(c=equal_to(3), a=equal_to(1)), target) |
66
|
|
|
self.assert_does_not_match('no d:3', |
67
|
|
|
has_entries(b=equal_to(2), d=equal_to(3)), target) |
68
|
|
|
|
69
|
|
|
def testMatchesKwargsWithImplicitEqualTo(self): |
70
|
|
|
target = {'a': 1, 'b': 2, 'c': 3} |
71
|
|
|
self.assert_matches('has a & b', |
72
|
|
|
has_entries(a=1, b=2), target) |
73
|
|
|
self.assert_matches('has c & a', |
74
|
|
|
has_entries(c=3, a=1), target) |
75
|
|
|
self.assert_does_not_match('no d:3', |
76
|
|
|
has_entries(b=2, d=3), target) |
77
|
|
|
|
78
|
|
|
def testMatchesDictionaryContainingSingleKeyWithMatchingValue(self): |
79
|
|
|
target = {'a': 1, 'b': 2} |
80
|
|
|
self.assert_matches('has a:1', has_entries('a', equal_to(1)), target) |
81
|
|
|
self.assert_matches('has b:2', has_entries('b', equal_to(2)), target) |
82
|
|
|
self.assert_does_not_match('no b:3', has_entries('b', equal_to(3)), target) |
83
|
|
|
self.assert_does_not_match('no c:2', has_entries('c', equal_to(2)), target) |
84
|
|
|
|
85
|
|
|
def testMatchesDictionaryContainingMultipleKeysWithMatchingValues(self): |
86
|
|
|
target = {'a': 1, 'b': 2, 'c': 3} |
87
|
|
|
self.assert_matches('has a & b', |
88
|
|
|
has_entries('a', equal_to(1), 'b', equal_to(2)), target) |
89
|
|
|
self.assert_matches('has c & a', |
90
|
|
|
has_entries('c', equal_to(3), 'a', equal_to(1)), target) |
91
|
|
|
self.assert_does_not_match('no d:3', |
92
|
|
|
has_entries('b', equal_to(3), 'd', equal_to(3)), target) |
93
|
|
|
|
94
|
|
|
def testProvidesConvenientShortcutForMatchingWithEqualTo(self): |
95
|
|
|
target = {'a': 1, 'b': 2, 'c': 3} |
96
|
|
|
self.assert_matches('has a & b', has_entries('a', 1, 'b', 2), target) |
97
|
|
|
self.assert_matches('has c & a', has_entries('c', 3, 'a', 1), target) |
98
|
|
|
self.assert_does_not_match('no d:4', has_entries('b', 3, 'd', 4), target) |
99
|
|
|
|
100
|
|
|
def testHasReadableDescription(self): |
101
|
|
|
self.assert_description("a dictionary containing {'a': <1>, 'b': <2>}", |
102
|
|
|
has_entries('a', 1, 'b', 2)) |
103
|
|
|
|
104
|
|
|
def testSuccessfulMatchDoesNotGenerateMismatchDescription(self): |
105
|
|
|
self.assert_no_mismatch_description(has_entries('a', 1), {'a': 1}) |
106
|
|
|
|
107
|
|
|
def testMismatchDescriptionOfNonDictionaryShowsActualArgument(self): |
108
|
|
|
self.assert_mismatch_description("'bad' is not a mapping object", has_entries('a', 1), 'bad') |
109
|
|
|
|
110
|
|
|
def testMismatchDescriptionOfDictionaryWithNonMatchingValue(self): |
111
|
|
|
self.assert_mismatch_description("value for 'a' was <2>", |
112
|
|
|
has_entries('a', 1), {'a': 2}) |
113
|
|
|
|
114
|
|
|
def testDescribeMismatchOfNonDictionaryShowsActualArgument(self): |
115
|
|
|
self.assert_describe_mismatch("'bad' is not a mapping object", has_entries('a', 1), 'bad') |
116
|
|
|
|
117
|
|
|
def testDescribeMismatchOfDictionaryWithoutKey(self): |
118
|
|
|
d = {'a': 1, 'c': 3} |
119
|
|
|
self.assert_describe_mismatch("no 'b' key in <%s>" % (d, ), |
120
|
|
|
has_entries('a', 1, 'b', 2), d) |
121
|
|
|
|
122
|
|
|
def testDescribeMismatchOfDictionaryWithNonMatchingValue(self): |
123
|
|
|
self.assert_describe_mismatch("value for 'a' was <2>", |
124
|
|
|
has_entries('a', 1), {'a': 2}) |
125
|
|
|
|
126
|
|
|
|
127
|
|
|
if __name__ == '__main__': |
128
|
|
|
unittest.main() |
129
|
|
|
|