2015-11-27 22:43:01 +08:00
|
|
|
# flake8: noqa
|
|
|
|
# disable flake check on this file because some constructs are strange
|
|
|
|
# or redundant on purpose and can't be disable on a line-by-line basis
|
2017-03-17 09:21:30 +08:00
|
|
|
from __future__ import absolute_import, division, print_function
|
2017-12-27 11:47:26 +08:00
|
|
|
import inspect
|
2015-11-27 22:43:01 +08:00
|
|
|
import sys
|
|
|
|
|
|
|
|
import _pytest._code
|
|
|
|
import py
|
|
|
|
import pytest
|
|
|
|
from _pytest._code import Source
|
2017-12-12 01:54:47 +08:00
|
|
|
from _pytest._code.source import ast
|
2015-11-27 22:43:01 +08:00
|
|
|
|
|
|
|
|
2017-12-12 01:54:47 +08:00
|
|
|
astonly = pytest.mark.nothing
|
2015-11-27 22:43:01 +08:00
|
|
|
failsonjython = pytest.mark.xfail("sys.platform.startswith('java')")
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
def test_source_str_function():
|
|
|
|
x = Source("3")
|
|
|
|
assert str(x) == "3"
|
|
|
|
|
|
|
|
x = Source(" 3")
|
|
|
|
assert str(x) == "3"
|
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
x = Source(
|
|
|
|
"""
|
2015-11-27 22:43:01 +08:00
|
|
|
3
|
2018-05-23 22:48:46 +08:00
|
|
|
""",
|
|
|
|
rstrip=False,
|
|
|
|
)
|
2015-11-27 22:43:01 +08:00
|
|
|
assert str(x) == "\n3\n "
|
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
x = Source(
|
|
|
|
"""
|
2015-11-27 22:43:01 +08:00
|
|
|
3
|
2018-05-23 22:48:46 +08:00
|
|
|
""",
|
|
|
|
rstrip=True,
|
|
|
|
)
|
2015-11-27 22:43:01 +08:00
|
|
|
assert str(x) == "\n3"
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
def test_unicode():
|
|
|
|
try:
|
|
|
|
unicode
|
|
|
|
except NameError:
|
|
|
|
return
|
|
|
|
x = Source(unicode("4"))
|
|
|
|
assert str(x) == "4"
|
2018-05-23 22:48:46 +08:00
|
|
|
co = _pytest._code.compile(unicode('u"\xc3\xa5"', "utf8"), mode="eval")
|
2015-11-27 22:43:01 +08:00
|
|
|
val = eval(co)
|
|
|
|
assert isinstance(val, unicode)
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
def test_source_from_function():
|
|
|
|
source = _pytest._code.Source(test_source_str_function)
|
2018-05-23 22:48:46 +08:00
|
|
|
assert str(source).startswith("def test_source_str_function():")
|
2015-11-27 22:43:01 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
def test_source_from_method():
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestClass(object):
|
2015-11-27 22:43:01 +08:00
|
|
|
def test_method(self):
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
source = _pytest._code.Source(TestClass().test_method)
|
2018-05-23 22:48:46 +08:00
|
|
|
assert source.lines == ["def test_method(self):", " pass"]
|
2015-11-27 22:43:01 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
def test_source_from_lines():
|
|
|
|
lines = ["a \n", "b\n", "c"]
|
|
|
|
source = _pytest._code.Source(lines)
|
2018-05-23 22:48:46 +08:00
|
|
|
assert source.lines == ["a ", "b", "c"]
|
2015-11-27 22:43:01 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
def test_source_from_inner_function():
|
|
|
|
def f():
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
source = _pytest._code.Source(f, deindent=False)
|
2018-05-23 22:48:46 +08:00
|
|
|
assert str(source).startswith(" def f():")
|
2015-11-27 22:43:01 +08:00
|
|
|
source = _pytest._code.Source(f)
|
2018-05-23 22:48:46 +08:00
|
|
|
assert str(source).startswith("def f():")
|
2015-11-27 22:43:01 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
def test_source_putaround_simple():
|
|
|
|
source = Source("raise ValueError")
|
|
|
|
source = source.putaround(
|
2018-05-23 22:48:46 +08:00
|
|
|
"try:",
|
|
|
|
"""\
|
2015-11-27 22:43:01 +08:00
|
|
|
except ValueError:
|
|
|
|
x = 42
|
|
|
|
else:
|
2018-05-23 22:48:46 +08:00
|
|
|
x = 23""",
|
|
|
|
)
|
|
|
|
assert (
|
|
|
|
str(source)
|
|
|
|
== """\
|
2015-11-27 22:43:01 +08:00
|
|
|
try:
|
|
|
|
raise ValueError
|
|
|
|
except ValueError:
|
|
|
|
x = 42
|
|
|
|
else:
|
|
|
|
x = 23"""
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2015-11-27 22:43:01 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
def test_source_putaround():
|
|
|
|
source = Source()
|
2018-05-23 22:48:46 +08:00
|
|
|
source = source.putaround(
|
|
|
|
"""
|
2015-11-27 22:43:01 +08:00
|
|
|
if 1:
|
|
|
|
x=1
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2015-11-27 22:43:01 +08:00
|
|
|
assert str(source).strip() == "if 1:\n x=1"
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
def test_source_strips():
|
|
|
|
source = Source("")
|
|
|
|
assert source == Source()
|
2018-05-23 22:48:46 +08:00
|
|
|
assert str(source) == ""
|
2015-11-27 22:43:01 +08:00
|
|
|
assert source.strip() == source
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
def test_source_strip_multiline():
|
|
|
|
source = Source()
|
|
|
|
source.lines = ["", " hello", " "]
|
|
|
|
source2 = source.strip()
|
|
|
|
assert source2.lines == [" hello"]
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
def test_syntaxerror_rerepresentation():
|
2018-05-23 22:48:46 +08:00
|
|
|
ex = pytest.raises(SyntaxError, _pytest._code.compile, "xyz xyz")
|
2015-11-27 22:43:01 +08:00
|
|
|
assert ex.value.lineno == 1
|
2017-07-17 07:25:09 +08:00
|
|
|
assert ex.value.offset in (4, 7) # XXX pypy/jython versus cpython?
|
2018-05-23 22:48:46 +08:00
|
|
|
assert ex.value.text.strip(), "x x"
|
2015-11-27 22:43:01 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
def test_isparseable():
|
|
|
|
assert Source("hello").isparseable()
|
|
|
|
assert Source("if 1:\n pass").isparseable()
|
|
|
|
assert Source(" \nif 1:\n pass").isparseable()
|
|
|
|
assert not Source("if 1:\n").isparseable()
|
|
|
|
assert not Source(" \nif 1:\npass").isparseable()
|
|
|
|
assert not Source(chr(0)).isparseable()
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestAccesses(object):
|
2018-05-23 22:48:46 +08:00
|
|
|
source = Source(
|
|
|
|
"""\
|
2015-11-27 22:43:01 +08:00
|
|
|
def f(x):
|
|
|
|
pass
|
|
|
|
def g(x):
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
def test_getrange(self):
|
|
|
|
x = self.source[0:2]
|
|
|
|
assert x.isparseable()
|
|
|
|
assert len(x.lines) == 2
|
|
|
|
assert str(x) == "def f(x):\n pass"
|
|
|
|
|
|
|
|
def test_getline(self):
|
|
|
|
x = self.source[0]
|
|
|
|
assert x == "def f(x):"
|
|
|
|
|
|
|
|
def test_len(self):
|
|
|
|
assert len(self.source) == 4
|
|
|
|
|
|
|
|
def test_iter(self):
|
2017-11-04 23:17:20 +08:00
|
|
|
values = [x for x in self.source]
|
|
|
|
assert len(values) == 4
|
2015-11-27 22:43:01 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestSourceParsingAndCompiling(object):
|
2018-05-23 22:48:46 +08:00
|
|
|
source = Source(
|
|
|
|
"""\
|
2015-11-27 22:43:01 +08:00
|
|
|
def f(x):
|
|
|
|
assert (x ==
|
|
|
|
3 +
|
|
|
|
4)
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
).strip()
|
2015-11-27 22:43:01 +08:00
|
|
|
|
|
|
|
def test_compile(self):
|
|
|
|
co = _pytest._code.compile("x=3")
|
|
|
|
d = {}
|
2017-07-21 06:14:54 +08:00
|
|
|
exec(co, d)
|
2018-05-23 22:48:46 +08:00
|
|
|
assert d["x"] == 3
|
2015-11-27 22:43:01 +08:00
|
|
|
|
|
|
|
def test_compile_and_getsource_simple(self):
|
|
|
|
co = _pytest._code.compile("x=3")
|
2017-07-21 06:14:54 +08:00
|
|
|
exec(co)
|
2015-11-27 22:43:01 +08:00
|
|
|
source = _pytest._code.Source(co)
|
|
|
|
assert str(source) == "x=3"
|
|
|
|
|
|
|
|
def test_compile_and_getsource_through_same_function(self):
|
|
|
|
def gensource(source):
|
|
|
|
return _pytest._code.compile(source)
|
2018-05-23 22:48:46 +08:00
|
|
|
|
|
|
|
co1 = gensource(
|
|
|
|
"""
|
2015-11-27 22:43:01 +08:00
|
|
|
def f():
|
|
|
|
raise KeyError()
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
co2 = gensource(
|
|
|
|
"""
|
2015-11-27 22:43:01 +08:00
|
|
|
def f():
|
|
|
|
raise ValueError()
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2017-12-27 11:47:26 +08:00
|
|
|
source1 = inspect.getsource(co1)
|
2018-05-23 22:48:46 +08:00
|
|
|
assert "KeyError" in source1
|
2017-12-27 11:47:26 +08:00
|
|
|
source2 = inspect.getsource(co2)
|
2018-05-23 22:48:46 +08:00
|
|
|
assert "ValueError" in source2
|
2015-11-27 22:43:01 +08:00
|
|
|
|
|
|
|
def test_getstatement(self):
|
2017-07-17 07:25:09 +08:00
|
|
|
# print str(self.source)
|
2015-11-27 22:43:01 +08:00
|
|
|
ass = str(self.source[1:])
|
|
|
|
for i in range(1, 4):
|
2017-07-17 07:25:09 +08:00
|
|
|
# print "trying start in line %r" % self.source[i]
|
2015-11-27 22:43:01 +08:00
|
|
|
s = self.source.getstatement(i)
|
2018-05-23 22:48:46 +08:00
|
|
|
# x = s.deindent()
|
2015-11-27 22:43:01 +08:00
|
|
|
assert str(s) == ass
|
|
|
|
|
|
|
|
def test_getstatementrange_triple_quoted(self):
|
2017-07-17 07:25:09 +08:00
|
|
|
# print str(self.source)
|
2018-05-23 22:48:46 +08:00
|
|
|
source = Source(
|
|
|
|
"""hello('''
|
|
|
|
''')"""
|
|
|
|
)
|
2015-11-27 22:43:01 +08:00
|
|
|
s = source.getstatement(0)
|
|
|
|
assert s == str(source)
|
|
|
|
s = source.getstatement(1)
|
|
|
|
assert s == str(source)
|
|
|
|
|
|
|
|
@astonly
|
|
|
|
def test_getstatementrange_within_constructs(self):
|
2018-05-23 22:48:46 +08:00
|
|
|
source = Source(
|
|
|
|
"""\
|
2015-11-27 22:43:01 +08:00
|
|
|
try:
|
|
|
|
try:
|
|
|
|
raise ValueError
|
|
|
|
except SomeThing:
|
|
|
|
pass
|
|
|
|
finally:
|
|
|
|
42
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2015-11-27 22:43:01 +08:00
|
|
|
assert len(source) == 7
|
|
|
|
# check all lineno's that could occur in a traceback
|
2017-07-17 07:25:09 +08:00
|
|
|
# assert source.getstatementrange(0) == (0, 7)
|
|
|
|
# assert source.getstatementrange(1) == (1, 5)
|
2015-11-27 22:43:01 +08:00
|
|
|
assert source.getstatementrange(2) == (2, 3)
|
|
|
|
assert source.getstatementrange(3) == (3, 4)
|
|
|
|
assert source.getstatementrange(4) == (4, 5)
|
2017-07-17 07:25:09 +08:00
|
|
|
# assert source.getstatementrange(5) == (0, 7)
|
2015-11-27 22:43:01 +08:00
|
|
|
assert source.getstatementrange(6) == (6, 7)
|
|
|
|
|
|
|
|
def test_getstatementrange_bug(self):
|
2018-05-23 22:48:46 +08:00
|
|
|
source = Source(
|
|
|
|
"""\
|
2015-11-27 22:43:01 +08:00
|
|
|
try:
|
|
|
|
x = (
|
|
|
|
y +
|
|
|
|
z)
|
|
|
|
except:
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2015-11-27 22:43:01 +08:00
|
|
|
assert len(source) == 6
|
|
|
|
assert source.getstatementrange(2) == (1, 4)
|
|
|
|
|
|
|
|
def test_getstatementrange_bug2(self):
|
2018-05-23 22:48:46 +08:00
|
|
|
source = Source(
|
|
|
|
"""\
|
2015-11-27 22:43:01 +08:00
|
|
|
assert (
|
|
|
|
33
|
|
|
|
==
|
|
|
|
[
|
|
|
|
X(3,
|
|
|
|
b=1, c=2
|
|
|
|
),
|
|
|
|
]
|
|
|
|
)
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2015-11-27 22:43:01 +08:00
|
|
|
assert len(source) == 9
|
|
|
|
assert source.getstatementrange(5) == (0, 9)
|
|
|
|
|
|
|
|
def test_getstatementrange_ast_issue58(self):
|
2018-05-23 22:48:46 +08:00
|
|
|
source = Source(
|
|
|
|
"""\
|
2015-11-27 22:43:01 +08:00
|
|
|
|
|
|
|
def test_some():
|
|
|
|
for a in [a for a in
|
|
|
|
CAUSE_ERROR]: pass
|
|
|
|
|
|
|
|
x = 3
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2015-11-27 22:43:01 +08:00
|
|
|
assert getstatement(2, source).lines == source.lines[2:3]
|
|
|
|
assert getstatement(3, source).lines == source.lines[3:4]
|
|
|
|
|
|
|
|
def test_getstatementrange_out_of_bounds_py3(self):
|
|
|
|
source = Source("if xxx:\n from .collections import something")
|
|
|
|
r = source.getstatementrange(1)
|
2017-07-17 07:25:08 +08:00
|
|
|
assert r == (1, 2)
|
2015-11-27 22:43:01 +08:00
|
|
|
|
|
|
|
def test_getstatementrange_with_syntaxerror_issue7(self):
|
|
|
|
source = Source(":")
|
|
|
|
pytest.raises(SyntaxError, lambda: source.getstatementrange(0))
|
|
|
|
|
|
|
|
def test_compile_to_ast(self):
|
|
|
|
import ast
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
source = Source("x = 4")
|
|
|
|
mod = source.compile(flag=ast.PyCF_ONLY_AST)
|
|
|
|
assert isinstance(mod, ast.Module)
|
|
|
|
compile(mod, "<filename>", "exec")
|
|
|
|
|
|
|
|
def test_compile_and_getsource(self):
|
|
|
|
co = self.source.compile()
|
|
|
|
py.builtin.exec_(co, globals())
|
|
|
|
f(7)
|
|
|
|
excinfo = pytest.raises(AssertionError, "f(6)")
|
|
|
|
frame = excinfo.traceback[-1].frame
|
|
|
|
stmt = frame.code.fullsource.getstatement(frame.lineno)
|
2017-07-17 07:25:09 +08:00
|
|
|
# print "block", str(block)
|
2018-05-23 22:48:46 +08:00
|
|
|
assert str(stmt).strip().startswith("assert")
|
2015-11-27 22:43:01 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
@pytest.mark.parametrize("name", ["", None, "my"])
|
2016-07-12 08:06:35 +08:00
|
|
|
def test_compilefuncs_and_path_sanity(self, name):
|
2015-11-27 22:43:01 +08:00
|
|
|
def check(comp, name):
|
|
|
|
co = comp(self.source, name)
|
|
|
|
if not name:
|
2018-06-25 10:09:09 +08:00
|
|
|
expected = "codegen %s:%d>" % (mypath, mylineno + 2 + 2)
|
2015-11-27 22:43:01 +08:00
|
|
|
else:
|
2018-06-25 10:09:09 +08:00
|
|
|
expected = "codegen %r %s:%d>" % (name, mypath, mylineno + 2 + 2)
|
2015-11-27 22:43:01 +08:00
|
|
|
fn = co.co_filename
|
|
|
|
assert fn.endswith(expected)
|
|
|
|
|
|
|
|
mycode = _pytest._code.Code(self.test_compilefuncs_and_path_sanity)
|
|
|
|
mylineno = mycode.firstlineno
|
|
|
|
mypath = mycode.path
|
|
|
|
|
|
|
|
for comp in _pytest._code.compile, _pytest._code.Source.compile:
|
2016-07-12 08:06:35 +08:00
|
|
|
check(comp, name)
|
2015-11-27 22:43:01 +08:00
|
|
|
|
|
|
|
def test_offsetless_synerr(self):
|
2018-05-23 22:48:46 +08:00
|
|
|
pytest.raises(SyntaxError, _pytest._code.compile, "lambda a,a: 0", mode="eval")
|
2015-11-27 22:43:01 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
def test_getstartingblock_singleline():
|
2017-02-17 02:41:51 +08:00
|
|
|
class A(object):
|
2015-11-27 22:43:01 +08:00
|
|
|
def __init__(self, *args):
|
|
|
|
frame = sys._getframe(1)
|
|
|
|
self.source = _pytest._code.Frame(frame).statement
|
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
x = A("x", "y")
|
2015-11-27 22:43:01 +08:00
|
|
|
|
2017-11-04 23:17:20 +08:00
|
|
|
values = [i for i in x.source.lines if i.strip()]
|
|
|
|
assert len(values) == 1
|
2015-11-27 22:43:01 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
def test_getline_finally():
|
2018-05-23 22:48:46 +08:00
|
|
|
def c():
|
|
|
|
pass
|
|
|
|
|
|
|
|
excinfo = pytest.raises(
|
|
|
|
TypeError,
|
|
|
|
"""
|
2015-11-27 22:43:01 +08:00
|
|
|
teardown = None
|
|
|
|
try:
|
|
|
|
c(1)
|
|
|
|
finally:
|
|
|
|
if teardown:
|
|
|
|
teardown()
|
2018-05-23 22:48:46 +08:00
|
|
|
""",
|
|
|
|
)
|
2015-11-27 22:43:01 +08:00
|
|
|
source = excinfo.traceback[-1].statement
|
2018-05-23 22:48:46 +08:00
|
|
|
assert str(source).strip() == "c(1)"
|
2015-11-27 22:43:01 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
def test_getfuncsource_dynamic():
|
|
|
|
source = """
|
|
|
|
def f():
|
|
|
|
raise ValueError
|
|
|
|
|
|
|
|
def g(): pass
|
|
|
|
"""
|
|
|
|
co = _pytest._code.compile(source)
|
|
|
|
py.builtin.exec_(co, globals())
|
2018-05-23 22:48:46 +08:00
|
|
|
assert str(_pytest._code.Source(f)).strip() == "def f():\n raise ValueError"
|
|
|
|
assert str(_pytest._code.Source(g)).strip() == "def g(): pass"
|
2015-11-27 22:43:01 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_getfuncsource_with_multine_string():
|
|
|
|
def f():
|
2018-05-23 22:48:46 +08:00
|
|
|
c = """while True:
|
2015-11-27 22:43:01 +08:00
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
assert (
|
|
|
|
str(_pytest._code.Source(f)).strip()
|
2018-05-24 00:22:18 +08:00
|
|
|
== 'def f():\n c = """while True:\n pass\n"""'
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2015-11-27 22:43:01 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_deindent():
|
|
|
|
from _pytest._code.source import deindent as deindent
|
2018-05-23 22:48:46 +08:00
|
|
|
|
|
|
|
assert deindent(["\tfoo", "\tbar"]) == ["foo", "bar"]
|
2015-11-27 22:43:01 +08:00
|
|
|
|
|
|
|
def f():
|
2018-05-23 22:48:46 +08:00
|
|
|
c = """while True:
|
2015-11-27 22:43:01 +08:00
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
lines = deindent(inspect.getsource(f).splitlines())
|
2018-05-24 00:22:18 +08:00
|
|
|
assert lines == ["def f():", ' c = """while True:', " pass", '"""']
|
2015-11-27 22:43:01 +08:00
|
|
|
|
|
|
|
source = """
|
|
|
|
def f():
|
|
|
|
def g():
|
|
|
|
pass
|
|
|
|
"""
|
|
|
|
lines = deindent(source.splitlines())
|
2018-05-23 22:48:46 +08:00
|
|
|
assert lines == ["", "def f():", " def g():", " pass", " "]
|
2015-11-27 22:43:01 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
def test_source_of_class_at_eof_without_newline(tmpdir):
|
|
|
|
# this test fails because the implicit inspect.getsource(A) below
|
|
|
|
# does not return the "x = 1" last line.
|
2018-05-23 22:48:46 +08:00
|
|
|
source = _pytest._code.Source(
|
|
|
|
"""
|
2015-11-27 22:43:01 +08:00
|
|
|
class A(object):
|
|
|
|
def method(self):
|
|
|
|
x = 1
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2015-11-27 22:43:01 +08:00
|
|
|
path = tmpdir.join("a.py")
|
|
|
|
path.write(source)
|
|
|
|
s2 = _pytest._code.Source(tmpdir.join("a.py").pyimport().A)
|
|
|
|
assert str(source).strip() == str(s2).strip()
|
|
|
|
|
2017-07-17 07:25:06 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
if True:
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
def x():
|
|
|
|
pass
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
def test_getsource_fallback():
|
|
|
|
from _pytest._code.source import getsource
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
expected = """def x():
|
|
|
|
pass"""
|
|
|
|
src = getsource(x)
|
|
|
|
assert src == expected
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
def test_idem_compile_and_getsource():
|
|
|
|
from _pytest._code.source import getsource
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
expected = "def x(): pass"
|
|
|
|
co = _pytest._code.compile(expected)
|
|
|
|
src = getsource(co)
|
|
|
|
assert src == expected
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
def test_findsource_fallback():
|
|
|
|
from _pytest._code.source import findsource
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
src, lineno = findsource(x)
|
2018-05-23 22:48:46 +08:00
|
|
|
assert "test_findsource_simple" in str(src)
|
|
|
|
assert src[lineno] == " def x():"
|
2015-11-27 22:43:01 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
def test_findsource():
|
|
|
|
from _pytest._code.source import findsource
|
2018-05-23 22:48:46 +08:00
|
|
|
|
|
|
|
co = _pytest._code.compile(
|
|
|
|
"""if 1:
|
2015-11-27 22:43:01 +08:00
|
|
|
def x():
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2015-11-27 22:43:01 +08:00
|
|
|
|
|
|
|
src, lineno = findsource(co)
|
2018-05-23 22:48:46 +08:00
|
|
|
assert "if 1:" in str(src)
|
2015-11-27 22:43:01 +08:00
|
|
|
|
|
|
|
d = {}
|
|
|
|
eval(co, d)
|
2018-05-23 22:48:46 +08:00
|
|
|
src, lineno = findsource(d["x"])
|
|
|
|
assert "if 1:" in str(src)
|
2015-11-27 22:43:01 +08:00
|
|
|
assert src[lineno] == " def x():"
|
|
|
|
|
|
|
|
|
|
|
|
def test_getfslineno():
|
|
|
|
from _pytest._code import getfslineno
|
|
|
|
|
|
|
|
def f(x):
|
|
|
|
pass
|
|
|
|
|
|
|
|
fspath, lineno = getfslineno(f)
|
|
|
|
|
|
|
|
assert fspath.basename == "test_source.py"
|
2017-07-17 07:25:09 +08:00
|
|
|
assert lineno == _pytest._code.getrawcode(f).co_firstlineno - 1 # see findsource
|
2015-11-27 22:43:01 +08:00
|
|
|
|
|
|
|
class A(object):
|
|
|
|
pass
|
|
|
|
|
|
|
|
fspath, lineno = getfslineno(A)
|
|
|
|
|
2017-12-27 11:47:26 +08:00
|
|
|
_, A_lineno = inspect.findsource(A)
|
2015-11-27 22:43:01 +08:00
|
|
|
assert fspath.basename == "test_source.py"
|
|
|
|
assert lineno == A_lineno
|
|
|
|
|
|
|
|
assert getfslineno(3) == ("", -1)
|
2017-07-17 07:25:06 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class B(object):
|
2015-11-27 22:43:01 +08:00
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
B.__name__ = "B2"
|
|
|
|
assert getfslineno(B)[1] == -1
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
def test_code_of_object_instance_with_call():
|
2017-02-17 02:41:51 +08:00
|
|
|
class A(object):
|
2015-11-27 22:43:01 +08:00
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
pytest.raises(TypeError, lambda: _pytest._code.Source(A()))
|
2017-07-17 07:25:06 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class WithCall(object):
|
2015-11-27 22:43:01 +08:00
|
|
|
def __call__(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
code = _pytest._code.Code(WithCall())
|
2018-05-23 22:48:46 +08:00
|
|
|
assert "pass" in str(code.source())
|
2015-11-27 22:43:01 +08:00
|
|
|
|
|
|
|
class Hello(object):
|
|
|
|
def __call__(self):
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
pytest.raises(TypeError, lambda: _pytest._code.Code(Hello))
|
|
|
|
|
|
|
|
|
|
|
|
def getstatement(lineno, source):
|
|
|
|
from _pytest._code.source import getstatementrange_ast
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
source = _pytest._code.Source(source, deindent=False)
|
|
|
|
ast, start, end = getstatementrange_ast(lineno, source)
|
|
|
|
return source[start:end]
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
def test_oneline():
|
|
|
|
source = getstatement(0, "raise ValueError")
|
|
|
|
assert str(source) == "raise ValueError"
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
def test_comment_and_no_newline_at_end():
|
|
|
|
from _pytest._code.source import getstatementrange_ast
|
2018-05-23 22:48:46 +08:00
|
|
|
|
|
|
|
source = Source(
|
|
|
|
[
|
|
|
|
"def test_basic_complex():",
|
|
|
|
" assert 1 == 2",
|
|
|
|
"# vim: filetype=pyopencl:fdm=marker",
|
|
|
|
]
|
|
|
|
)
|
2015-11-27 22:43:01 +08:00
|
|
|
ast, start, end = getstatementrange_ast(1, source)
|
|
|
|
assert end == 2
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
def test_oneline_and_comment():
|
|
|
|
source = getstatement(0, "raise ValueError\n#hello")
|
|
|
|
assert str(source) == "raise ValueError"
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
@pytest.mark.xfail(hasattr(sys, "pypy_version_info"), reason="does not work on pypy")
|
2015-11-27 22:43:01 +08:00
|
|
|
def test_comments():
|
|
|
|
source = '''def test():
|
|
|
|
"comment 1"
|
|
|
|
x = 1
|
|
|
|
# comment 2
|
|
|
|
# comment 3
|
|
|
|
|
|
|
|
assert False
|
|
|
|
|
|
|
|
"""
|
|
|
|
comment 4
|
|
|
|
"""
|
|
|
|
'''
|
2017-07-17 07:25:08 +08:00
|
|
|
for line in range(2, 6):
|
2018-05-23 22:48:46 +08:00
|
|
|
assert str(getstatement(line, source)) == " x = 1"
|
2017-07-17 07:25:08 +08:00
|
|
|
for line in range(6, 10):
|
2018-05-23 22:48:46 +08:00
|
|
|
assert str(getstatement(line, source)) == " assert False"
|
2015-11-27 22:43:01 +08:00
|
|
|
assert str(getstatement(10, source)) == '"""'
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
def test_comment_in_statement():
|
2018-05-23 22:48:46 +08:00
|
|
|
source = """test(foo=1,
|
2015-11-27 22:43:01 +08:00
|
|
|
# comment 1
|
|
|
|
bar=2)
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2017-07-17 07:25:08 +08:00
|
|
|
for line in range(1, 3):
|
2018-05-23 22:48:46 +08:00
|
|
|
assert (
|
|
|
|
str(getstatement(line, source))
|
|
|
|
== "test(foo=1,\n # comment 1\n bar=2)"
|
|
|
|
)
|
2015-11-27 22:43:01 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
def test_single_line_else():
|
|
|
|
source = getstatement(1, "if False: 2\nelse: 3")
|
|
|
|
assert str(source) == "else: 3"
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
def test_single_line_finally():
|
|
|
|
source = getstatement(1, "try: 1\nfinally: 3")
|
|
|
|
assert str(source) == "finally: 3"
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
def test_issue55():
|
2018-05-23 22:48:46 +08:00
|
|
|
source = (
|
|
|
|
"def round_trip(dinp):\n assert 1 == dinp\n"
|
|
|
|
'def test_rt():\n round_trip("""\n""")\n'
|
|
|
|
)
|
2015-11-27 22:43:01 +08:00
|
|
|
s = getstatement(3, source)
|
|
|
|
assert str(s) == ' round_trip("""\n""")'
|
|
|
|
|
|
|
|
|
|
|
|
def XXXtest_multiline():
|
2018-05-23 22:48:46 +08:00
|
|
|
source = getstatement(
|
|
|
|
0,
|
|
|
|
"""\
|
2015-11-27 22:43:01 +08:00
|
|
|
raise ValueError(
|
|
|
|
23
|
|
|
|
)
|
|
|
|
x = 3
|
2018-05-23 22:48:46 +08:00
|
|
|
""",
|
|
|
|
)
|
2015-11-27 22:43:01 +08:00
|
|
|
assert str(source) == "raise ValueError(\n 23\n)"
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestTry(object):
|
2015-11-27 22:43:01 +08:00
|
|
|
pytestmark = astonly
|
|
|
|
source = """\
|
|
|
|
try:
|
|
|
|
raise ValueError
|
|
|
|
except Something:
|
|
|
|
raise IndexError(1)
|
|
|
|
else:
|
|
|
|
raise KeyError()
|
|
|
|
"""
|
|
|
|
|
|
|
|
def test_body(self):
|
|
|
|
source = getstatement(1, self.source)
|
|
|
|
assert str(source) == " raise ValueError"
|
|
|
|
|
|
|
|
def test_except_line(self):
|
|
|
|
source = getstatement(2, self.source)
|
|
|
|
assert str(source) == "except Something:"
|
|
|
|
|
|
|
|
def test_except_body(self):
|
|
|
|
source = getstatement(3, self.source)
|
|
|
|
assert str(source) == " raise IndexError(1)"
|
|
|
|
|
|
|
|
def test_else(self):
|
|
|
|
source = getstatement(5, self.source)
|
|
|
|
assert str(source) == " raise KeyError()"
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestTryFinally(object):
|
2015-11-27 22:43:01 +08:00
|
|
|
source = """\
|
|
|
|
try:
|
|
|
|
raise ValueError
|
|
|
|
finally:
|
|
|
|
raise IndexError(1)
|
|
|
|
"""
|
|
|
|
|
|
|
|
def test_body(self):
|
|
|
|
source = getstatement(1, self.source)
|
|
|
|
assert str(source) == " raise ValueError"
|
|
|
|
|
|
|
|
def test_finally(self):
|
|
|
|
source = getstatement(3, self.source)
|
|
|
|
assert str(source) == " raise IndexError(1)"
|
|
|
|
|
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestIf(object):
|
2015-11-27 22:43:01 +08:00
|
|
|
pytestmark = astonly
|
|
|
|
source = """\
|
|
|
|
if 1:
|
|
|
|
y = 3
|
|
|
|
elif False:
|
|
|
|
y = 5
|
|
|
|
else:
|
|
|
|
y = 7
|
|
|
|
"""
|
|
|
|
|
|
|
|
def test_body(self):
|
|
|
|
source = getstatement(1, self.source)
|
|
|
|
assert str(source) == " y = 3"
|
|
|
|
|
|
|
|
def test_elif_clause(self):
|
|
|
|
source = getstatement(2, self.source)
|
|
|
|
assert str(source) == "elif False:"
|
|
|
|
|
|
|
|
def test_elif(self):
|
|
|
|
source = getstatement(3, self.source)
|
|
|
|
assert str(source) == " y = 5"
|
|
|
|
|
|
|
|
def test_else(self):
|
|
|
|
source = getstatement(5, self.source)
|
|
|
|
assert str(source) == " y = 7"
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
def test_semicolon():
|
|
|
|
s = """\
|
|
|
|
hello ; pytest.skip()
|
|
|
|
"""
|
|
|
|
source = getstatement(0, s)
|
|
|
|
assert str(source) == s.strip()
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
def test_def_online():
|
|
|
|
s = """\
|
|
|
|
def func(): raise ValueError(42)
|
|
|
|
|
|
|
|
def something():
|
|
|
|
pass
|
|
|
|
"""
|
|
|
|
source = getstatement(0, s)
|
|
|
|
assert str(source) == "def func(): raise ValueError(42)"
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
def XXX_test_expression_multiline():
|
|
|
|
source = """\
|
|
|
|
something
|
|
|
|
'''
|
|
|
|
'''"""
|
|
|
|
result = getstatement(1, source)
|
|
|
|
assert str(result) == "'''\n'''"
|