Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tools, tools.calculation: docs/type-hint improvements, API fixes, better test coverage #2543

Merged
merged 6 commits into from
Nov 3, 2023
Prev Previous commit
test: cover remainder of tools.calculation module
Nasty assertions on exception args... Really need to refactor that silly
ExpressionEvaluator.Error type into some sensible PUBLIC hierarchy.
  • Loading branch information
dgw committed Nov 3, 2023
commit 3c4e477530b89314fcef7128f4c93c3a365b3103
32 changes: 31 additions & 1 deletion test/tools/test_tools_calculation.py
Original file line number Diff line number Diff line change
@@ -21,8 +21,38 @@ def test_expression_eval():
assert evaluator("43 - 1") == 42
assert evaluator("1 + 1 - 2") == 0

with pytest.raises(ExpressionEvaluator.Error):
with pytest.raises(ExpressionEvaluator.Error) as exc:
evaluator("2 * 2")
assert "Unsupported binary operator" in exc.value.args[0]

with pytest.raises(ExpressionEvaluator.Error) as exc:
evaluator("~2")
assert "Unsupported unary operator" in exc.value.args[0]


def test_equation_eval_invalid_constant():
"""Ensure unsupported constants are rejected."""
evaluator = EquationEvaluator()

with pytest.raises(ExpressionEvaluator.Error) as exc:
evaluator("2 + 'string'")
assert "values are not supported" in exc.value.args[0]


def test_equation_eval_timeout():
"""Ensure EquationEvaluator times out as expected."""
# timeout is added to the current time;
# negative means the timeout is "reached" before even starting
timeout = -1.0
evaluator = EquationEvaluator()

with pytest.raises(ExpressionEvaluator.Error) as exc:
evaluator("1000000**100", timeout)
assert "Time for evaluating" in exc.value.args[0]

with pytest.raises(ExpressionEvaluator.Error) as exc:
evaluator("+42", timeout)
assert "Time for evaluating" in exc.value.args[0]


def test_equation_eval():