run black
This commit is contained in:
@@ -23,14 +23,20 @@ def test_source_str_function():
|
||||
x = Source(" 3")
|
||||
assert str(x) == "3"
|
||||
|
||||
x = Source("""
|
||||
x = Source(
|
||||
"""
|
||||
3
|
||||
""", rstrip=False)
|
||||
""",
|
||||
rstrip=False,
|
||||
)
|
||||
assert str(x) == "\n3\n "
|
||||
|
||||
x = Source("""
|
||||
x = Source(
|
||||
"""
|
||||
3
|
||||
""", rstrip=True)
|
||||
""",
|
||||
rstrip=True,
|
||||
)
|
||||
assert str(x) == "\n3"
|
||||
|
||||
|
||||
@@ -41,70 +47,81 @@ def test_unicode():
|
||||
return
|
||||
x = Source(unicode("4"))
|
||||
assert str(x) == "4"
|
||||
co = _pytest._code.compile(unicode('u"\xc3\xa5"', 'utf8'), mode='eval')
|
||||
co = _pytest._code.compile(unicode('u"\xc3\xa5"', "utf8"), mode="eval")
|
||||
val = eval(co)
|
||||
assert isinstance(val, unicode)
|
||||
|
||||
|
||||
def test_source_from_function():
|
||||
source = _pytest._code.Source(test_source_str_function)
|
||||
assert str(source).startswith('def test_source_str_function():')
|
||||
assert str(source).startswith("def test_source_str_function():")
|
||||
|
||||
|
||||
def test_source_from_method():
|
||||
|
||||
class TestClass(object):
|
||||
|
||||
def test_method(self):
|
||||
pass
|
||||
|
||||
source = _pytest._code.Source(TestClass().test_method)
|
||||
assert source.lines == ["def test_method(self):",
|
||||
" pass"]
|
||||
assert source.lines == ["def test_method(self):", " pass"]
|
||||
|
||||
|
||||
def test_source_from_lines():
|
||||
lines = ["a \n", "b\n", "c"]
|
||||
source = _pytest._code.Source(lines)
|
||||
assert source.lines == ['a ', 'b', 'c']
|
||||
assert source.lines == ["a ", "b", "c"]
|
||||
|
||||
|
||||
def test_source_from_inner_function():
|
||||
|
||||
def f():
|
||||
pass
|
||||
|
||||
source = _pytest._code.Source(f, deindent=False)
|
||||
assert str(source).startswith(' def f():')
|
||||
assert str(source).startswith(" def f():")
|
||||
source = _pytest._code.Source(f)
|
||||
assert str(source).startswith('def f():')
|
||||
assert str(source).startswith("def f():")
|
||||
|
||||
|
||||
def test_source_putaround_simple():
|
||||
source = Source("raise ValueError")
|
||||
source = source.putaround(
|
||||
"try:", """\
|
||||
"try:",
|
||||
"""\
|
||||
except ValueError:
|
||||
x = 42
|
||||
else:
|
||||
x = 23""")
|
||||
assert str(source) == """\
|
||||
x = 23""",
|
||||
)
|
||||
assert (
|
||||
str(source)
|
||||
== """\
|
||||
try:
|
||||
raise ValueError
|
||||
except ValueError:
|
||||
x = 42
|
||||
else:
|
||||
x = 23"""
|
||||
)
|
||||
|
||||
|
||||
def test_source_putaround():
|
||||
source = Source()
|
||||
source = source.putaround("""
|
||||
source = source.putaround(
|
||||
"""
|
||||
if 1:
|
||||
x=1
|
||||
""")
|
||||
"""
|
||||
)
|
||||
assert str(source).strip() == "if 1:\n x=1"
|
||||
|
||||
|
||||
def test_source_strips():
|
||||
source = Source("")
|
||||
assert source == Source()
|
||||
assert str(source) == ''
|
||||
assert str(source) == ""
|
||||
assert source.strip() == source
|
||||
|
||||
|
||||
@@ -116,10 +133,10 @@ def test_source_strip_multiline():
|
||||
|
||||
|
||||
def test_syntaxerror_rerepresentation():
|
||||
ex = pytest.raises(SyntaxError, _pytest._code.compile, 'xyz xyz')
|
||||
ex = pytest.raises(SyntaxError, _pytest._code.compile, "xyz xyz")
|
||||
assert ex.value.lineno == 1
|
||||
assert ex.value.offset in (4, 7) # XXX pypy/jython versus cpython?
|
||||
assert ex.value.text.strip(), 'x x'
|
||||
assert ex.value.text.strip(), "x x"
|
||||
|
||||
|
||||
def test_isparseable():
|
||||
@@ -132,12 +149,14 @@ def test_isparseable():
|
||||
|
||||
|
||||
class TestAccesses(object):
|
||||
source = Source("""\
|
||||
source = Source(
|
||||
"""\
|
||||
def f(x):
|
||||
pass
|
||||
def g(x):
|
||||
pass
|
||||
""")
|
||||
"""
|
||||
)
|
||||
|
||||
def test_getrange(self):
|
||||
x = self.source[0:2]
|
||||
@@ -158,18 +177,20 @@ class TestAccesses(object):
|
||||
|
||||
|
||||
class TestSourceParsingAndCompiling(object):
|
||||
source = Source("""\
|
||||
source = Source(
|
||||
"""\
|
||||
def f(x):
|
||||
assert (x ==
|
||||
3 +
|
||||
4)
|
||||
""").strip()
|
||||
"""
|
||||
).strip()
|
||||
|
||||
def test_compile(self):
|
||||
co = _pytest._code.compile("x=3")
|
||||
d = {}
|
||||
exec(co, d)
|
||||
assert d['x'] == 3
|
||||
assert d["x"] == 3
|
||||
|
||||
def test_compile_and_getsource_simple(self):
|
||||
co = _pytest._code.compile("x=3")
|
||||
@@ -178,20 +199,26 @@ class TestSourceParsingAndCompiling(object):
|
||||
assert str(source) == "x=3"
|
||||
|
||||
def test_compile_and_getsource_through_same_function(self):
|
||||
|
||||
def gensource(source):
|
||||
return _pytest._code.compile(source)
|
||||
co1 = gensource("""
|
||||
|
||||
co1 = gensource(
|
||||
"""
|
||||
def f():
|
||||
raise KeyError()
|
||||
""")
|
||||
co2 = gensource("""
|
||||
"""
|
||||
)
|
||||
co2 = gensource(
|
||||
"""
|
||||
def f():
|
||||
raise ValueError()
|
||||
""")
|
||||
"""
|
||||
)
|
||||
source1 = inspect.getsource(co1)
|
||||
assert 'KeyError' in source1
|
||||
assert "KeyError" in source1
|
||||
source2 = inspect.getsource(co2)
|
||||
assert 'ValueError' in source2
|
||||
assert "ValueError" in source2
|
||||
|
||||
def test_getstatement(self):
|
||||
# print str(self.source)
|
||||
@@ -199,13 +226,15 @@ class TestSourceParsingAndCompiling(object):
|
||||
for i in range(1, 4):
|
||||
# print "trying start in line %r" % self.source[i]
|
||||
s = self.source.getstatement(i)
|
||||
#x = s.deindent()
|
||||
# x = s.deindent()
|
||||
assert str(s) == ass
|
||||
|
||||
def test_getstatementrange_triple_quoted(self):
|
||||
# print str(self.source)
|
||||
source = Source("""hello('''
|
||||
''')""")
|
||||
source = Source(
|
||||
"""hello('''
|
||||
''')"""
|
||||
)
|
||||
s = source.getstatement(0)
|
||||
assert s == str(source)
|
||||
s = source.getstatement(1)
|
||||
@@ -213,7 +242,8 @@ class TestSourceParsingAndCompiling(object):
|
||||
|
||||
@astonly
|
||||
def test_getstatementrange_within_constructs(self):
|
||||
source = Source("""\
|
||||
source = Source(
|
||||
"""\
|
||||
try:
|
||||
try:
|
||||
raise ValueError
|
||||
@@ -221,7 +251,8 @@ class TestSourceParsingAndCompiling(object):
|
||||
pass
|
||||
finally:
|
||||
42
|
||||
""")
|
||||
"""
|
||||
)
|
||||
assert len(source) == 7
|
||||
# check all lineno's that could occur in a traceback
|
||||
# assert source.getstatementrange(0) == (0, 7)
|
||||
@@ -233,19 +264,22 @@ class TestSourceParsingAndCompiling(object):
|
||||
assert source.getstatementrange(6) == (6, 7)
|
||||
|
||||
def test_getstatementrange_bug(self):
|
||||
source = Source("""\
|
||||
source = Source(
|
||||
"""\
|
||||
try:
|
||||
x = (
|
||||
y +
|
||||
z)
|
||||
except:
|
||||
pass
|
||||
""")
|
||||
"""
|
||||
)
|
||||
assert len(source) == 6
|
||||
assert source.getstatementrange(2) == (1, 4)
|
||||
|
||||
def test_getstatementrange_bug2(self):
|
||||
source = Source("""\
|
||||
source = Source(
|
||||
"""\
|
||||
assert (
|
||||
33
|
||||
==
|
||||
@@ -255,19 +289,22 @@ class TestSourceParsingAndCompiling(object):
|
||||
),
|
||||
]
|
||||
)
|
||||
""")
|
||||
"""
|
||||
)
|
||||
assert len(source) == 9
|
||||
assert source.getstatementrange(5) == (0, 9)
|
||||
|
||||
def test_getstatementrange_ast_issue58(self):
|
||||
source = Source("""\
|
||||
source = Source(
|
||||
"""\
|
||||
|
||||
def test_some():
|
||||
for a in [a for a in
|
||||
CAUSE_ERROR]: pass
|
||||
|
||||
x = 3
|
||||
""")
|
||||
"""
|
||||
)
|
||||
assert getstatement(2, source).lines == source.lines[2:3]
|
||||
assert getstatement(3, source).lines == source.lines[3:4]
|
||||
|
||||
@@ -282,6 +319,7 @@ class TestSourceParsingAndCompiling(object):
|
||||
|
||||
def test_compile_to_ast(self):
|
||||
import ast
|
||||
|
||||
source = Source("x = 4")
|
||||
mod = source.compile(flag=ast.PyCF_ONLY_AST)
|
||||
assert isinstance(mod, ast.Module)
|
||||
@@ -295,10 +333,11 @@ class TestSourceParsingAndCompiling(object):
|
||||
frame = excinfo.traceback[-1].frame
|
||||
stmt = frame.code.fullsource.getstatement(frame.lineno)
|
||||
# print "block", str(block)
|
||||
assert str(stmt).strip().startswith('assert')
|
||||
assert str(stmt).strip().startswith("assert")
|
||||
|
||||
@pytest.mark.parametrize('name', ['', None, 'my'])
|
||||
@pytest.mark.parametrize("name", ["", None, "my"])
|
||||
def test_compilefuncs_and_path_sanity(self, name):
|
||||
|
||||
def check(comp, name):
|
||||
co = comp(self.source, name)
|
||||
if not name:
|
||||
@@ -316,33 +355,41 @@ class TestSourceParsingAndCompiling(object):
|
||||
check(comp, name)
|
||||
|
||||
def test_offsetless_synerr(self):
|
||||
pytest.raises(SyntaxError, _pytest._code.compile, "lambda a,a: 0", mode='eval')
|
||||
pytest.raises(SyntaxError, _pytest._code.compile, "lambda a,a: 0", mode="eval")
|
||||
|
||||
|
||||
def test_getstartingblock_singleline():
|
||||
|
||||
class A(object):
|
||||
|
||||
def __init__(self, *args):
|
||||
frame = sys._getframe(1)
|
||||
self.source = _pytest._code.Frame(frame).statement
|
||||
|
||||
x = A('x', 'y')
|
||||
x = A("x", "y")
|
||||
|
||||
values = [i for i in x.source.lines if i.strip()]
|
||||
assert len(values) == 1
|
||||
|
||||
|
||||
def test_getline_finally():
|
||||
def c(): pass
|
||||
excinfo = pytest.raises(TypeError, """
|
||||
|
||||
def c():
|
||||
pass
|
||||
|
||||
excinfo = pytest.raises(
|
||||
TypeError,
|
||||
"""
|
||||
teardown = None
|
||||
try:
|
||||
c(1)
|
||||
finally:
|
||||
if teardown:
|
||||
teardown()
|
||||
""")
|
||||
""",
|
||||
)
|
||||
source = excinfo.traceback[-1].statement
|
||||
assert str(source).strip() == 'c(1)'
|
||||
assert str(source).strip() == "c(1)"
|
||||
|
||||
|
||||
def test_getfuncsource_dynamic():
|
||||
@@ -354,26 +401,33 @@ def test_getfuncsource_dynamic():
|
||||
"""
|
||||
co = _pytest._code.compile(source)
|
||||
py.builtin.exec_(co, globals())
|
||||
assert str(_pytest._code.Source(f)).strip() == 'def f():\n raise ValueError'
|
||||
assert str(_pytest._code.Source(g)).strip() == 'def g(): pass'
|
||||
assert str(_pytest._code.Source(f)).strip() == "def f():\n raise ValueError"
|
||||
assert str(_pytest._code.Source(g)).strip() == "def g(): pass"
|
||||
|
||||
|
||||
def test_getfuncsource_with_multine_string():
|
||||
|
||||
def f():
|
||||
c = '''while True:
|
||||
c = """while True:
|
||||
pass
|
||||
'''
|
||||
assert str(_pytest._code.Source(f)).strip() == "def f():\n c = '''while True:\n pass\n'''"
|
||||
"""
|
||||
|
||||
assert (
|
||||
str(_pytest._code.Source(f)).strip()
|
||||
== "def f():\n c = '''while True:\n pass\n'''"
|
||||
)
|
||||
|
||||
|
||||
def test_deindent():
|
||||
from _pytest._code.source import deindent as deindent
|
||||
assert deindent(['\tfoo', '\tbar', ]) == ['foo', 'bar']
|
||||
|
||||
assert deindent(["\tfoo", "\tbar"]) == ["foo", "bar"]
|
||||
|
||||
def f():
|
||||
c = '''while True:
|
||||
c = """while True:
|
||||
pass
|
||||
'''
|
||||
"""
|
||||
|
||||
lines = deindent(inspect.getsource(f).splitlines())
|
||||
assert lines == ["def f():", " c = '''while True:", " pass", "'''"]
|
||||
|
||||
@@ -383,17 +437,19 @@ def test_deindent():
|
||||
pass
|
||||
"""
|
||||
lines = deindent(source.splitlines())
|
||||
assert lines == ['', 'def f():', ' def g():', ' pass', ' ']
|
||||
assert lines == ["", "def f():", " def g():", " pass", " "]
|
||||
|
||||
|
||||
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.
|
||||
source = _pytest._code.Source('''
|
||||
source = _pytest._code.Source(
|
||||
"""
|
||||
class A(object):
|
||||
def method(self):
|
||||
x = 1
|
||||
''')
|
||||
"""
|
||||
)
|
||||
path = tmpdir.join("a.py")
|
||||
path.write(source)
|
||||
s2 = _pytest._code.Source(tmpdir.join("a.py").pyimport().A)
|
||||
@@ -401,12 +457,14 @@ def test_source_of_class_at_eof_without_newline(tmpdir):
|
||||
|
||||
|
||||
if True:
|
||||
|
||||
def x():
|
||||
pass
|
||||
|
||||
|
||||
def test_getsource_fallback():
|
||||
from _pytest._code.source import getsource
|
||||
|
||||
expected = """def x():
|
||||
pass"""
|
||||
src = getsource(x)
|
||||
@@ -415,6 +473,7 @@ def test_getsource_fallback():
|
||||
|
||||
def test_idem_compile_and_getsource():
|
||||
from _pytest._code.source import getsource
|
||||
|
||||
expected = "def x(): pass"
|
||||
co = _pytest._code.compile(expected)
|
||||
src = getsource(co)
|
||||
@@ -423,25 +482,29 @@ def test_idem_compile_and_getsource():
|
||||
|
||||
def test_findsource_fallback():
|
||||
from _pytest._code.source import findsource
|
||||
|
||||
src, lineno = findsource(x)
|
||||
assert 'test_findsource_simple' in str(src)
|
||||
assert src[lineno] == ' def x():'
|
||||
assert "test_findsource_simple" in str(src)
|
||||
assert src[lineno] == " def x():"
|
||||
|
||||
|
||||
def test_findsource():
|
||||
from _pytest._code.source import findsource
|
||||
co = _pytest._code.compile("""if 1:
|
||||
|
||||
co = _pytest._code.compile(
|
||||
"""if 1:
|
||||
def x():
|
||||
pass
|
||||
""")
|
||||
"""
|
||||
)
|
||||
|
||||
src, lineno = findsource(co)
|
||||
assert 'if 1:' in str(src)
|
||||
assert "if 1:" in str(src)
|
||||
|
||||
d = {}
|
||||
eval(co, d)
|
||||
src, lineno = findsource(d['x'])
|
||||
assert 'if 1:' in str(src)
|
||||
src, lineno = findsource(d["x"])
|
||||
assert "if 1:" in str(src)
|
||||
assert src[lineno] == " def x():"
|
||||
|
||||
|
||||
@@ -469,30 +532,37 @@ def test_getfslineno():
|
||||
|
||||
class B(object):
|
||||
pass
|
||||
|
||||
B.__name__ = "B2"
|
||||
assert getfslineno(B)[1] == -1
|
||||
|
||||
|
||||
def test_code_of_object_instance_with_call():
|
||||
|
||||
class A(object):
|
||||
pass
|
||||
|
||||
pytest.raises(TypeError, lambda: _pytest._code.Source(A()))
|
||||
|
||||
class WithCall(object):
|
||||
|
||||
def __call__(self):
|
||||
pass
|
||||
|
||||
code = _pytest._code.Code(WithCall())
|
||||
assert 'pass' in str(code.source())
|
||||
assert "pass" in str(code.source())
|
||||
|
||||
class Hello(object):
|
||||
|
||||
def __call__(self):
|
||||
pass
|
||||
|
||||
pytest.raises(TypeError, lambda: _pytest._code.Code(Hello))
|
||||
|
||||
|
||||
def getstatement(lineno, source):
|
||||
from _pytest._code.source import getstatementrange_ast
|
||||
|
||||
source = _pytest._code.Source(source, deindent=False)
|
||||
ast, start, end = getstatementrange_ast(lineno, source)
|
||||
return source[start:end]
|
||||
@@ -505,9 +575,14 @@ def test_oneline():
|
||||
|
||||
def test_comment_and_no_newline_at_end():
|
||||
from _pytest._code.source import getstatementrange_ast
|
||||
source = Source(['def test_basic_complex():',
|
||||
' assert 1 == 2',
|
||||
'# vim: filetype=pyopencl:fdm=marker'])
|
||||
|
||||
source = Source(
|
||||
[
|
||||
"def test_basic_complex():",
|
||||
" assert 1 == 2",
|
||||
"# vim: filetype=pyopencl:fdm=marker",
|
||||
]
|
||||
)
|
||||
ast, start, end = getstatementrange_ast(1, source)
|
||||
assert end == 2
|
||||
|
||||
@@ -517,8 +592,7 @@ def test_oneline_and_comment():
|
||||
assert str(source) == "raise ValueError"
|
||||
|
||||
|
||||
@pytest.mark.xfail(hasattr(sys, "pypy_version_info"),
|
||||
reason='does not work on pypy')
|
||||
@pytest.mark.xfail(hasattr(sys, "pypy_version_info"), reason="does not work on pypy")
|
||||
def test_comments():
|
||||
source = '''def test():
|
||||
"comment 1"
|
||||
@@ -533,20 +607,22 @@ comment 4
|
||||
"""
|
||||
'''
|
||||
for line in range(2, 6):
|
||||
assert str(getstatement(line, source)) == ' x = 1'
|
||||
assert str(getstatement(line, source)) == " x = 1"
|
||||
for line in range(6, 10):
|
||||
assert str(getstatement(line, source)) == ' assert False'
|
||||
assert str(getstatement(line, source)) == " assert False"
|
||||
assert str(getstatement(10, source)) == '"""'
|
||||
|
||||
|
||||
def test_comment_in_statement():
|
||||
source = '''test(foo=1,
|
||||
source = """test(foo=1,
|
||||
# comment 1
|
||||
bar=2)
|
||||
'''
|
||||
"""
|
||||
for line in range(1, 3):
|
||||
assert str(getstatement(line, source)) == \
|
||||
'test(foo=1,\n # comment 1\n bar=2)'
|
||||
assert (
|
||||
str(getstatement(line, source))
|
||||
== "test(foo=1,\n # comment 1\n bar=2)"
|
||||
)
|
||||
|
||||
|
||||
def test_single_line_else():
|
||||
@@ -560,19 +636,24 @@ def test_single_line_finally():
|
||||
|
||||
|
||||
def test_issue55():
|
||||
source = ('def round_trip(dinp):\n assert 1 == dinp\n'
|
||||
'def test_rt():\n round_trip("""\n""")\n')
|
||||
source = (
|
||||
"def round_trip(dinp):\n assert 1 == dinp\n"
|
||||
'def test_rt():\n round_trip("""\n""")\n'
|
||||
)
|
||||
s = getstatement(3, source)
|
||||
assert str(s) == ' round_trip("""\n""")'
|
||||
|
||||
|
||||
def XXXtest_multiline():
|
||||
source = getstatement(0, """\
|
||||
source = getstatement(
|
||||
0,
|
||||
"""\
|
||||
raise ValueError(
|
||||
23
|
||||
)
|
||||
x = 3
|
||||
""")
|
||||
""",
|
||||
)
|
||||
assert str(source) == "raise ValueError(\n 23\n)"
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user