Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ordered argument to ShouldWarn context manager #185

Merged
merged 4 commits into from
Sep 15, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Use SequenceComparison for all cases of ShouldWarn
  • Loading branch information
stinovlas committed Sep 14, 2023
commit b7162b31d3f24ecc78b9110ce982771cf19dbd42
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