-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Various improvements: dynamic dataclasses, helptext, tests
- Loading branch information
Showing
6 changed files
with
95 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import contextlib | ||
import dataclasses | ||
import io | ||
|
||
import pytest | ||
|
||
import dcargs | ||
|
||
|
||
def test_helptext(): | ||
@dataclasses.dataclass | ||
class Helptext: | ||
x: int # Documentation 1 | ||
|
||
# Documentation 2 | ||
y: int | ||
|
||
z: int = 3 | ||
"""Documentation 3""" | ||
|
||
f = io.StringIO() | ||
with pytest.raises(SystemExit): | ||
with contextlib.redirect_stdout(f): | ||
dcargs.parse(Helptext, args=["--help"]) | ||
helptext = f.getvalue() | ||
assert "--x INT Documentation 1\n" in helptext | ||
assert "--y INT Documentation 2\n" in helptext | ||
assert "--z INT Documentation 3 (default: 3)\n" in helptext | ||
|
||
|
||
def test_multiline_helptext(): | ||
@dataclasses.dataclass | ||
class HelptextMultiline: | ||
x: int # Documentation 1 | ||
|
||
# Documentation 2 | ||
# Next line of documentation 2 | ||
y: int | ||
|
||
z: int = 3 | ||
"""Documentation 3 | ||
Next line of documentation 3""" | ||
|
||
f = io.StringIO() | ||
with pytest.raises(SystemExit): | ||
with contextlib.redirect_stdout(f): | ||
dcargs.parse(HelptextMultiline, args=["--help"]) | ||
helptext = f.getvalue() | ||
assert " --x INT Documentation 1\n" in helptext | ||
assert ( | ||
" --y INT Documentation 2\n Next line of documentation 2\n" | ||
in helptext | ||
) | ||
assert ( | ||
" --z INT Documentation 3\n Next line of documentation 3 (default: 3)\n" | ||
in helptext | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from dataclasses import field, make_dataclass | ||
|
||
import pytest | ||
|
||
import dcargs | ||
|
||
|
||
def test_dynamic(): | ||
B = make_dataclass("B", [("c", int, field())]) | ||
A = make_dataclass("A", [("b", B, field())]) | ||
|
||
with pytest.raises(SystemExit): | ||
dcargs.parse(A, args=[]) | ||
assert dcargs.parse(A, args=["--b.c", "5"]) == A(b=B(c=5)) |