1
|
|
|
from __future__ import absolute_import |
2
|
|
|
|
3
|
|
|
from hamcrest.library.collection.issequence_onlycontaining import * |
4
|
|
|
|
5
|
|
|
from hamcrest.core.core.isequal import equal_to |
6
|
|
|
from hamcrest.library.number.ordering_comparison import less_than |
7
|
|
|
from hamcrest_unit_test.matcher_test import MatcherTest |
8
|
|
|
from .quasisequence import QuasiSequence |
9
|
|
|
from .sequencemixin import SequenceForm, GeneratorForm |
10
|
|
|
import unittest |
11
|
|
|
|
12
|
|
|
__author__ = "Jon Reid" |
13
|
|
|
__copyright__ = "Copyright 2011 hamcrest.org" |
14
|
|
|
__license__ = "BSD, see License.txt" |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
class IsSequenceOnlyContainingTestBase(object): |
18
|
|
|
|
19
|
|
|
def testMatchesSingletonList(self): |
20
|
|
|
self.assert_matches('singleton list', only_contains(equal_to(1)), self._sequence(1)) |
21
|
|
|
|
22
|
|
|
def testMatchesAllItemsWithOneMatcher(self): |
23
|
|
|
self.assert_matches('one matcher', |
24
|
|
|
only_contains(less_than(3)), self._sequence(0, 1, 2)) |
25
|
|
|
|
26
|
|
|
def testMatchesAllItemsWithMultipleMatchers(self): |
27
|
|
|
self.assert_matches('multiple matchers', |
28
|
|
|
only_contains(less_than(3), equal_to(7)), |
29
|
|
|
self._sequence(0, 7, 1, 2)) |
30
|
|
|
|
31
|
|
|
def testProvidesConvenientShortcutForMatchingWithEqualTo(self): |
32
|
|
|
self.assert_matches('Values automatically wrapped with equal_to', |
33
|
|
|
only_contains(less_than(3), 7), |
34
|
|
|
self._sequence(0, 7, 1, 2)) |
35
|
|
|
|
36
|
|
|
def testDoesNotMatchListWithMismatchingItem(self): |
37
|
|
|
self.assert_does_not_match('3 is not less than 3', |
38
|
|
|
only_contains(less_than(3)), self._sequence(1, 2, 3)) |
39
|
|
|
|
40
|
|
|
def testDoesNotMatchEmptyList(self): |
41
|
|
|
self.assert_does_not_match('empty', only_contains('foo'), self._sequence()) |
42
|
|
|
|
43
|
|
|
def testMatchesAnyConformingSequence(self): |
44
|
|
|
class ObjectWithLenOnly(object): |
45
|
|
|
def __len__(self): |
46
|
|
|
return 20 |
47
|
|
|
|
48
|
|
|
self.assert_matches('quasi-sequence', |
49
|
|
|
only_contains(less_than(3)), QuasiSequence()) |
50
|
|
|
self.assert_does_not_match('non-sequence', only_contains(1), object()) |
51
|
|
|
self.assert_does_not_match('non-sequence with length', |
52
|
|
|
only_contains(1), ObjectWithLenOnly()) |
53
|
|
|
|
54
|
|
|
def testHasAReadableDescription(self): |
55
|
|
|
self.assert_description('a sequence containing items matching (<1> or <2>)', |
56
|
|
|
only_contains(1,2)) |
57
|
|
|
|
58
|
|
|
def testDescribeMismatch(self): |
59
|
|
|
self.assert_describe_mismatch("was 'bad'", only_contains(1,2), 'bad') |
60
|
|
|
|
61
|
|
|
def testDescribeMismatchOfNonSequence(self): |
62
|
|
|
self.assert_describe_mismatch("was <3>", only_contains(1,2), 3) |
63
|
|
|
|
64
|
|
|
|
65
|
|
|
class IsConcreteSequenceOnlyContainingTest(MatcherTest, IsSequenceOnlyContainingTestBase, SequenceForm): |
66
|
|
|
pass |
67
|
|
|
|
68
|
|
|
class IsGeneratorSequenceOnlyContainingTest(MatcherTest, IsSequenceOnlyContainingTestBase, GeneratorForm): |
69
|
|
|
pass |
70
|
|
|
|
71
|
|
|
|
72
|
|
|
if __name__ == '__main__': |
73
|
|
|
unittest.main() |
74
|
|
|
|