Skip to content

Commit

Permalink
Test README codeblocks
Browse files Browse the repository at this point in the history
  • Loading branch information
Kuba314 committed Dec 11, 2023
1 parent 3446451 commit 276d522
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 6 deletions.
11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ Disclaimer: This library is young and probably highly unstable. Use at your own
```py
from arcparse import arcparser, positional


@arcparser
class Args:
name: str = positional()
Expand All @@ -18,7 +17,7 @@ class Args:
happy: bool


args = Args.parse()
args = Args.parse("Thomas 25 news coffee running --happy".split())
print(f"Hi, my name is {args.name}!")
```

Expand Down Expand Up @@ -86,7 +85,8 @@ Passing `name_override=...` will cause the provided string to be used instead of

This is useful in combination with accepting multiple values with `append=True`, because the user will use `--value foo --value bar`, while the code will use `args.values`.
```py
class Args(ArcParser):
@arcparser
class Args:
values: list[str] = option(name_override="value", append=True)
```

Expand All @@ -96,6 +96,7 @@ Automatic type conversions are supported. The type-hint is used in `type=...` in
Custom converters may be used in combination with multiple values per argument. These converters are called `itemwise` and need to be wrapped in `itemwise()`. This wrapper is used automatically if an argument is typed as `list[...]` and no converter is set.
```py
from arcparse.converters import sv, csv, sv_dict, itemwise
from enum import StrEnum
from re import Pattern

@arcparser
Expand Down Expand Up @@ -132,11 +133,9 @@ class Args:
### Subparsers
Type-hinting an argument as a union of ArcParser subclasses creates subparsers in the background. Assigning from `subparsers()` gives them names as they will be entered from the command-line. Subparsers are required by default. Adding `None` to the union makes the subparsers optional.
```py
@arcparser
class FooArgs:
arg1: str

@arcparser
class BarArgs:
arg2: int = positional()

Expand All @@ -155,7 +154,7 @@ python3 script.py foo --arg1 baz
python3 script.py bar --arg2 123
```
```py
args = Args.parse()
args = Args.parse("foo --arg1 baz".split())
if isinstance(foo := args.action, FooArgs):
print(f"foo {foo.arg1}")
elif isinstance(bar := args.action, BarArgs):
Expand Down
38 changes: 38 additions & 0 deletions tests/test_readme.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from pathlib import Path
from collections.abc import Iterable
import re
import subprocess

import pytest


def python_codeblocks(text: str) -> list[str]:
return re.findall(r"```py\s*(.*?)\s*```", text, flags=re.MULTILINE | re.DOTALL)


def cumul(texts: Iterable[str]) -> list[str]:
cum = ""
out = []
for text in texts:
cum += text + "\n"
out.append(cum)
return out


readme_python_codeblocks = cumul(
python_codeblocks(Path(__file__, "..", "..", "README.md").resolve().read_text())
)
readme_python_codeblocks_ids = range(1, len(readme_python_codeblocks) + 1)


@pytest.mark.parametrize("code", readme_python_codeblocks, ids=readme_python_codeblocks_ids)
def test_python_codeblock_functional(code: str, tmp_path: Path) -> None:
code = "from arcparse import *\n" + code
path = tmp_path / "code.py"
path.write_text(code)

# try:
subprocess.check_call(["python3", str(path)])
# runpy.run_path(str(tmp_path))
# except BaseException as e:
# raise Exception(code) from e

0 comments on commit 276d522

Please sign in to comment.