Skip to content

Commit

Permalink
Test the Python command-line interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Kodiologist committed Nov 17, 2023
1 parent 4ed9f6b commit 554abba
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/python/code/gnoll/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,16 @@ def main(EXPR, times, no_builtins, **kwargs):
EXPR,
builtins=not no_builtins,
**kwargs)
if breakdown:
print(breakdown[0], '-->', result)
else:
print(result)
yield (breakdown[0], '-->', result) if breakdown else (result,)


def main_with_args(args):
"""Parse the commandline args and then run `main`
@param args - the arguments from the commandline (excluding the python3 call)
"""
yield from main(**vars(parse_cmdline_args(args)))


if __name__ == '__main__':
main(**vars(parse_cmdline_args(sys.argv[1:])))
for line in main_with_args(sys.argv[1:]):
print(*line)
20 changes: 20 additions & 0 deletions tests/python/test_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import gnoll.__main__


m = lambda *x: list(gnoll.__main__.main_with_args(x))


def test_cli():

[[r]] = m('1d4')
assert isinstance(r, int)

[[(die1, die2), a, r]] = m('2d4', '--breakdown')
assert all((isinstance(x, int) for x in [die1, die2, r]))
assert a == '-->'

executions = m('4d6kh3', '+', '1', '--breakdown', '--times', '6', '--no-builtins')
assert len(executions) == 6
for (die1, die2, die3, die4), a, r in executions:
assert all((isinstance(x, int) for x in [die1, die2, die3, die4, r]))
assert a == '-->'

0 comments on commit 554abba

Please sign in to comment.