Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates find_examples to support alternate docstring style. #7

Merged
merged 2 commits into from
Apr 5, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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?"""[^\r\n]*\n)(.+?)\1"""', code, flags=re.M | re.S):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for m_docstring in re.finditer(r'(^ *)(r?"""[^\r\n]*\n)(.+?)\1"""', code, flags=re.M | re.S):
for m_docstring in re.finditer(r'(^ *)(r?""".*?\n)(.+?)\1"""', code, flags=re.M | re.S):

Should work the same.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Updated in 85681d7

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