Skip to content

Commit 7f981c0

Browse files
committed
more tests, fix print twice
1 parent 8e166a2 commit 7f981c0

File tree

5 files changed

+104
-4
lines changed

5 files changed

+104
-4
lines changed

pytest_examples/eval_example.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ def run_print_check(
8484
Run the example and check print statements.
8585
8686
:param example: The example to run.
87-
:param line_length: The line length to use when wrapping print statements.
8887
:param rewrite_assertions: If True, rewrite assertions in the example using pytest's assertion rewriting.
8988
"""
9089
__tracebackhide__ = True
@@ -101,7 +100,6 @@ def run_print_update(
101100
Run the example and update print statements, requires `--update-examples`.
102101
103102
:param example: The example to run.
104-
:param line_length: The line length to use when wrapping print statements.
105103
:param rewrite_assertions: If True, rewrite assertions in the example using pytest's assertion rewriting.
106104
"""
107105
__tracebackhide__ = True

pytest_examples/insert_print.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,14 +111,18 @@ def _insert_print_statements(self, example: CodeExample) -> str:
111111

112112
lines = example.source.splitlines()
113113

114+
old_line_no = -1
115+
114116
for s in reversed(self.print_func.statements):
115117
line_no, col = find_print_location(example, s.line_no)
116118

117119
# switch from 1-indexed line number to 0-indexed indexes into lines
118120
line_index = line_no - 1
119121

120-
remove_old_print(lines, line_index)
122+
if s.line_no != old_line_no:
123+
remove_old_print(lines, line_index)
121124
self._insert_print_args(lines, s, example.in_py_file(), line_index, col)
125+
old_line_no = s.line_no
122126

123127
return '\n'.join(lines) + '\n'
124128

@@ -211,7 +215,18 @@ def find_print_location(example: CodeExample, line_no: int) -> tuple[int, int]:
211215

212216

213217
# ast nodes that have a body
214-
with_body = ast.Module, ast.FunctionDef, ast.If, ast.Try, ast.ExceptHandler, ast.With, ast.For, ast.AsyncFor, ast.While
218+
with_body = (
219+
ast.Module,
220+
ast.FunctionDef,
221+
ast.If,
222+
ast.Try,
223+
ast.ExceptHandler,
224+
ast.With,
225+
ast.For,
226+
ast.AsyncFor,
227+
ast.While,
228+
ast.ClassDef,
229+
)
215230

216231

217232
def find_print(node: Any, line: int) -> tuple[int, int] | None:

tests/cases_update/call_twice.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
```py
2+
def foobar(x):
3+
print(f'x={x}')
4+
5+
foobar(1)
6+
foobar(2)
7+
```
8+
################### OUTPUT ###################
9+
```py
10+
def foobar(x):
11+
print(f'x={x}')
12+
#> x=1
13+
#> x=2
14+
15+
16+
foobar(1)
17+
foobar(2)
18+
```
19+
################### TEST #####################
20+
```py
21+
from pytest_examples import find_examples, CodeExample, EvalExample
22+
import pytest
23+
24+
25+
@pytest.mark.parametrize('example', find_examples('.'), ids=str)
26+
def test_find_examples(example: CodeExample, eval_example: EvalExample):
27+
if eval_example.update_examples:
28+
eval_example.format(example)
29+
eval_example.run_print_update(example)
30+
```
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
```py
2+
from dataclasses import dataclass
3+
4+
5+
@dataclass
6+
class User:
7+
foobar: int
8+
9+
def __post_init__(self):
10+
print(self.foobar)
11+
12+
13+
User(foobar=123)
14+
```
15+
################### OUTPUT ###################
16+
```py
17+
from dataclasses import dataclass
18+
19+
20+
@dataclass
21+
class User:
22+
foobar: int
23+
24+
def __post_init__(self):
25+
print(self.foobar)
26+
#> 123
27+
28+
29+
User(foobar=123)
30+
```
31+
################### TEST #####################
32+
```py
33+
from pytest_examples import find_examples, CodeExample, EvalExample
34+
import pytest
35+
36+
37+
@pytest.mark.parametrize('example', find_examples('.'), ids=str)
38+
def test_find_examples(example: CodeExample, eval_example: EvalExample):
39+
eval_example.set_config(quotes='double')
40+
if eval_example.update_examples:
41+
eval_example.format(example)
42+
eval_example.run_print_update(example)
43+
```

tests/test_insert_print.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,20 @@ def foobar():
106106
(2, 0),
107107
id='call-attribute',
108108
),
109+
pytest.param(
110+
"""\
111+
@dataclass
112+
class User:
113+
foobar: int
114+
115+
def __post_init__(self):
116+
print(self.foobar)
117+
#> 123
118+
""",
119+
6,
120+
(6, 8),
121+
id='class-method',
122+
),
109123
]
110124

111125

0 commit comments

Comments
 (0)