1
|
|
|
if __name__ == '__main__': |
2
|
|
|
import sys |
3
|
|
|
sys.path.insert(0, '..') |
4
|
|
|
sys.path.insert(0, '../..') |
5
|
|
|
|
6
|
|
|
from hamcrest.library.object.haslength import * |
7
|
|
|
|
8
|
|
|
from hamcrest.core.core.isequal import equal_to |
9
|
|
|
from hamcrest.library.number.ordering_comparison import greater_than |
10
|
|
|
from hamcrest_unit_test.matcher_test import MatcherTest |
11
|
|
|
import unittest |
12
|
|
|
|
13
|
|
|
__author__ = "Jon Reid" |
14
|
|
|
__copyright__ = "Copyright 2011 hamcrest.org" |
15
|
|
|
__license__ = "BSD, see License.txt" |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
class FakeWithLen(object): |
19
|
|
|
|
20
|
|
|
def __init__(self, len): |
21
|
|
|
self.len = len |
22
|
|
|
|
23
|
|
|
def __len__(self): |
24
|
|
|
return self.len |
25
|
|
|
|
26
|
|
|
def __str__(self): |
27
|
|
|
return 'FakeWithLen' |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
class FakeWithoutLen(object): |
31
|
|
|
|
32
|
|
|
def __str__(self): |
33
|
|
|
return 'FakeWithoutLen' |
34
|
|
|
|
35
|
|
|
|
36
|
|
|
class HasLengthTest(MatcherTest): |
37
|
|
|
|
38
|
|
|
def testPassesResultOfLenToNestedMatcher(self): |
39
|
|
|
self.assert_matches('equal', has_length(equal_to(42)), FakeWithLen(42)) |
40
|
|
|
self.assert_does_not_match('unequal', |
41
|
|
|
has_length(equal_to(42)), FakeWithLen(1)) |
42
|
|
|
|
43
|
|
|
def testProvidesConvenientShortcutForHasLengthEqualTo(self): |
44
|
|
|
self.assert_matches('equal', has_length(42), FakeWithLen(42)) |
45
|
|
|
self.assert_does_not_match('unequal', has_length(42), FakeWithLen(1)) |
46
|
|
|
|
47
|
|
|
def testDoesNotMatchObjectWithoutLen(self): |
48
|
|
|
self.assert_does_not_match('no length', has_length(42), object()) |
49
|
|
|
|
50
|
|
|
def testHasReadableDescription(self): |
51
|
|
|
self.assert_description('an object with length of a value greater than <5>', |
52
|
|
|
has_length(greater_than(5))) |
53
|
|
|
|
54
|
|
|
def testSuccessfulMatchDoesNotGenerateMismatchDescription(self): |
55
|
|
|
self.assert_no_mismatch_description(has_length(3), 'foo') |
56
|
|
|
|
57
|
|
|
def testMismatchDescriptionForItemWithWrongLength(self): |
58
|
|
|
self.assert_mismatch_description('was <FakeWithLen> with length of <4>', |
59
|
|
|
has_length(3), FakeWithLen(4)) |
60
|
|
|
|
61
|
|
|
def testMismatchDescriptionForItemWithoutLength(self): |
62
|
|
|
self.assert_mismatch_description("was <FakeWithoutLen>", |
63
|
|
|
has_length(3), FakeWithoutLen()) |
64
|
|
|
|
65
|
|
|
def testDescribeMismatchForItemWithWrongLength(self): |
66
|
|
|
self.assert_describe_mismatch('was <FakeWithLen> with length of <4>', |
67
|
|
|
has_length(3), FakeWithLen(4)) |
68
|
|
|
|
69
|
|
|
def testDescribeMismatchForItemWithoutLength(self): |
70
|
|
|
self.assert_describe_mismatch("was <FakeWithoutLen>", |
71
|
|
|
has_length(3), FakeWithoutLen()) |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
|
75
|
|
|
if __name__ == '__main__': |
76
|
|
|
unittest.main() |
77
|
|
|
|