test_ok2/testing/code/test_code.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

214 lines
5.4 KiB
Python
Raw Permalink Normal View History

# mypy: allow-untyped-defs
import re
import sys
from types import FrameType
from unittest import mock
from _pytest._code import Code
from _pytest._code import ExceptionInfo
from _pytest._code import Frame
from _pytest._code import Source
from _pytest._code.code import ExceptionChainRepr
from _pytest._code.code import ReprFuncArgs
import pytest
def test_ne() -> None:
code1 = Code(compile('foo = "bar"', "", "exec"))
assert code1 == code1
code2 = Code(compile('foo = "baz"', "", "exec"))
assert code2 != code1
def test_code_gives_back_name_for_not_existing_file() -> None:
2018-05-23 22:48:46 +08:00
name = "abc-123"
co_code = compile("pass\n", name, "exec")
assert co_code.co_filename == name
code = Code(co_code)
assert str(code.path) == name
assert code.fullsource is None
2020-10-27 22:07:03 +08:00
def test_code_from_function_with_class() -> None:
2019-06-03 06:32:00 +08:00
class A:
pass
2018-05-23 22:48:46 +08:00
2020-10-27 22:07:03 +08:00
with pytest.raises(TypeError):
Code.from_function(A)
def x() -> None:
2018-08-27 07:13:22 +08:00
raise NotImplementedError()
def test_code_fullsource() -> None:
2020-10-27 22:07:03 +08:00
code = Code.from_function(x)
full = code.fullsource
2018-05-23 22:48:46 +08:00
assert "test_code_fullsource()" in str(full)
def test_code_source() -> None:
2020-10-27 22:07:03 +08:00
code = Code.from_function(x)
src = code.source()
expected = """def x() -> None:
2018-08-27 07:13:22 +08:00
raise NotImplementedError()"""
assert str(src) == expected
def test_frame_getsourcelineno_myself() -> None:
def func() -> FrameType:
return sys._getframe(0)
2018-05-23 22:48:46 +08:00
f = Frame(func())
source, lineno = f.code.fullsource, f.lineno
assert source is not None
assert source[lineno].startswith(" return sys._getframe(0)")
def test_getstatement_empty_fullsource() -> None:
def func() -> FrameType:
return sys._getframe(0)
2018-05-23 22:48:46 +08:00
f = Frame(func())
with mock.patch.object(f.code.__class__, "fullsource", None):
assert f.statement == Source("")
def test_code_from_func() -> None:
2020-10-27 22:07:03 +08:00
co = Code.from_function(test_frame_getsourcelineno_myself)
assert co.firstlineno
assert co.path
def test_unicode_handling() -> None:
2019-06-03 06:32:00 +08:00
value = "ąć".encode()
def f() -> None:
raise Exception(value)
excinfo = pytest.raises(Exception, f)
2019-06-03 06:32:00 +08:00
str(excinfo)
def test_code_getargs() -> None:
def f1(x):
2018-08-27 07:13:22 +08:00
raise NotImplementedError()
2018-05-23 22:48:46 +08:00
2020-10-27 22:07:03 +08:00
c1 = Code.from_function(f1)
2018-05-23 22:48:46 +08:00
assert c1.getargs(var=True) == ("x",)
def f2(x, *y):
2018-08-27 07:13:22 +08:00
raise NotImplementedError()
2018-05-23 22:48:46 +08:00
2020-10-27 22:07:03 +08:00
c2 = Code.from_function(f2)
2018-05-23 22:48:46 +08:00
assert c2.getargs(var=True) == ("x", "y")
def f3(x, **z):
2018-08-27 07:13:22 +08:00
raise NotImplementedError()
2018-05-23 22:48:46 +08:00
2020-10-27 22:07:03 +08:00
c3 = Code.from_function(f3)
2018-05-23 22:48:46 +08:00
assert c3.getargs(var=True) == ("x", "z")
def f4(x, *y, **z):
2018-08-27 07:13:22 +08:00
raise NotImplementedError()
2018-05-23 22:48:46 +08:00
2020-10-27 22:07:03 +08:00
c4 = Code.from_function(f4)
2018-05-23 22:48:46 +08:00
assert c4.getargs(var=True) == ("x", "y", "z")
def test_frame_getargs() -> None:
def f1(x) -> FrameType:
return sys._getframe(0)
2018-05-23 22:48:46 +08:00
fr1 = Frame(f1("a"))
2018-05-23 22:48:46 +08:00
assert fr1.getargs(var=True) == [("x", "a")]
def f2(x, *y) -> FrameType:
return sys._getframe(0)
2018-05-23 22:48:46 +08:00
fr2 = Frame(f2("a", "b", "c"))
2018-05-23 22:48:46 +08:00
assert fr2.getargs(var=True) == [("x", "a"), ("y", ("b", "c"))]
def f3(x, **z) -> FrameType:
return sys._getframe(0)
2018-05-23 22:48:46 +08:00
fr3 = Frame(f3("a", b="c"))
2018-05-23 22:48:46 +08:00
assert fr3.getargs(var=True) == [("x", "a"), ("z", {"b": "c"})]
def f4(x, *y, **z) -> FrameType:
return sys._getframe(0)
2018-05-23 22:48:46 +08:00
fr4 = Frame(f4("a", "b", c="d"))
2018-05-23 22:48:46 +08:00
assert fr4.getargs(var=True) == [("x", "a"), ("y", ("b",)), ("z", {"c": "d"})]
2019-06-03 06:32:00 +08:00
class TestExceptionInfo:
def test_bad_getsource(self) -> None:
try:
if False:
pass
else:
assert False
except AssertionError:
exci = ExceptionInfo.from_current()
assert exci.getrepr()
def test_from_current_with_missing(self) -> None:
with pytest.raises(AssertionError, match="no current exception"):
ExceptionInfo.from_current()
2019-06-03 06:32:00 +08:00
class TestTracebackEntry:
def test_getsource(self) -> None:
try:
if False:
pass
else:
assert False
except AssertionError:
exci = ExceptionInfo.from_current()
entry = exci.traceback[0]
source = entry.getsource()
assert source is not None
assert len(source) == 6
2018-05-23 22:48:46 +08:00
assert "assert False" in source[5]
def test_tb_entry_str(self):
try:
assert False
except AssertionError:
exci = ExceptionInfo.from_current()
pattern = r" File '.*test_code.py':\d+ in test_tb_entry_str\n assert False"
entry = str(exci.traceback[0])
assert re.match(pattern, entry)
2019-06-03 06:32:00 +08:00
class TestReprFuncArgs:
def test_not_raise_exception_with_mixed_encoding(self, tw_mock) -> None:
2019-06-03 06:32:00 +08:00
args = [("unicode_string", "São Paulo"), ("utf8_string", b"S\xc3\xa3o Paulo")]
r = ReprFuncArgs(args)
r.toterminal(tw_mock)
assert (
tw_mock.lines[0]
== r"unicode_string = São Paulo, utf8_string = b'S\xc3\xa3o Paulo'"
)
def test_ExceptionChainRepr():
"""Test ExceptionChainRepr, especially with regard to being hashable."""
try:
raise ValueError()
except ValueError:
excinfo1 = ExceptionInfo.from_current()
excinfo2 = ExceptionInfo.from_current()
repr1 = excinfo1.getrepr()
repr2 = excinfo2.getrepr()
assert repr1 != repr2
assert isinstance(repr1, ExceptionChainRepr)
assert hash(repr1) != hash(repr2)
assert repr1 is not excinfo1.getrepr()