File: haslength_test.py

package info (click to toggle)
pyhamcrest 2.1.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 844 kB
  • sloc: python: 4,081; makefile: 114; sh: 15
file content (73 lines) | stat: -rw-r--r-- 2,382 bytes parent folder | download | duplicates (3)
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
if __name__ == "__main__":
    import sys

    sys.path.insert(0, "..")
    sys.path.insert(0, "../..")

import unittest

from hamcrest.core.core.isequal import equal_to
from hamcrest.library.number.ordering_comparison import greater_than
from hamcrest.library.object.haslength import has_length
from hamcrest_unit_test.matcher_test import MatcherTest

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


class FakeWithLen(object):
    def __init__(self, len):
        self.len = len

    def __len__(self):
        return self.len

    def __str__(self):
        return "FakeWithLen"


class FakeWithoutLen(object):
    def __str__(self):
        return "FakeWithoutLen"


class HasLengthTest(MatcherTest):
    def testPassesResultOfLenToNestedMatcher(self):
        self.assert_matches("equal", has_length(equal_to(42)), FakeWithLen(42))
        self.assert_does_not_match("unequal", has_length(equal_to(42)), FakeWithLen(1))

    def testProvidesConvenientShortcutForHasLengthEqualTo(self):
        self.assert_matches("equal", has_length(42), FakeWithLen(42))
        self.assert_does_not_match("unequal", has_length(42), FakeWithLen(1))

    def testDoesNotMatchObjectWithoutLen(self):
        self.assert_does_not_match("no length", has_length(42), object())

    def testHasReadableDescription(self):
        self.assert_description(
            "an object with length of a value greater than <5>", has_length(greater_than(5))
        )

    def testSuccessfulMatchDoesNotGenerateMismatchDescription(self):
        self.assert_no_mismatch_description(has_length(3), "foo")

    def testMismatchDescriptionForItemWithWrongLength(self):
        self.assert_mismatch_description(
            "was <FakeWithLen> with length of <4>", has_length(3), FakeWithLen(4)
        )

    def testMismatchDescriptionForItemWithoutLength(self):
        self.assert_mismatch_description("was <FakeWithoutLen>", has_length(3), FakeWithoutLen())

    def testDescribeMismatchForItemWithWrongLength(self):
        self.assert_describe_mismatch(
            "was <FakeWithLen> with length of <4>", has_length(3), FakeWithLen(4)
        )

    def testDescribeMismatchForItemWithoutLength(self):
        self.assert_describe_mismatch("was <FakeWithoutLen>", has_length(3), FakeWithoutLen())


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