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
|
--- contrib/python/PyHamcrest/py3/hamcrest/core/core/raises.py (index)
+++ contrib/python/PyHamcrest/py3/hamcrest/core/core/raises.py (working tree)
@@ -18,12 +18,14 @@ class Raises(BaseMatcher[Callable[..., Any]]):
expected: Type[Exception],
pattern: Optional[str] = None,
matching: Optional[Matcher] = None,
+ matcher: Optional[Matcher] = None,
) -> None:
self.pattern = pattern
- self.matcher = matching
+ self.matcher = matching or matcher
self.expected = expected
self.actual: Optional[BaseException] = None
self.function: Optional[Callable[..., Any]] = None
+ self.actual_return_value = None
def _matches(self, function: Callable[..., Any]) -> bool:
if not callable(function):
@@ -35,7 +37,7 @@ class Raises(BaseMatcher[Callable[..., Any]]):
def _call_function(self, function: Callable[..., Any]) -> bool:
self.actual = None
try:
- function()
+ self.actual_return_value = function()
except BaseException:
self.actual = sys.exc_info()[1]
@@ -51,6 +53,9 @@ class Raises(BaseMatcher[Callable[..., Any]]):
def describe_to(self, description: Description) -> None:
description.append_text("Expected a callable raising %s" % self.expected)
+ if self.matcher is not None:
+ description.append_text("\n and ")
+ description.append_description_of(self.matcher)
def describe_mismatch(self, item, description: Description) -> None:
if not callable(item):
@@ -64,7 +69,7 @@ class Raises(BaseMatcher[Callable[..., Any]]):
return
if self.actual is None:
- description.append_text("No exception raised.")
+ description.append_text("No exception raised and actual return value = '{}'".format(self.actual_return_value))
elif isinstance(self.actual, self.expected):
if self.pattern is not None or self.matcher is not None:
description.append_text("Correct assertion type raised, but ")
@@ -88,7 +93,7 @@ class Raises(BaseMatcher[Callable[..., Any]]):
)
-def raises(exception: Type[Exception], pattern=None, matching=None) -> Matcher[Callable[..., Any]]:
+def raises(exception: Type[Exception], pattern=None, matching=None, matcher=None) -> Matcher[Callable[..., Any]]:
"""Matches if the called function raised the expected exception.
:param exception: The class of the expected exception
@@ -109,7 +114,7 @@ def raises(exception: Type[Exception], pattern=None, matching=None) -> Matcher[C
raises(HTTPError, matching=has_properties(status_code=500)
)
"""
- return Raises(exception, pattern, matching)
+ return Raises(exception, pattern, matching, matcher)
class DeferredCallable(object):
|