File: isdict_containingentries_test.py

package info (click to toggle)
pyhamcrest 1.6-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 556 kB
  • sloc: python: 2,938; makefile: 2
file content (125 lines) | stat: -rw-r--r-- 5,688 bytes parent folder | download
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
if __name__ == '__main__':
    import sys
    sys.path.insert(0, '..')
    sys.path.insert(0, '../..')

from hamcrest.library.collection.isdict_containingentries import *

from hamcrest.core.core.isequal import equal_to
from hamcrest_unit_test.matcher_test import MatcherTest
import unittest

__author__ = "Jon Reid"
__copyright__ = "Copyright 2011 hamcrest.org"
__license__ = "BSD, see License.txt"


class IsDictContainingEntriesTest(MatcherTest):

    def testMatcherCreationRequiresEvenNumberOfPositionalArgs(self):
        self.assertRaises(ValueError, has_entries, 'a', 'b', 'c')

    def testDoesNotMatchNonDictionary(self):
        self.assert_does_not_match('non-dictionary',
                                    has_entries('a', equal_to(1)), object())

    def testMatchesDictLike(self):
        class DictLike(object):
            def __getitem__(self, key):
                return 'value: ' + str(key)
            def __contains__(self, key):
                return True
        self.assert_matches('matches a dictionary-like object',
                            has_entries('a', equal_to('value: a')),
                            DictLike())

    def testMatchesUsingSingleDictionaryArgument(self):
        target = {'a': 1, 'b': 2, 'c': 3}
        self.assert_matches('has a & b',
                        has_entries({'a':equal_to(1), 'b':equal_to(2)}), target)
        self.assert_matches('has c & a',
                        has_entries({'c':equal_to(3), 'a':equal_to(1)}), target)
        self.assert_does_not_match('no d:3',
                        has_entries({'b':equal_to(2), 'd':equal_to(3)}), target)

    def testMatcheSingleDictionaryArgumentWithImplicitEqualTo(self):
        target = {'a': 1, 'b': 2, 'c': 3}
        self.assert_matches('has a & b',
                        has_entries({'a':1, 'b':2}), target)
        self.assert_matches('has c & a',
                        has_entries({'c':3, 'a':1}), target)
        self.assert_does_not_match('no d:3',
                        has_entries({'b':2, 'd': 3}), target)

    def testMatchesUsingKwargs(self):
        target = {'a': 1, 'b': 2, 'c': 3}
        self.assert_matches('has a & b',
                        has_entries(a=equal_to(1), b=equal_to(2)), target)
        self.assert_matches('has c & a',
                        has_entries(c=equal_to(3), a=equal_to(1)), target)
        self.assert_does_not_match('no d:3',
                        has_entries(b=equal_to(2), d=equal_to(3)), target)

    def testMatchesKwargsWithImplicitEqualTo(self):
        target = {'a': 1, 'b': 2, 'c': 3}
        self.assert_matches('has a & b',
                        has_entries(a=1, b=2), target)
        self.assert_matches('has c & a',
                        has_entries(c=3, a=1), target)
        self.assert_does_not_match('no d:3',
                        has_entries(b=2, d=3), target)

    def testMatchesDictionaryContainingSingleKeyWithMatchingValue(self):
        target = {'a': 1, 'b': 2}
        self.assert_matches('has a:1', has_entries('a', equal_to(1)), target)
        self.assert_matches('has b:2', has_entries('b', equal_to(2)), target)
        self.assert_does_not_match('no b:3', has_entries('b', equal_to(3)), target)
        self.assert_does_not_match('no c:2', has_entries('c', equal_to(2)), target)

    def testMatchesDictionaryContainingMultipleKeysWithMatchingValues(self):
        target = {'a': 1, 'b': 2, 'c': 3}
        self.assert_matches('has a & b',
                        has_entries('a', equal_to(1), 'b', equal_to(2)), target)
        self.assert_matches('has c & a',
                        has_entries('c', equal_to(3), 'a', equal_to(1)), target)
        self.assert_does_not_match('no d:3',
                        has_entries('b', equal_to(3), 'd', equal_to(3)), target)

    def testProvidesConvenientShortcutForMatchingWithEqualTo(self):
        target = {'a': 1, 'b': 2, 'c': 3}
        self.assert_matches('has a & b', has_entries('a', 1, 'b', 2), target)
        self.assert_matches('has c & a', has_entries('c', 3, 'a', 1), target)
        self.assert_does_not_match('no d:4', has_entries('b', 3, 'd', 4), target)

    def testHasReadableDescription(self):
        self.assert_description("a dictionary containing {'a': <1>, 'b': <2>}",
                                has_entries('a', 1, 'b', 2))

    def testSuccessfulMatchDoesNotGenerateMismatchDescription(self):
        self.assert_no_mismatch_description(has_entries('a', 1), {'a': 1})

    def testMismatchDescriptionOfNonDictionaryShowsActualArgument(self):
        self.assert_mismatch_description("'bad' is not a mapping object", has_entries('a', 1), 'bad')

    def testMismatchDescriptionOfDictionaryWithoutKey(self):
        self.assert_mismatch_description("no 'b' key in <{'a': 1, 'c': 3}>",
                                has_entries('a', 1, 'b', 2), {'a': 1, 'c': 3})

    def testMismatchDescriptionOfDictionaryWithNonMatchingValue(self):
        self.assert_mismatch_description("value for 'a' was <2>",
                                has_entries('a', 1), {'a': 2})

    def testDescribeMismatchOfNonDictionaryShowsActualArgument(self):
        self.assert_describe_mismatch("'bad' is not a mapping object", has_entries('a', 1), 'bad')

    def testDescribeMismatchOfDictionaryWithoutKey(self):
        self.assert_describe_mismatch("no 'b' key in <{'a': 1, 'c': 3}>",
                                has_entries('a', 1, 'b', 2), {'a': 1, 'c': 3})

    def testDescribeMismatchOfDictionaryWithNonMatchingValue(self):
        self.assert_describe_mismatch("value for 'a' was <2>",
                                has_entries('a', 1), {'a': 2})


if __name__ == '__main__':
    unittest.main()