|
| 1 | +# encoding: utf-8 |
| 2 | +import unittest |
| 3 | + |
| 4 | +from hamcrest.core.assert_that import DeferAssertContextManager |
| 5 | +from hamcrest.core.core.isequal import equal_to |
| 6 | + |
| 7 | +class DeferAssertContextManagerTest(unittest.TestCase): |
| 8 | + def testAssertionSuccessfully(self): |
| 9 | + with DeferAssertContextManager() as da: |
| 10 | + da.assert_that(1, equal_to(1)) |
| 11 | + |
| 12 | + def testAssertionTeardownSuccessfully(self): |
| 13 | + actual = "ACTUAL" |
| 14 | + |
| 15 | + with DeferAssertContextManager() as da: |
| 16 | + da.assert_that(actual, equal_to("ACTUAL")) |
| 17 | + actual = "" |
| 18 | + self.assertEqual(actual, "") |
| 19 | + |
| 20 | + def testAssertionErrorShouldTeardownBeforeRaiseExeption(self): |
| 21 | + self.maxDiff = None |
| 22 | + expected = "EXPECTED" |
| 23 | + actual = "ACTUAL" |
| 24 | + |
| 25 | + expectedMessage = "\nExpected: 'EXPECTED'\n but: was 'ACTUAL'\n" |
| 26 | + |
| 27 | + with self.assertRaises(AssertionError) as e: |
| 28 | + with DeferAssertContextManager() as da: |
| 29 | + da.assert_that(actual, equal_to(expected)) |
| 30 | + actual = "" |
| 31 | + self.assertEqual(actual, "") |
| 32 | + self.assertEqual(expectedMessage, str(e.exception)) |
| 33 | + |
| 34 | + |
| 35 | + def testAssertionErrorShouldRaiseExceptionBeforeExitingContext(self): |
| 36 | + self.maxDiff = None |
| 37 | + expected = "EXPECTED" |
| 38 | + actual = "ACTUAL" |
| 39 | + |
| 40 | + expectedMessage = "\nExpected: 'EXPECTED'\n but: was 'ACTUAL'\n" |
| 41 | + |
| 42 | + with self.assertRaises(AssertionError) as e: |
| 43 | + with DeferAssertContextManager() as da: |
| 44 | + da.assert_that(actual, equal_to(expected)) |
| 45 | + actual = "" |
| 46 | + self.assertNotEqual(actual, "") |
| 47 | + self.assertEqual(expectedMessage, str(e.exception)) |
| 48 | + |
| 49 | + |
| 50 | + |
| 51 | +if __name__ == "__main__": |
| 52 | + unittest.main() |
0 commit comments