File: assert_that_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 (78 lines) | stat: -rw-r--r-- 2,275 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
74
75
76
77
78
# encoding: utf-8
import unittest
import warnings

from hamcrest.core.assert_that import assert_that
from hamcrest.core.core.isequal import equal_to


def u(x):
    return x


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


class AssertThatTest(unittest.TestCase):
    def testShouldBeSilentOnSuccessfulMatch(self):
        assert_that(1, equal_to(1))

    def testAssertionErrorShouldDescribeExpectedAndActual(self):
        expected = "EXPECTED"
        actual = "ACTUAL"

        expectedMessage = "\nExpected: 'EXPECTED'\n     but: was 'ACTUAL'\n"

        with self.assertRaises(AssertionError) as e:
            assert_that(actual, equal_to(expected))

        self.assertEqual(expectedMessage, str(e.exception))

    def testAssertionErrorShouldIncludeOptionalReason(self):
        expected = "EXPECTED"
        actual = "ACTUAL"

        expectedMessage = "REASON\nExpected: 'EXPECTED'\n     but: was 'ACTUAL'\n"

        with self.assertRaises(AssertionError) as e:
            assert_that(actual, equal_to(expected), "REASON")

        self.assertEqual(expectedMessage, str(e.exception))

    def testAssertionUnicodeEncodesProperly(self):
        expected = "EXPECTED"
        actual = u("\xdcnic\N{Latin Small Letter O with diaeresis}de")

        with self.assertRaises(AssertionError):
            assert_that(actual, equal_to(expected), "REASON")

    def testCanTestBoolDirectly(self):
        assert_that(True, "should accept True")

        with self.assertRaises(AssertionError) as e:
            assert_that(False, "FAILURE REASON")
        self.assertEqual("FAILURE REASON", str(e.exception))

    def testCanTestBoolDirectlyWithoutReason(self):
        assert_that(True)

        with self.assertRaises(AssertionError) as e:
            assert_that(False)

        self.assertEqual("Assertion failed", str(e.exception))

    def testWarnsForMatcherAsArg1(self):
        assert_that(True)

        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter("always")
            assert_that(equal_to(1))

            self.assertEqual(len(w), 1)
            self.assertTrue("arg1 should be boolean" in str(w[-1].message))


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