Skip to content

Commit c9414f5

Browse files
committed
Test example.py and example.pyi blocks in user guide
1 parent 1701b79 commit c9414f5

3 files changed

Lines changed: 59 additions & 11 deletions

File tree

doc/command_line_reference.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ docstub --help
66
```
77
will print
88

9-
<!--- Changes to the following block are overridden by the test suite --->
9+
<!--- The following block is checked by the test suite --->
1010
<!--- begin command-line-help --->
1111

1212
```plain

doc/user_guide.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,11 @@ If you want to pin to a certain commit you can append `@COMMIT_SHA` to the repo
2020

2121
Consider a simple example with the following documented function
2222

23+
<!--- The following block is checked by the test suite --->
24+
<!--- begin example.py --->
25+
2326
```python
24-
# src/example.py
27+
# example.py
2528

2629
def example_metric(image, *, mask=None, sigma=1.0, method='standard'):
2730
"""Pretend to calculate a local metric between two images.
@@ -45,6 +48,8 @@ def example_metric(image, *, mask=None, sigma=1.0, method='standard'):
4548
pass
4649
```
4750

51+
<!--- end example.py --->
52+
4853
Feeding this input to docstub with
4954

5055
```shell
@@ -53,6 +58,8 @@ docstub simple_script.py
5358

5459
will create `example.pyi` in the same directory
5560

61+
<!--- The following block is checked by the test suite --->
62+
<!--- begin example.pyi --->
5663
```python
5764
# File generated with docstub
5865

@@ -70,6 +77,7 @@ def example_metric(
7077
method: Literal["standard", "modified"] = ...
7178
) -> NDArray[float]: ...
7279
```
80+
<!--- end example.pyi --->
7381

7482
There are several interesting things to note here:
7583

tests/test_doc.py

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,67 @@
55

66
import click
77

8-
from docstub._cli import main
8+
from docstub._cli import main as docstub_main
99

1010
PROJECT_ROOT = Path(__file__).parent.parent
1111

1212

13+
def test_getting_started_example(tmp_path):
14+
# Load user guide
15+
md_file = PROJECT_ROOT / "doc/user_guide.md"
16+
with md_file.open("r") as io:
17+
md_content = io.read()
18+
19+
# Extract code block for example.py
20+
regex_py = (
21+
r"<!--- begin example.py --->"
22+
r"\n```python\n(.*)\n```\n"
23+
r"<!--- end example.py --->"
24+
)
25+
matches_py = re.findall(regex_py, md_content, flags=re.DOTALL)
26+
assert len(matches_py) == 1
27+
py_source = matches_py[0]
28+
29+
# Create example.py and run docstub on it
30+
py_file = tmp_path / "example.py"
31+
with py_file.open("x") as io:
32+
io.write(py_source)
33+
docstub_main([str(py_file)], standalone_mode=False)
34+
35+
# Load created PYI file, this is what we expect to find in the user guide's
36+
# code block for example.pyi
37+
pyi_file = py_file.with_suffix(".pyi")
38+
assert pyi_file.is_file()
39+
with pyi_file.open("r") as io:
40+
expected_pyi = io.read().strip()
41+
42+
# Extract code block for example.pyi from guide
43+
regex_pyi = (
44+
r"<!--- begin example.pyi --->"
45+
r"\n```python\n(.*)\n```\n"
46+
r"<!--- end example.pyi --->"
47+
)
48+
matches_pyi = re.findall(regex_pyi, md_content, flags=re.DOTALL)
49+
assert len(matches_pyi) == 1
50+
actual_pyi = matches_pyi[0].strip()
51+
52+
assert expected_pyi == actual_pyi
53+
54+
1355
def test_command_line_help():
14-
ctx = click.Context(main, info_name="docstub")
56+
ctx = click.Context(docstub_main, info_name="docstub")
1557
expected_help = f"""
16-
1758
```plain
18-
{main.get_help(ctx)}
59+
{docstub_main.get_help(ctx)}
1960
```
20-
21-
"""
61+
""".strip()
2262
md_file = PROJECT_ROOT / "doc/command_line_reference.md"
2363
with md_file.open("r") as io:
2464
md_content = io.read()
2565

2666
regex = r"<!--- begin command-line-help --->(.*)<!--- end command-line-help --->"
27-
match = re.findall(regex, md_content, flags=re.DOTALL)
28-
assert len(match) == 1
67+
matches = re.findall(regex, md_content, flags=re.DOTALL)
68+
assert len(matches) == 1
2969

30-
actual_help = match[0]
70+
actual_help = matches[0].strip()
3171
assert actual_help == expected_help

0 commit comments

Comments
 (0)