|
1
|
|
|
# encoding: utf-8 |
|
2
|
|
|
from __future__ import with_statement |
|
3
|
|
|
from hamcrest.core.assert_that import assert_that |
|
4
|
|
|
from hamcrest.core.core.isequal import equal_to |
|
5
|
|
|
try: |
|
6
|
|
|
import unittest2 as unittest |
|
7
|
|
|
except ImportError: |
|
8
|
|
|
import unittest |
|
9
|
|
|
|
|
10
|
|
|
import sys |
|
11
|
|
|
if sys.version < '3': |
|
12
|
|
|
import codecs |
|
13
|
|
|
def u(x): |
|
14
|
|
|
return codecs.unicode_escape_decode(x)[0] |
|
15
|
|
|
else: |
|
16
|
|
|
def u(x): |
|
17
|
|
|
return x |
|
18
|
|
|
|
|
19
|
|
|
__author__ = "Jon Reid" |
|
20
|
|
|
__copyright__ = "Copyright 2011 hamcrest.org" |
|
21
|
|
|
__license__ = "BSD, see License.txt" |
|
22
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
class AssertThatTest(unittest.TestCase): |
|
25
|
|
|
|
|
26
|
|
|
def testShouldBeSilentOnSuccessfulMatch(self): |
|
27
|
|
|
assert_that(1, equal_to(1)) |
|
28
|
|
|
|
|
29
|
|
|
def testAssertionErrorShouldDescribeExpectedAndActual(self): |
|
30
|
|
|
expected = 'EXPECTED' |
|
31
|
|
|
actual = 'ACTUAL' |
|
32
|
|
|
|
|
33
|
|
|
expectedMessage = "\nExpected: 'EXPECTED'\n but: was 'ACTUAL'\n" |
|
34
|
|
|
|
|
35
|
|
|
with self.assertRaises(AssertionError) as e: |
|
36
|
|
|
assert_that(actual, equal_to(expected)) |
|
37
|
|
|
|
|
38
|
|
|
self.assertEqual(expectedMessage, str(e.exception)) |
|
39
|
|
|
|
|
40
|
|
|
def testAssertionErrorShouldIncludeOptionalReason(self): |
|
41
|
|
|
expected = 'EXPECTED' |
|
42
|
|
|
actual = 'ACTUAL' |
|
43
|
|
|
|
|
44
|
|
|
expectedMessage = "REASON\nExpected: 'EXPECTED'\n but: was 'ACTUAL'\n" |
|
45
|
|
|
|
|
46
|
|
|
with self.assertRaises(AssertionError) as e: |
|
47
|
|
|
assert_that(actual, equal_to(expected), 'REASON') |
|
48
|
|
|
|
|
49
|
|
|
self.assertEqual(expectedMessage, str(e.exception)) |
|
50
|
|
|
|
|
51
|
|
|
def testAssertionUnicodeEncodesProperly(self): |
|
52
|
|
|
expected = 'EXPECTED' |
|
53
|
|
|
actual = u('\xdcnic\N{Latin Small Letter O with diaeresis}de') |
|
54
|
|
|
|
|
55
|
|
|
with self.assertRaises(AssertionError): |
|
56
|
|
|
assert_that(actual, equal_to(expected), 'REASON') |
|
57
|
|
|
|
|
58
|
|
|
def testCanTestBoolDirectly(self): |
|
59
|
|
|
assert_that(True, 'should accept True') |
|
60
|
|
|
|
|
61
|
|
|
with self.assertRaises(AssertionError) as e: |
|
62
|
|
|
assert_that(False, 'FAILURE REASON') |
|
63
|
|
|
self.assertEqual('FAILURE REASON', str(e.exception)) |
|
64
|
|
|
|
|
65
|
|
|
def testCanTestBoolDirectlyWithoutReason(self): |
|
66
|
|
|
assert_that(True) |
|
67
|
|
|
|
|
68
|
|
|
with self.assertRaises(AssertionError) as e: |
|
69
|
|
|
assert_that(False) |
|
70
|
|
|
|
|
71
|
|
|
self.assertEqual('Assertion failed', str(e.exception)) |
|
72
|
|
|
|
|
73
|
|
|
|
|
74
|
|
|
if __name__ == "__main__": |
|
75
|
|
|
unittest.main() |
|
76
|
|
|
|