tests: test_code: improve/clarify imports
This commit is contained in:
parent
b42938421e
commit
3dbc61dd80
|
@ -5,15 +5,18 @@ from unittest import mock
|
||||||
|
|
||||||
import py.path
|
import py.path
|
||||||
|
|
||||||
import _pytest._code
|
|
||||||
import pytest
|
import pytest
|
||||||
|
from _pytest._code import Code
|
||||||
|
from _pytest._code import ExceptionInfo
|
||||||
|
from _pytest._code import Frame
|
||||||
from _pytest._code import getfslineno
|
from _pytest._code import getfslineno
|
||||||
|
from _pytest._code.code import ReprFuncArgs
|
||||||
|
|
||||||
|
|
||||||
def test_ne() -> None:
|
def test_ne() -> None:
|
||||||
code1 = _pytest._code.Code(compile('foo = "bar"', "", "exec"))
|
code1 = Code(compile('foo = "bar"', "", "exec"))
|
||||||
assert code1 == code1
|
assert code1 == code1
|
||||||
code2 = _pytest._code.Code(compile('foo = "baz"', "", "exec"))
|
code2 = Code(compile('foo = "baz"', "", "exec"))
|
||||||
assert code2 != code1
|
assert code2 != code1
|
||||||
|
|
||||||
|
|
||||||
|
@ -21,7 +24,7 @@ def test_code_gives_back_name_for_not_existing_file() -> None:
|
||||||
name = "abc-123"
|
name = "abc-123"
|
||||||
co_code = compile("pass\n", name, "exec")
|
co_code = compile("pass\n", name, "exec")
|
||||||
assert co_code.co_filename == name
|
assert co_code.co_filename == name
|
||||||
code = _pytest._code.Code(co_code)
|
code = Code(co_code)
|
||||||
assert str(code.path) == name
|
assert str(code.path) == name
|
||||||
assert code.fullsource is None
|
assert code.fullsource is None
|
||||||
|
|
||||||
|
@ -30,7 +33,7 @@ def test_code_with_class() -> None:
|
||||||
class A:
|
class A:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
pytest.raises(TypeError, _pytest._code.Code, A)
|
pytest.raises(TypeError, Code, A)
|
||||||
|
|
||||||
|
|
||||||
def x() -> None:
|
def x() -> None:
|
||||||
|
@ -38,13 +41,13 @@ def x() -> None:
|
||||||
|
|
||||||
|
|
||||||
def test_code_fullsource() -> None:
|
def test_code_fullsource() -> None:
|
||||||
code = _pytest._code.Code(x)
|
code = Code(x)
|
||||||
full = code.fullsource
|
full = code.fullsource
|
||||||
assert "test_code_fullsource()" in str(full)
|
assert "test_code_fullsource()" in str(full)
|
||||||
|
|
||||||
|
|
||||||
def test_code_source() -> None:
|
def test_code_source() -> None:
|
||||||
code = _pytest._code.Code(x)
|
code = Code(x)
|
||||||
src = code.source()
|
src = code.source()
|
||||||
expected = """def x() -> None:
|
expected = """def x() -> None:
|
||||||
raise NotImplementedError()"""
|
raise NotImplementedError()"""
|
||||||
|
@ -55,7 +58,7 @@ def test_frame_getsourcelineno_myself() -> None:
|
||||||
def func() -> FrameType:
|
def func() -> FrameType:
|
||||||
return sys._getframe(0)
|
return sys._getframe(0)
|
||||||
|
|
||||||
f = _pytest._code.Frame(func())
|
f = Frame(func())
|
||||||
source, lineno = f.code.fullsource, f.lineno
|
source, lineno = f.code.fullsource, f.lineno
|
||||||
assert source is not None
|
assert source is not None
|
||||||
assert source[lineno].startswith(" return sys._getframe(0)")
|
assert source[lineno].startswith(" return sys._getframe(0)")
|
||||||
|
@ -65,13 +68,13 @@ def test_getstatement_empty_fullsource() -> None:
|
||||||
def func() -> FrameType:
|
def func() -> FrameType:
|
||||||
return sys._getframe(0)
|
return sys._getframe(0)
|
||||||
|
|
||||||
f = _pytest._code.Frame(func())
|
f = Frame(func())
|
||||||
with mock.patch.object(f.code.__class__, "fullsource", None):
|
with mock.patch.object(f.code.__class__, "fullsource", None):
|
||||||
assert f.statement == ""
|
assert f.statement == ""
|
||||||
|
|
||||||
|
|
||||||
def test_code_from_func() -> None:
|
def test_code_from_func() -> None:
|
||||||
co = _pytest._code.Code(test_frame_getsourcelineno_myself)
|
co = Code(test_frame_getsourcelineno_myself)
|
||||||
assert co.firstlineno
|
assert co.firstlineno
|
||||||
assert co.path
|
assert co.path
|
||||||
|
|
||||||
|
@ -90,25 +93,25 @@ def test_code_getargs() -> None:
|
||||||
def f1(x):
|
def f1(x):
|
||||||
raise NotImplementedError()
|
raise NotImplementedError()
|
||||||
|
|
||||||
c1 = _pytest._code.Code(f1)
|
c1 = Code(f1)
|
||||||
assert c1.getargs(var=True) == ("x",)
|
assert c1.getargs(var=True) == ("x",)
|
||||||
|
|
||||||
def f2(x, *y):
|
def f2(x, *y):
|
||||||
raise NotImplementedError()
|
raise NotImplementedError()
|
||||||
|
|
||||||
c2 = _pytest._code.Code(f2)
|
c2 = Code(f2)
|
||||||
assert c2.getargs(var=True) == ("x", "y")
|
assert c2.getargs(var=True) == ("x", "y")
|
||||||
|
|
||||||
def f3(x, **z):
|
def f3(x, **z):
|
||||||
raise NotImplementedError()
|
raise NotImplementedError()
|
||||||
|
|
||||||
c3 = _pytest._code.Code(f3)
|
c3 = Code(f3)
|
||||||
assert c3.getargs(var=True) == ("x", "z")
|
assert c3.getargs(var=True) == ("x", "z")
|
||||||
|
|
||||||
def f4(x, *y, **z):
|
def f4(x, *y, **z):
|
||||||
raise NotImplementedError()
|
raise NotImplementedError()
|
||||||
|
|
||||||
c4 = _pytest._code.Code(f4)
|
c4 = Code(f4)
|
||||||
assert c4.getargs(var=True) == ("x", "y", "z")
|
assert c4.getargs(var=True) == ("x", "y", "z")
|
||||||
|
|
||||||
|
|
||||||
|
@ -116,25 +119,25 @@ def test_frame_getargs() -> None:
|
||||||
def f1(x) -> FrameType:
|
def f1(x) -> FrameType:
|
||||||
return sys._getframe(0)
|
return sys._getframe(0)
|
||||||
|
|
||||||
fr1 = _pytest._code.Frame(f1("a"))
|
fr1 = Frame(f1("a"))
|
||||||
assert fr1.getargs(var=True) == [("x", "a")]
|
assert fr1.getargs(var=True) == [("x", "a")]
|
||||||
|
|
||||||
def f2(x, *y) -> FrameType:
|
def f2(x, *y) -> FrameType:
|
||||||
return sys._getframe(0)
|
return sys._getframe(0)
|
||||||
|
|
||||||
fr2 = _pytest._code.Frame(f2("a", "b", "c"))
|
fr2 = Frame(f2("a", "b", "c"))
|
||||||
assert fr2.getargs(var=True) == [("x", "a"), ("y", ("b", "c"))]
|
assert fr2.getargs(var=True) == [("x", "a"), ("y", ("b", "c"))]
|
||||||
|
|
||||||
def f3(x, **z) -> FrameType:
|
def f3(x, **z) -> FrameType:
|
||||||
return sys._getframe(0)
|
return sys._getframe(0)
|
||||||
|
|
||||||
fr3 = _pytest._code.Frame(f3("a", b="c"))
|
fr3 = Frame(f3("a", b="c"))
|
||||||
assert fr3.getargs(var=True) == [("x", "a"), ("z", {"b": "c"})]
|
assert fr3.getargs(var=True) == [("x", "a"), ("z", {"b": "c"})]
|
||||||
|
|
||||||
def f4(x, *y, **z) -> FrameType:
|
def f4(x, *y, **z) -> FrameType:
|
||||||
return sys._getframe(0)
|
return sys._getframe(0)
|
||||||
|
|
||||||
fr4 = _pytest._code.Frame(f4("a", "b", c="d"))
|
fr4 = Frame(f4("a", "b", c="d"))
|
||||||
assert fr4.getargs(var=True) == [("x", "a"), ("y", ("b",)), ("z", {"c": "d"})]
|
assert fr4.getargs(var=True) == [("x", "a"), ("y", ("b",)), ("z", {"c": "d"})]
|
||||||
|
|
||||||
|
|
||||||
|
@ -146,12 +149,12 @@ class TestExceptionInfo:
|
||||||
else:
|
else:
|
||||||
assert False
|
assert False
|
||||||
except AssertionError:
|
except AssertionError:
|
||||||
exci = _pytest._code.ExceptionInfo.from_current()
|
exci = ExceptionInfo.from_current()
|
||||||
assert exci.getrepr()
|
assert exci.getrepr()
|
||||||
|
|
||||||
def test_from_current_with_missing(self) -> None:
|
def test_from_current_with_missing(self) -> None:
|
||||||
with pytest.raises(AssertionError, match="no current exception"):
|
with pytest.raises(AssertionError, match="no current exception"):
|
||||||
_pytest._code.ExceptionInfo.from_current()
|
ExceptionInfo.from_current()
|
||||||
|
|
||||||
|
|
||||||
class TestTracebackEntry:
|
class TestTracebackEntry:
|
||||||
|
@ -162,7 +165,7 @@ class TestTracebackEntry:
|
||||||
else:
|
else:
|
||||||
assert False
|
assert False
|
||||||
except AssertionError:
|
except AssertionError:
|
||||||
exci = _pytest._code.ExceptionInfo.from_current()
|
exci = ExceptionInfo.from_current()
|
||||||
entry = exci.traceback[0]
|
entry = exci.traceback[0]
|
||||||
source = entry.getsource()
|
source = entry.getsource()
|
||||||
assert source is not None
|
assert source is not None
|
||||||
|
@ -172,8 +175,6 @@ class TestTracebackEntry:
|
||||||
|
|
||||||
class TestReprFuncArgs:
|
class TestReprFuncArgs:
|
||||||
def test_not_raise_exception_with_mixed_encoding(self, tw_mock) -> None:
|
def test_not_raise_exception_with_mixed_encoding(self, tw_mock) -> None:
|
||||||
from _pytest._code.code import ReprFuncArgs
|
|
||||||
|
|
||||||
args = [("unicode_string", "São Paulo"), ("utf8_string", b"S\xc3\xa3o Paulo")]
|
args = [("unicode_string", "São Paulo"), ("utf8_string", b"S\xc3\xa3o Paulo")]
|
||||||
|
|
||||||
r = ReprFuncArgs(args)
|
r = ReprFuncArgs(args)
|
||||||
|
|
Loading…
Reference in New Issue