Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /als-apg_osprey /tests /simulation /test_expressions.py
| """Tests for the safe AST expression evaluator.""" | |
| import math | |
| import pytest | |
| from osprey.simulation.expressions import ( | |
| ExpressionError, | |
| compile_expression, | |
| evaluate, | |
| evaluate_channel, | |
| extract_channel_refs, | |
| ) | |
| def _eval(source, channels=None): | |
| channels = channels or {} | |
| return evaluate(compile_expression(source), lambda name: channels[name]) | |
| class TestAllowedExpressions: | |
| """Allowed grammar evaluates correctly.""" | |
| def test_arithmetic_operators(self): | |
| assert _eval("1 + 2 * 3") == 7.0 | |
| assert _eval("10 / 4") == 2.5 | |
| assert _eval("2 ** 3 - 1") == 7.0 | |
| assert _eval("(1 + 2) * 3") == 9.0 | |
| def test_unary_minus(self): | |
| assert _eval("-5 + 2") == -3.0 | |
| assert _eval("-(2 + 3)") == -5.0 | |
| def test_float_and_int_literals(self): | |
| assert _eval("3.5e-7") == pytest.approx(3.5e-7) | |
| assert _eval("42") == 42.0 | |
| def test_functions(self): | |
| assert _eval("abs(-4.2)") == pytest.approx(4.2) | |
| assert _eval("min(3, 1, 2)") == 1.0 | |
| assert _eval("max(0.0, -5.0)") == 0.0 | |
| assert _eval("sqrt(16)") == 4.0 | |
| assert _eval("exp(0)") == 1.0 | |
| assert _eval("exp(1)") == pytest.approx(math.e) | |
| def test_channel_reference(self): | |
| assert _eval("ch('PV:A') * 2", {"PV:A": 21.0}) == 42.0 | |
| def test_nested(self): | |
| result = _eval( | |
| "max(0.0, 98.5 - 0.85 * abs(ch('PV:SP') - 42.0)) * ch('PV:STATUS')", | |
| {"PV:SP": 28.4, "PV:STATUS": 1.0}, | |
| ) | |
| assert result == pytest.approx(98.5 - 0.85 * 13.6) | |
| class TestRejectedExpressions: | |
| """Everything outside the tiny grammar is rejected at compile time.""" | |
| def test_rejected(self, source): | |
| with pytest.raises(ExpressionError): | |
| compile_expression(source) | |
| def test_syntax_error(self): | |
| with pytest.raises(ExpressionError, match="syntax"): | |
| compile_expression("1 +") | |
| class TestChannelRefs: | |
| """Channel reference extraction.""" | |
| def test_extract_refs(self): | |
| node = compile_expression("ch('PV:A') + ch('PV:B') * abs(ch('PV:A'))") | |
| assert extract_channel_refs(node) == {"PV:A", "PV:B"} | |
| def test_no_refs(self): | |
| node = compile_expression("1 + 2") | |
| assert extract_channel_refs(node) == set() | |
| class TestEvaluateChannel: | |
| """The channel-context error-wrapping helper shared by both eval paths.""" | |
| def test_success_passes_through(self): | |
| node = compile_expression("ch('PV:A') * 2") | |
| assert evaluate_channel(node, "ch('PV:A') * 2", "PV:OUT", lambda _: 3.0) == 6.0 | |
| def test_expression_error_is_prefixed_with_channel(self): | |
| # A resolver that itself raises ExpressionError (e.g. string channel) | |
| # should be re-raised with the owning channel name attached. | |
| node = compile_expression("ch('PV:STR')") | |
| def resolver(_name): | |
| raise ExpressionError("holds a string value") | |
| with pytest.raises(ExpressionError, match=r"Channel 'PV:OUT': holds a string value"): | |
| evaluate_channel(node, "ch('PV:STR')", "PV:OUT", resolver) | |
| def test_arithmetic_error_names_channel_and_source(self): | |
| node = compile_expression("1 / ch('PV:A')") | |
| with pytest.raises(ExpressionError) as exc: | |
| evaluate_channel(node, "1 / ch('PV:A')", "PV:OUT", lambda _: 0.0) | |
| message = str(exc.value) | |
| assert "PV:OUT" in message | |
| assert "1 / ch('PV:A')" in message | |
Xet Storage Details
- Size:
- 4.38 kB
- Xet hash:
- 5a1b50748759dec1a76b236242a044cecf907919ec42129ee65da90fc357fb36
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.