Skip to content

Commit 50f99f5

Browse files
committed
add Defer Assertion
1 parent 346d3f7 commit 50f99f5

File tree

4 files changed

+87
-1
lines changed

4 files changed

+87
-1
lines changed

examples/ExampleWithDeferAssert.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import sys
2+
3+
sys.path.append("..")
4+
5+
from hamcrest import *
6+
import unittest
7+
8+
9+
class ExampleWithDeferAssert(unittest.TestCase):
10+
def testUsingDeferAssertThat(self):
11+
with DeferAssertContextManager() as da:
12+
da.assert_that("xx", is_("xx"))
13+
14+
15+
if __name__ == "__main__":
16+
unittest.main()

src/hamcrest/core/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# ruff: noqa: F405, F403
22

3-
from hamcrest.core.assert_that import assert_that
3+
from hamcrest.core.assert_that import assert_that, DeferAssertContextManager
44
from hamcrest.core.core import *
55

66
__author__ = "Jon Reid"
@@ -23,4 +23,5 @@
2323
"not_none",
2424
"raises",
2525
"same_instance",
26+
"DeferAssertContextManager",
2627
]

src/hamcrest/core/assert_that.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,21 @@ def _assert_bool(assertion: bool, reason: Optional[str] = None) -> None:
7676
if not reason:
7777
reason = "Assertion failed"
7878
raise AssertionError(reason)
79+
80+
81+
class DeferAssertContextManager:
82+
def __init__(self):
83+
self.exceptions = []
84+
85+
def assert_that(self, *args, **kwargs):
86+
try:
87+
assert_that(*args, **kwargs)
88+
except AssertionError as e:
89+
self.exceptions.append(e)
90+
91+
def __enter__(self):
92+
return self
93+
94+
def __exit__(self, exc_type, exc_value, traceback):
95+
if len(self.exceptions) > 0:
96+
raise self.exceptions.pop(0)
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
8+
class DeferAssertContextManagerTest(unittest.TestCase):
9+
def testAssertionSuccessfully(self):
10+
with DeferAssertContextManager() as da:
11+
da.assert_that(1, equal_to(1))
12+
13+
def testAssertionTeardownSuccessfully(self):
14+
actual = "ACTUAL"
15+
16+
with DeferAssertContextManager() as da:
17+
da.assert_that(actual, equal_to("ACTUAL"))
18+
actual = ""
19+
self.assertEqual(actual, "")
20+
21+
def testAssertionErrorShouldTeardownBeforeRaiseExeption(self):
22+
self.maxDiff = None
23+
expected = "EXPECTED"
24+
actual = "ACTUAL"
25+
26+
expectedMessage = "\nExpected: 'EXPECTED'\n but: was 'ACTUAL'\n"
27+
28+
with self.assertRaises(AssertionError) as e:
29+
with DeferAssertContextManager() as da:
30+
da.assert_that(actual, equal_to(expected))
31+
actual = ""
32+
self.assertEqual(actual, "")
33+
self.assertEqual(expectedMessage, str(e.exception))
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+
if __name__ == "__main__":
51+
unittest.main()

0 commit comments

Comments
 (0)