Use TypeError instead of AssertionError for no sequence

Improve/extends tests.
This commit is contained in:
Daniel Hahler 2020-02-01 22:06:15 +01:00
parent 09a0e45492
commit b10ab0211c
2 changed files with 15 additions and 3 deletions

View File

@ -1430,7 +1430,8 @@ class LineMatcher:
:param str match_nickname: the nickname for the match function that
will be logged to stdout when a match occurs
"""
assert isinstance(lines2, collections.abc.Sequence)
if not isinstance(lines2, collections.abc.Sequence):
raise TypeError("invalid type for lines2: {}".format(type(lines2).__name__))
lines2 = self._getlines(lines2)
lines1 = self.lines[:]
nextline = None

View File

@ -458,15 +458,26 @@ def test_testdir_run_timeout_expires(testdir) -> None:
def test_linematcher_with_nonlist() -> None:
"""Test LineMatcher with regard to passing in a set (accidentally)."""
from _pytest._code.source import Source
lm = LineMatcher([])
with pytest.raises(AssertionError):
with pytest.raises(TypeError, match="invalid type for lines2: set"):
lm.fnmatch_lines(set())
with pytest.raises(AssertionError):
with pytest.raises(TypeError, match="invalid type for lines2: dict"):
lm.fnmatch_lines({})
with pytest.raises(TypeError, match="invalid type for lines2: set"):
lm.re_match_lines(set())
with pytest.raises(TypeError, match="invalid type for lines2: dict"):
lm.re_match_lines({})
with pytest.raises(TypeError, match="invalid type for lines2: Source"):
lm.fnmatch_lines(Source())
lm.fnmatch_lines([])
lm.fnmatch_lines(())
lm.fnmatch_lines("")
assert lm._getlines({}) == {}
assert lm._getlines(set()) == set()
assert lm._getlines(Source()) == []
assert lm._getlines(Source("pass\npass")) == ["pass", "pass"]
def test_linematcher_match_failure() -> None: