Skip to content

Commit

Permalink
tests: harden test_reprcompare_notin, factor out callop (#235)
Browse files Browse the repository at this point in the history
  • Loading branch information
blueyed committed Feb 19, 2020
1 parent f98728a commit 3a4a494
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
7 changes: 6 additions & 1 deletion src/_pytest/assertion/__init__.py
Expand Up @@ -2,12 +2,15 @@
support for presenting detailed information in failing assertions.
"""
import sys
from typing import Any
from typing import List
from typing import Optional

from _pytest.assertion import rewrite
from _pytest.assertion import truncate
from _pytest.assertion import util
from _pytest.compat import TYPE_CHECKING
from _pytest.config import Config
from _pytest.config import hookimpl

if TYPE_CHECKING:
Expand Down Expand Up @@ -169,5 +172,7 @@ def pytest_sessionfinish(session):
assertstate.hook.set_session(None)


def pytest_assertrepr_compare(config, op, left, right):
def pytest_assertrepr_compare(
config: Config, op: str, left: Any, right: Any
) -> Optional[List[str]]:
return util.assertrepr_compare(config=config, op=op, left=left, right=right)
22 changes: 16 additions & 6 deletions testing/test_assertion.py
@@ -1,6 +1,9 @@
import collections.abc as collections_abc
import sys
import textwrap
from typing import Any
from typing import List
from typing import Optional

import attr

Expand Down Expand Up @@ -312,9 +315,13 @@ def test_check(list):
result.stdout.fnmatch_lines(["*test_hello*FAIL*", "*test_check*PASS*"])


def callequal(left, right, verbose=0):
def callop(op: str, left: Any, right: Any, verbose: int = 0) -> Optional[List[str]]:
config = mock_config(verbose=verbose)
return plugin.pytest_assertrepr_compare(config, "==", left, right)
return plugin.pytest_assertrepr_compare(config, op, left, right)


def callequal(left: Any, right: Any, verbose: int = 0) -> Optional[List[str]]:
return callop("==", left, right, verbose)


class TestAssert_reprcompare:
Expand Down Expand Up @@ -1170,10 +1177,13 @@ def test_rewritten():
assert testdir.runpytest().ret == 0


def test_reprcompare_notin():
config = mock_config()
detail = plugin.pytest_assertrepr_compare(config, "not in", "foo", "aaafoobbb")[1:]
assert detail == ["'foo' is contained here:", " aaafoobbb", "? +++"]
def test_reprcompare_notin() -> None:
assert callop("not in", "foo", "aaafoobbb") == [
"'foo' not in 'aaafoobbb'",
"'foo' is contained here:",
" aaafoobbb",
"? +++",
]


def test_reprcompare_whitespaces():
Expand Down

0 comments on commit 3a4a494

Please sign in to comment.