13
13
14
14
import py .path
15
15
16
- import _pytest ._code
17
16
import pytest
18
- from _pytest ._code import getfslineno
17
+ from _pytest ._code import Code
18
+ from _pytest ._code import Frame
19
19
from _pytest ._code import Source
20
+ from _pytest ._code import getfslineno
20
21
21
22
22
23
def test_source_str_function () -> None :
@@ -35,7 +36,7 @@ def test_source_str_function() -> None:
35
36
36
37
37
38
def test_source_from_function () -> None :
38
- source = _pytest . _code . Source (test_source_str_function )
39
+ source = Source (test_source_str_function )
39
40
assert str (source ).startswith ("def test_source_str_function() -> None:" )
40
41
41
42
@@ -44,21 +45,21 @@ class TestClass:
44
45
def test_method (self ):
45
46
pass
46
47
47
- source = _pytest . _code . Source (TestClass ().test_method )
48
+ source = Source (TestClass ().test_method )
48
49
assert source .lines == ["def test_method(self):" , " pass" ]
49
50
50
51
51
52
def test_source_from_lines () -> None :
52
53
lines = ["a \n " , "b\n " , "c" ]
53
- source = _pytest . _code . Source (lines )
54
+ source = Source (lines )
54
55
assert source .lines == ["a " , "b" , "c" ]
55
56
56
57
57
58
def test_source_from_inner_function () -> None :
58
59
def f ():
59
60
raise NotImplementedError ()
60
61
61
- source = _pytest . _code . Source (f )
62
+ source = Source (f )
62
63
assert str (source ).startswith ("def f():" )
63
64
64
65
@@ -220,7 +221,7 @@ def test_getstartingblock_singleline() -> None:
220
221
class A :
221
222
def __init__ (self , * args ) -> None :
222
223
frame = sys ._getframe (1 )
223
- self .source = _pytest . _code . Frame (frame ).statement
224
+ self .source = Frame (frame ).statement
224
225
225
226
x = A ("x" , "y" )
226
227
@@ -250,8 +251,8 @@ def f():
250
251
def g ():
251
252
pass # pragma: no cover
252
253
253
- f_source = _pytest . _code . Source (f )
254
- g_source = _pytest . _code . Source (g )
254
+ f_source = Source (f )
255
+ g_source = Source (g )
255
256
assert str (f_source ).strip () == "def f():\n raise NotImplementedError()"
256
257
assert str (g_source ).strip () == "def g():\n pass # pragma: no cover"
257
258
@@ -268,7 +269,7 @@ def f():
268
269
pass
269
270
"""
270
271
'''
271
- assert str (_pytest . _code . Source (f )) == expected .rstrip ()
272
+ assert str (Source (f )) == expected .rstrip ()
272
273
273
274
274
275
def test_deindent () -> None :
@@ -288,7 +289,7 @@ def g():
288
289
def test_source_of_class_at_eof_without_newline (tmpdir , _sys_snapshot ) -> None :
289
290
# this test fails because the implicit inspect.getsource(A) below
290
291
# does not return the "x = 1" last line.
291
- source = _pytest . _code . Source (
292
+ source = Source (
292
293
"""
293
294
class A(object):
294
295
def method(self):
@@ -297,7 +298,7 @@ def method(self):
297
298
)
298
299
path = tmpdir .join ("a.py" )
299
300
path .write (source )
300
- s2 = _pytest . _code . Source (tmpdir .join ("a.py" ).pyimport ().A )
301
+ s2 = Source (tmpdir .join ("a.py" ).pyimport ().A )
301
302
assert str (source ).strip () == str (s2 ).strip ()
302
303
303
304
@@ -386,26 +387,26 @@ def test_code_of_object_instance_with_call() -> None:
386
387
class A :
387
388
pass
388
389
389
- pytest .raises (TypeError , lambda : _pytest . _code . Source (A ()))
390
+ pytest .raises (TypeError , lambda : Source (A ()))
390
391
391
392
class WithCall :
392
393
def __call__ (self ) -> None :
393
394
pass
394
395
395
- code = _pytest . _code . Code (WithCall ())
396
+ code = Code (WithCall ())
396
397
assert "pass" in str (code .source ())
397
398
398
399
class Hello :
399
400
def __call__ (self ) -> None :
400
401
pass
401
402
402
- pytest .raises (TypeError , lambda : _pytest . _code . Code (Hello ))
403
+ pytest .raises (TypeError , lambda : Code (Hello ))
403
404
404
405
405
406
def getstatement (lineno : int , source ) -> Source :
406
407
from _pytest ._code .source import getstatementrange_ast
407
408
408
- src = _pytest . _code . Source (source )
409
+ src = Source (source )
409
410
ast , start , end = getstatementrange_ast (lineno , src )
410
411
return src [start :end ]
411
412
@@ -637,7 +638,7 @@ def test_getstartingblock_multiline() -> None:
637
638
class A :
638
639
def __init__ (self , * args ):
639
640
frame = sys ._getframe (1 )
640
- self .source = _pytest . _code . Frame (frame ).statement
641
+ self .source = Frame (frame ).statement
641
642
642
643
# fmt: off
643
644
x = A ('x' ,
0 commit comments