Skip to content

Commit

Permalink
Use SequenceComparison for all cases of ShouldWarn
Browse files Browse the repository at this point in the history
  • Loading branch information
stinovlas committed Sep 14, 2023
1 parent 7b1e189 commit b7162b3
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 21 deletions.
12 changes: 7 additions & 5 deletions docs/warnings.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ causing the test in which it occurs to fail:
... warn("sorry dave, I can't let you do that")
Traceback (most recent call last):
...
AssertionError: sequence not as expected:
<BLANKLINE>
AssertionError:...
<SequenceComparison(ordered=True, partial=False)(failed)>
same:
[]
<BLANKLINE>
Expand All @@ -40,6 +40,7 @@ attributes differ:
<BLANKLINE>
actual:
[UserWarning("sorry dave, I can't let you do that"...)]
</SequenceComparison(ordered=True, partial=False)> (expected) != [UserWarning("sorry dave, I can't let you do that"...)] (actual)

You can check multiple warnings in a particular piece of code:

Expand All @@ -50,7 +51,7 @@ You can check multiple warnings in a particular piece of code:
... warn('you should fix that')
... warn('and that too')

If you don't care about order of issued warnings, you can use ``ordered=False``
If you don't care about the order of issued warnings, you can use the ``ordered=False``
argument of :class:`ShouldWarn`:

>>> from warnings import warn
Expand Down Expand Up @@ -89,8 +90,8 @@ context it manages, it will raise an :class:`AssertionError` to indicate this:
... warn("woah dude")
Traceback (most recent call last):
...
AssertionError: sequence not as expected:
<BLANKLINE>
AssertionError:...
<SequenceComparison(ordered=True, partial=False)(failed)>
same:
[]
<BLANKLINE>
Expand All @@ -99,3 +100,4 @@ expected:
<BLANKLINE>
actual:
[UserWarning('woah dude'...)]
</SequenceComparison(ordered=True, partial=False)> (expected) != [UserWarning('woah dude'...)] (actual)
12 changes: 6 additions & 6 deletions testfixtures/shouldwarn.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import warnings
from typing import Union, Type

from testfixtures import Comparison as C, SequenceComparison as S, compare
from testfixtures import Comparison, SequenceComparison, compare


WarningOrType = Union[Warning, Type[Warning]]
Expand Down Expand Up @@ -38,10 +38,10 @@ class ShouldWarn(warnings.catch_warnings):

def __init__(self, *expected: WarningOrType, ordered=True, **filters):
super(ShouldWarn, self).__init__(record=True)
if ordered:
self.expected = [C(e) for e in expected]
else:
self.expected = S(*[C(e) for e in expected], ordered=ordered)
self.expected = SequenceComparison(
*[Comparison(e) for e in expected],
ordered=ordered,
)
self.filters = filters

def __enter__(self):
Expand All @@ -53,7 +53,7 @@ def __exit__(self, exc_type, exc_val, exc_tb):
super(ShouldWarn, self).__exit__(exc_type, exc_val, exc_tb)
if not self.recorded and self._empty_okay:
return
if not self.expected and self.recorded and not self._empty_okay:
if not self.expected.expected and self.recorded and not self._empty_okay:
return
compare(self.expected, actual=[wm.message for wm in self.recorded])

Expand Down
28 changes: 18 additions & 10 deletions testfixtures/tests/test_shouldwarn.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ def test_warn_expected(self):

def test_warn_not_expected(self):
with ShouldAssert(
"sequence not as expected:\n\n"
"\n<SequenceComparison(ordered=True, partial=False)(failed)>\n"
"same:\n[]\n\n"
"expected:\n[]\n\n"
"actual:\n[UserWarning('foo'"+comma+")]"
"actual:\n[UserWarning('foo'"+comma+")]\n"
"</SequenceComparison(ordered=True, partial=False)> (expected) "
"!= [UserWarning('foo'"+comma+")] (actual)"
):
with warnings.catch_warnings(record=True) as backstop:
with ShouldNotWarn():
Expand All @@ -41,10 +43,11 @@ def test_no_warn_expected(self):

def test_no_warn_not_expected(self):
with ShouldAssert(
"sequence not as expected:\n\n"
"\n<SequenceComparison(ordered=True, partial=False)(failed)>\n"
"same:\n[]\n\n"
"expected:\n[<C:builtins.UserWarning>args: ('foo',)</>]"
"\n\nactual:\n[]"
"\n\nactual:\n[]\n"
"</SequenceComparison(ordered=True, partial=False)> (expected) != [] (actual)"
):
with ShouldWarn(UserWarning('foo')):
pass
Expand Down Expand Up @@ -93,11 +96,13 @@ def test_minimal_ok(self):

def test_minimal_bad(self):
with ShouldAssert(
"sequence not as expected:\n\n"
"\n<SequenceComparison(ordered=True, partial=False)(failed)>\n"
"same:\n[]\n\n"
"expected:\n"
"[<C:builtins.DeprecationWarning(failed)>wrong type</>]\n\n"
"actual:\n[UserWarning('foo'"+comma+")]"
"actual:\n[UserWarning('foo'"+comma+")]\n"
"</SequenceComparison(ordered=True, partial=False)> (expected) "
"!= [UserWarning('foo'"+comma+")] (actual)"
):
with ShouldWarn(DeprecationWarning):
warnings.warn('foo')
Expand All @@ -110,14 +115,16 @@ def test_maximal_ok(self):

def test_maximal_bad(self):
with ShouldAssert(
"sequence not as expected:\n\n"
"\n<SequenceComparison(ordered=True, partial=False)(failed)>\n"
"same:\n[]\n\n"
"expected:\n[\n"
"<C:builtins.DeprecationWarning(failed)>\n"
"attributes differ:\n"
"'args': ('bar',) (Comparison) != ('foo',) (actual)\n"
"</C:builtins.DeprecationWarning>]\n\n"
"actual:\n[DeprecationWarning('foo'"+comma+")]"
"actual:\n[DeprecationWarning('foo'"+comma+")]\n"
"</SequenceComparison(ordered=True, partial=False)> (expected) "
"!= [DeprecationWarning('foo'"+comma+")] (actual)"
):
with ShouldWarn(DeprecationWarning('bar')):
warnings.warn_explicit(
Expand Down Expand Up @@ -153,10 +160,11 @@ def test_filter_present(self):

def test_filter_missing(self):
with ShouldAssert(
"sequence not as expected:\n\n"
"\n<SequenceComparison(ordered=True, partial=False)(failed)>\n"
"same:\n[]\n\n"
"expected:\n[<C:builtins.DeprecationWarning>]\n\n"
"actual:\n[]"
"actual:\n[]\n"
"</SequenceComparison(ordered=True, partial=False)> (expected) != [] (actual)"
):
with ShouldWarn(DeprecationWarning,
message="This function is deprecated."):
Expand Down

0 comments on commit b7162b3

Please sign in to comment.