1
|
|
|
from __future__ import absolute_import |
2
|
|
|
|
3
|
|
|
from hamcrest.library.collection.issequence_containing import * |
4
|
|
|
|
5
|
|
|
from hamcrest.core.core.isequal import equal_to |
6
|
|
|
from hamcrest_unit_test.matcher_test import MatcherTest |
7
|
|
|
from .quasisequence import QuasiSequence |
8
|
|
|
from .sequencemixin import GeneratorForm, SequenceForm |
9
|
|
|
import unittest |
10
|
|
|
|
11
|
|
|
__author__ = "Jon Reid" |
12
|
|
|
__copyright__ = "Copyright 2011 hamcrest.org" |
13
|
|
|
__license__ = "BSD, see License.txt" |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
class IsSequenceContainingTestBase(object): |
17
|
|
|
|
18
|
|
|
def testMatchesASequenceThatContainsAnElementMatchingTheGivenMatcher(self): |
19
|
|
|
self.assert_matches("sequence contains 'a'", |
20
|
|
|
has_item(equal_to('a')), self._sequence('a', 'b', 'c')) |
21
|
|
|
|
22
|
|
|
def testNoMatchIfSequenceDoesntContainAnElementMatchingTheGivenMatcher(self): |
23
|
|
|
self.assert_does_not_match("sequence without 'a'", |
24
|
|
|
has_item(equal_to('a')), self._sequence('b', 'c')) |
25
|
|
|
self.assert_does_not_match('empty', has_item(equal_to('a')), []) |
26
|
|
|
|
27
|
|
|
def testProvidesConvenientShortcutForMatchingWithEqualTo(self): |
28
|
|
|
self.assert_matches("sequence contains 'a'", |
29
|
|
|
has_item('a'), self._sequence('a', 'b', 'c')) |
30
|
|
|
self.assert_does_not_match("sequence without 'a'", |
31
|
|
|
has_item('a'), self._sequence('b', 'c')) |
32
|
|
|
|
33
|
|
|
def testMatchesAnyConformingSequence(self): |
34
|
|
|
self.assert_matches('quasi-sequence', has_item(1), QuasiSequence()) |
35
|
|
|
self.assert_does_not_match('non-sequence', has_item(1), object()) |
36
|
|
|
|
37
|
|
|
def testHasAReadableDescription(self): |
38
|
|
|
self.assert_description("a sequence containing 'a'", has_item('a')) |
39
|
|
|
|
40
|
|
|
def testSuccessfulMatchDoesNotGenerateMismatchDescription(self): |
41
|
|
|
self.assert_no_mismatch_description(has_item('a'), self._sequence('a', 'b')) |
42
|
|
|
|
43
|
|
|
def testMismatchDescriptionShowsActualArgument(self): |
44
|
|
|
self.assert_mismatch_description("was <42>", has_item('a'), 42) |
45
|
|
|
|
46
|
|
|
def testDescribeMismatch(self): |
47
|
|
|
self.assert_describe_mismatch("was <42>", has_item('a'), 42) |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
class IsConcreteSequenceContaining(MatcherTest, SequenceForm, IsSequenceContainingTestBase): |
51
|
|
|
pass |
52
|
|
|
|
53
|
|
|
class IsGeneratorContaining(MatcherTest, GeneratorForm, IsSequenceContainingTestBase): |
54
|
|
|
pass |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
class IsSequenceContainingItemsTestBase(object): |
58
|
|
|
|
59
|
|
|
def testShouldMatchCollectionContainingAllItems(self): |
60
|
|
|
self.assert_matches('contains all items', |
61
|
|
|
has_items(equal_to('a'), equal_to('b'), equal_to('c')), |
62
|
|
|
self._sequence('a', 'b', 'c')) |
63
|
|
|
|
64
|
|
|
def testProvidesConvenientShortcutForMatchingWithEqualTo(self): |
65
|
|
|
self.assert_matches('Values automatically wrapped with equal_to', |
66
|
|
|
has_items('a', 'b', 'c'), |
67
|
|
|
self._sequence('a', 'b', 'c')) |
68
|
|
|
|
69
|
|
|
def testShouldMatchCollectionContainingAllItemsInDifferentOrder(self): |
70
|
|
|
self.assert_matches('all items in different order', |
71
|
|
|
has_items('a', 'b', 'c'), |
72
|
|
|
self._sequence('c', 'b', 'a')) |
73
|
|
|
|
74
|
|
|
def testShouldMatchCollectionContainingAllItemsPlusExtras(self): |
75
|
|
|
self.assert_matches('all items plus extras', |
76
|
|
|
has_items('a', 'b', 'c'), |
77
|
|
|
self._sequence('e', 'c', 'b', 'a', 'd')) |
78
|
|
|
|
79
|
|
|
def testNoMatchIfCollectionDoesntSatisfyAllMatchers(self): |
80
|
|
|
self.assert_does_not_match("missing 'a'", |
81
|
|
|
has_items('a', 'b', 'c'), |
82
|
|
|
self._sequence('e', 'c', 'b', 'd')) |
83
|
|
|
|
84
|
|
|
def testHasAReadableDescription(self): |
85
|
|
|
self.assert_description( |
86
|
|
|
"(a sequence containing 'a' and a sequence containing 'b')", |
87
|
|
|
has_items('a', 'b')) |
88
|
|
|
|
89
|
|
|
def testSuccessfulMatchDoesNotGenerateMismatchDescription(self): |
90
|
|
|
self.assert_no_mismatch_description(has_items('a', 'b'), self._sequence('a', 'b')) |
91
|
|
|
|
92
|
|
|
def testMismatchDescriptionShowsFirstUnmetMatcherAndActualArgument(self): |
93
|
|
|
self.assert_mismatch_description("a sequence containing 'a' was <42>", |
94
|
|
|
has_items('a', 'b'), 42) |
95
|
|
|
|
96
|
|
|
def testDescribeMismatch(self): |
97
|
|
|
self.assert_describe_mismatch("a sequence containing 'a' was <42>", |
98
|
|
|
has_items('a', 'b'), 42) |
99
|
|
|
|
100
|
|
|
|
101
|
|
|
class IsConcreteSequenceContainingItemsTest(MatcherTest, IsSequenceContainingItemsTestBase, SequenceForm): |
102
|
|
|
pass |
103
|
|
|
|
104
|
|
|
class IsGeneratorSequenceContainingItemsTest(MatcherTest, IsSequenceContainingItemsTestBase, GeneratorForm): |
105
|
|
|
pass |
106
|
|
|
|
107
|
|
|
if __name__ == '__main__': |
108
|
|
|
unittest.main() |
109
|
|
|
|