Skip to content

Commit 448088e

Browse files
committed
tests for EvalHelperFunc
1 parent d7ec33d commit 448088e

2 files changed

Lines changed: 48 additions & 12 deletions

File tree

‎paths_cli/tests/wizard/test_helper.py‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import pytest
22

33
from paths_cli.wizard.helper import *
4+
from paths_cli.wizard.helper import _LONG_EVAL_HELP
45
from paths_cli.tests.wizard.mock_wizard import mock_wizard
56

67
def test_raise_quit():
@@ -15,6 +16,29 @@ def test_force_exit():
1516
with pytest.raises(SystemExit):
1617
force_exit("foo", None)
1718

19+
class TestEvalHelperFunc:
20+
def setup(self):
21+
self.param_helper = {
22+
'str': "help_string",
23+
'method': lambda help_args, context: f"help_{help_args}"
24+
}
25+
self.expected = {
26+
'str': "help_string",
27+
'method': "help_foo"
28+
}
29+
30+
@pytest.mark.parametrize('helper_type', ['str', 'method'])
31+
def test_call(self, helper_type):
32+
help_func = EvalHelperFunc(self.param_helper[helper_type])
33+
help_str = help_func("foo")
34+
assert self.expected[helper_type] in help_str
35+
assert "ask for help with '?eval'" in help_str
36+
37+
@pytest.mark.parametrize('helper_type', ['str', 'method'])
38+
def test_call_eval(self, helper_type):
39+
help_func = EvalHelperFunc(self.param_helper[helper_type])
40+
assert help_func("eval") == _LONG_EVAL_HELP
41+
1842
class TestHelper:
1943
def setup(self):
2044
self.helper = Helper(help_func=lambda s, ctx: s)

‎paths_cli/wizard/helper.py‎

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from .errors import RestartObjectException
22

3+
34
class QuitWizard(BaseException):
45
pass
56

@@ -10,13 +11,16 @@ class QuitWizard(BaseException):
1011
def raise_quit(cmd, ctx):
1112
raise QuitWizard()
1213

14+
1315
def raise_restart(cmd, ctx):
1416
raise RestartObjectException()
1517

18+
1619
def force_exit(cmd, ctx):
1720
print("Exiting...")
1821
exit()
1922

23+
2024
HELPER_COMMANDS = {
2125
'q': raise_quit,
2226
'quit': raise_quit,
@@ -46,15 +50,23 @@ def force_exit(cmd, ctx):
4650
}
4751

4852

49-
_SHORT_EVAL_HELP = ("This parameter can be generated using our custom "
50-
"evaluation method. For details, ask for help with "
51-
"'?eval'.")
52-
_LONG_EVAL_HELP = ("The value for this parameter can be generated using a "
53-
"Python-like syntax. You're limited to a single "
54-
"expression (generally, a single line of Python) and "
55-
"the imports math and numpy (as np). However, this "
56-
"allows you to do simple calculations such as:\n"
57-
" 100 * np.pi / 180 # 100 degrees in radians")
53+
_SHORT_EVAL_HELP = ("You can use expression evaluation with this "
54+
"parameter! For details, ask for help with '?eval'.")
55+
_LONG_EVAL_HELP = (
56+
"You can use a Python expression to create the value for this "
57+
"parameter. However, there are a few limitations:\n\n",
58+
" * You're limited to a single expression. That basically means a "
59+
"single line of Python, and no control structures like for loops.\n\n"
60+
" * You can't import any libraries. However, math and numpy are "
61+
"included (use numpy as 'np').\n\n"
62+
"The expression evaluator means that you can use simple expressions "
63+
"as input. For example, say you wanted 100 degrees in radians. You "
64+
"could just type\n\n"
65+
" 100 * np.pi / 180\n\n"
66+
"as your input for the parameter."
67+
)
68+
69+
5870
class EvalHelperFunc:
5971
"""Helper function (input to :class:`.Helper`) for evaluated parameters.
6072
@@ -64,18 +76,19 @@ class EvalHelperFunc:
6476
help string or method that takes arguments and context dict and
6577
results the help string
6678
"""
67-
def __init__(self, param_helper, context=None):
79+
def __init__(self, param_helper):
6880
if isinstance(param_helper, str):
6981
helper = lambda wizard, context: param_helper
7082
else:
7183
helper = param_helper
7284
self.helper = helper
7385

74-
def __call__(self, helpargs, context):
86+
def __call__(self, helpargs, context=None):
7587
if helpargs == "eval":
7688
return _LONG_EVAL_HELP
7789
return self.helper(helpargs, context) + "\n\n" + _SHORT_EVAL_HELP
7890

91+
7992
class Helper:
8093
"""Manage help and command passing on command line.
8194
@@ -121,7 +134,6 @@ def _command_help(self, help_args, context):
121134

122135
return result
123136

124-
125137
def _run_command(self, command, context):
126138
"""Runs a the given command.
127139

0 commit comments

Comments
 (0)