Skip to content

Commit

Permalink
test: cover remainder of tools.calculation module
Browse files Browse the repository at this point in the history
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
1 parent 1bb5d45 commit bbaafc6
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion test/tools/test_tools_calculation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down

0 comments on commit bbaafc6

Please sign in to comment.