Skip to content

Commit

Permalink
Updates find_examples to support alternate docstring style. (#7)
Browse files Browse the repository at this point in the history
* Fixes find examples to support alternate docstring style.

Now supports docstrings without a line break directly after the triple
quotes.

* Update regex

Change [\r\n]* to .*?
  • Loading branch information
donovandicks authored Apr 5, 2023
1 parent 03f7fad commit a2c6dd1
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
12 changes: 12 additions & 0 deletions example/test_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,15 @@ def test_python_self(example: CodeExample, eval_example: EvalExample):
"""
eval_example.lint(example)
eval_example.run_print_check(example)


@pytest.mark.parametrize('example', find_examples('example/test_example.py'), ids=str)
def test_python_self_change_docstyle(example: CodeExample, eval_example: EvalExample):
"""Test this code (no line break at beginning of docstring).
```py
print('this is introspection!')
#> this is introspection!
```
"""
eval_example.lint(example)
eval_example.run_print_check(example)
2 changes: 1 addition & 1 deletion pytest_examples/find_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def find_examples(*paths: str | Path, skip: bool = False) -> Iterable[CodeExampl
group = uuid4()
if path.suffix == '.py':
code = path.read_text('utf-8')
for m_docstring in re.finditer(r'(^ *)(r?"""\n)(.+?)\1"""', code, flags=re.M | re.S):
for m_docstring in re.finditer(r'(^ *)(r?""".*?\n)(.+?)\1"""', code, flags=re.M | re.S):
start_line = code[: m_docstring.start()].count('\n') + 1
docstring = m_docstring.group(3)
index_offset = m_docstring.start() + len(m_docstring.group(1)) + len(m_docstring.group(2))
Expand Down
12 changes: 11 additions & 1 deletion tests/test_find_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,15 @@ def func_b():
```
"""
pass
def func_c():
"""Does cool things.
```py
g = 7
h = 8
assert g + h == 15
```
"""
''',
)
pytester.makepyfile(
Expand All @@ -107,12 +116,13 @@ def test_find_examples(example):
)

result = pytester.runpytest('-p', 'no:pretty', '-vs')
result.assert_outcomes(passed=3)
result.assert_outcomes(passed=4)

output = '\n'.join(result.outlines)
assert 'test_find_examples[my_file.py:3-7] PASSED' in output
assert 'test_find_examples[my_file.py:14-18] PASSED' in output
assert 'test_find_examples[my_file.py:20-24] PASSED' in output
assert 'test_find_examples[my_file.py:30-34] PASSED' in output


def test_find_file_example(pytester: pytest.Pytester):
Expand Down

0 comments on commit a2c6dd1

Please sign in to comment.