Skip to content

Commit

Permalink
Add pytest tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Kuba314 committed Nov 15, 2023
1 parent c8f9b86 commit 7a52d25
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 1 deletion.
48 changes: 47 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ python = "^3.12"
[tool.poetry.group.dev.dependencies]
black = "^23.11.0"
pyright = "^1.1.335"
pytest = "^7.4.3"

[build-system]
requires = ["poetry-core"]
Expand Down
101 changes: 101 additions & 0 deletions tests/test_arcparse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
from enum import StrEnum, auto
from typing import Optional
import pytest

from arcparse import ArcParser, positional, option, flag, no_flag, subparsers


class Pos(ArcParser):
foo: str = positional()
bar: str | None = positional()
baz: str = positional(default="123")


class Opt(ArcParser):
foo: str
bar: None | str
baz: str = option("-b", short_only=True, default="123")
boo: Optional[str]


class Flag(ArcParser):
foo: bool
bar: bool = no_flag()
baz: bool = flag(short="-z")
boo: bool = flag(short="-o", short_only=True)


class Conv(ArcParser):
class Result(StrEnum):
PASS = auto()
FAIL = auto()

num: int | None
res: Result | None


class Args(ArcParser):
sub: Pos | Opt | Flag | Conv = subparsers("pos", "opt", "flag", "conv")


@pytest.mark.parametrize(
"string,should_throw",
[
("", True),
("pos 1", False),
("pos 1 2", False),
("pos 1 2 3", False),
("pos", True),
("pos 1 2 3 4", True),
("opt --foo foo --bar bar -b baz", False),
("opt --foo foo --bar bar --baz baz", True),
("opt --foo foo --bar bar", False),
("opt --foo foo", False),
("opt --bar bar", True),
("flag --foo", False),
("flag --no-bar", False),
("flag --bar", True),
("flag --no-foo", True),
("flag --baz", False),
("flag -z", False),
("flag -o", False),
("flag --boo", True),
("conv --num 123", False),
("conv --num foo", True),
("conv --res pass", False),
("conv --res fail", False),
("conv --res bar", True),
]
)
def test_valid(string: str, should_throw: bool) -> None:
args = string.split()

if should_throw:
with pytest.raises(SystemExit):
Args.parse(args)
else:
Args.parse(args)


class Invalid1(ArcParser):
x: bool | None

class Invalid2(ArcParser):
x: bool = positional()

class Invalid3(ArcParser):
x: int | str

class Invalid4(ArcParser):
x: int | str | None

class Invalid5(ArcParser):
x = positional()

@pytest.mark.parametrize("parser", [Invalid1, Invalid2, Invalid3, Invalid4, Invalid5])
def test_invalid(parser: ArcParser) -> None:
with pytest.raises(Exception):
parser.parse([])

0 comments on commit 7a52d25

Please sign in to comment.