test_ok2/testing/code/test_code.py

185 lines
4.3 KiB
Python
Raw Normal View History

import sys
from unittest import mock
from test_excinfo import TWMock
import _pytest._code
import pytest
def test_ne():
2018-05-23 22:48:46 +08:00
code1 = _pytest._code.Code(compile('foo = "bar"', "", "exec"))
assert code1 == code1
2018-05-23 22:48:46 +08:00
code2 = _pytest._code.Code(compile('foo = "baz"', "", "exec"))
assert code2 != code1
def test_code_gives_back_name_for_not_existing_file():
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 = _pytest._code.Code(co_code)
assert str(code.path) == name
assert code.fullsource is None
def test_code_with_class():
2019-06-03 06:32:00 +08:00
class A:
pass
2018-05-23 22:48:46 +08:00
pytest.raises(TypeError, _pytest._code.Code, A)
2018-08-27 07:13:22 +08:00
def x():
raise NotImplementedError()
def test_code_fullsource():
code = _pytest._code.Code(x)
full = code.fullsource
2018-05-23 22:48:46 +08:00
assert "test_code_fullsource()" in str(full)
def test_code_source():
code = _pytest._code.Code(x)
src = code.source()
expected = """def x():
2018-08-27 07:13:22 +08:00
raise NotImplementedError()"""
assert str(src) == expected
def test_frame_getsourcelineno_myself():
def func():
return sys._getframe(0)
2018-05-23 22:48:46 +08:00
f = func()
f = _pytest._code.Frame(f)
source, lineno = f.code.fullsource, f.lineno
assert source[lineno].startswith(" return sys._getframe(0)")
def test_getstatement_empty_fullsource():
def func():
return sys._getframe(0)
2018-05-23 22:48:46 +08:00
f = func()
f = _pytest._code.Frame(f)
with mock.patch.object(f.code.__class__, "fullsource", None):
assert f.statement == ""
def test_code_from_func():
co = _pytest._code.Code(test_frame_getsourcelineno_myself)
assert co.firstlineno
assert co.path
def test_unicode_handling():
2019-06-03 06:32:00 +08:00
value = "ąć".encode()
def f():
raise Exception(value)
excinfo = pytest.raises(Exception, f)
2019-06-03 06:32:00 +08:00
str(excinfo)
def test_code_getargs():
def f1(x):
2018-08-27 07:13:22 +08:00
raise NotImplementedError()
2018-05-23 22:48:46 +08:00
c1 = _pytest._code.Code(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
c2 = _pytest._code.Code(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
c3 = _pytest._code.Code(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
c4 = _pytest._code.Code(f4)
2018-05-23 22:48:46 +08:00
assert c4.getargs(var=True) == ("x", "y", "z")
def test_frame_getargs():
def f1(x):
return sys._getframe(0)
2018-05-23 22:48:46 +08:00
fr1 = _pytest._code.Frame(f1("a"))
assert fr1.getargs(var=True) == [("x", "a")]
def f2(x, *y):
return sys._getframe(0)
2018-05-23 22:48:46 +08:00
fr2 = _pytest._code.Frame(f2("a", "b", "c"))
assert fr2.getargs(var=True) == [("x", "a"), ("y", ("b", "c"))]
def f3(x, **z):
return sys._getframe(0)
2018-05-23 22:48:46 +08:00
fr3 = _pytest._code.Frame(f3("a", b="c"))
assert fr3.getargs(var=True) == [("x", "a"), ("z", {"b": "c"})]
def f4(x, *y, **z):
return sys._getframe(0)
2018-05-23 22:48:46 +08:00
fr4 = _pytest._code.Frame(f4("a", "b", c="d"))
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):
try:
if False:
pass
else:
assert False
except AssertionError:
exci = _pytest._code.ExceptionInfo.from_current()
assert exci.getrepr()
def test_from_current_with_missing(self):
with pytest.raises(AssertionError, match="no current exception"):
_pytest._code.ExceptionInfo.from_current()
2019-06-03 06:32:00 +08:00
class TestTracebackEntry:
def test_getsource(self):
try:
if False:
pass
else:
assert False
except AssertionError:
exci = _pytest._code.ExceptionInfo.from_current()
entry = exci.traceback[0]
source = entry.getsource()
assert len(source) == 6
2018-05-23 22:48:46 +08:00
assert "assert False" in source[5]
2019-06-03 06:32:00 +08:00
class TestReprFuncArgs:
def test_not_raise_exception_with_mixed_encoding(self):
from _pytest._code.code import ReprFuncArgs
tw = TWMock()
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)
assert (
tw.lines[0]
== r"unicode_string = São Paulo, utf8_string = b'S\xc3\xa3o Paulo'"
)