Skip to content

Commit a3f482c

Browse files
committed
tests: move test_getfslineno
It should be in `test_code` when testing `_pytest._code.getfslineno`, not to be confused with `_pytest._code.source.getfslineno`. Adds an extra assert (via pytest-dev#6590).
1 parent 8e1d59a commit a3f482c

File tree

2 files changed

+33
-30
lines changed

2 files changed

+33
-30
lines changed

testing/code/test_code.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1+
import inspect
12
import sys
23
from types import FrameType
34
from unittest import mock
45

6+
import py.path
7+
58
import _pytest._code
69
import pytest
10+
from _pytest._code import getfslineno
711

812

913
def test_ne() -> None:
@@ -179,3 +183,32 @@ def test_not_raise_exception_with_mixed_encoding(self, tw_mock) -> None:
179183
tw_mock.lines[0]
180184
== r"unicode_string = São Paulo, utf8_string = b'S\xc3\xa3o Paulo'"
181185
)
186+
187+
188+
def test_getfslineno() -> None:
189+
def f(x) -> None:
190+
pass
191+
192+
fspath, lineno = getfslineno(f)
193+
194+
assert isinstance(fspath, py.path.local)
195+
assert fspath.basename == "test_code.py"
196+
assert lineno == f.__code__.co_firstlineno - 1 # see findsource
197+
198+
class A:
199+
pass
200+
201+
fspath, lineno = getfslineno(A)
202+
203+
_, A_lineno = inspect.findsource(A)
204+
assert isinstance(fspath, py.path.local)
205+
assert fspath.basename == "test_code.py"
206+
assert lineno == A_lineno
207+
208+
assert getfslineno(3) == ("", -1)
209+
210+
class B:
211+
pass
212+
213+
B.__name__ = "B2"
214+
assert getfslineno(B)[1] == -1

testing/code/test_source.py

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -495,36 +495,6 @@ def x():
495495
assert src[lineno] == " def x():"
496496

497497

498-
def test_getfslineno() -> None:
499-
from _pytest._code import getfslineno
500-
501-
def f(x) -> None:
502-
pass
503-
504-
fspath, lineno = getfslineno(f)
505-
506-
assert isinstance(fspath, py.path.local)
507-
assert fspath.basename == "test_source.py"
508-
assert lineno == f.__code__.co_firstlineno - 1 # see findsource
509-
510-
class A:
511-
pass
512-
513-
fspath, lineno = getfslineno(A)
514-
515-
_, A_lineno = inspect.findsource(A)
516-
assert fspath.basename == "test_source.py"
517-
assert lineno == A_lineno
518-
519-
assert getfslineno(3) == ("", -1)
520-
521-
class B:
522-
pass
523-
524-
B.__name__ = "B2"
525-
assert getfslineno(B)[1] == -1
526-
527-
528498
def test_code_of_object_instance_with_call() -> None:
529499
class A:
530500
pass

0 commit comments

Comments
 (0)