| import pytest |
| import asyncio |
| from domain.math_validator import MathPolygraph, _latex_to_sympy_str |
| import sympy |
|
|
| @pytest.mark.asyncio |
| async def test_blind_stripping_and_regex(): |
| |
| text = "ื ืฆืื $x=5$ ืื ืงืื $y=2$. ืืชืืฆืื ืืื $$z=7$$." |
| |
| ok, reason = await MathPolygraph._validate_single(text, 1) |
| assert ok, f"Failed mixed text: {reason}" |
|
|
| @pytest.mark.asyncio |
| async def test_multiline_display_math(): |
| |
| text = """ืื ื ืืฉืืืื: |
| $$ |
| x = 5 + 3 |
| y = 10 |
| $$ |
| ืกืืฃ.""" |
| ok, reason = await MathPolygraph._validate_single(text, 1) |
| assert ok, f"Failed multiline display math: {reason}" |
|
|
| @pytest.mark.asyncio |
| async def test_multi_equal_guard(): |
| |
| text = "ื ืชืื $x = y = 5$." |
| ok, reason = await MathPolygraph._validate_single(text, 1) |
| assert ok, f"Failed multi-equal check: {reason}" |
|
|
| @pytest.mark.asyncio |
| async def test_empty_string_guard(): |
| |
| text = "ืจืืง $$$$ ืืื $ $." |
| ok, reason = await MathPolygraph._validate_single(text, 1) |
| assert ok, f"Failed empty string guard: {reason}" |
|
|
| @pytest.mark.asyncio |
| async def test_arithmetic_exception_handling(): |
| |
| text = "ืืืืงื ืืืคืก $1/0$." |
| ok, reason = await MathPolygraph._validate_single(text, 1) |
| assert not ok |
| assert "SYMPY_PARSE_ERROR" in reason |
|
|
| @pytest.mark.asyncio |
| async def test_variable_trap_in_equivalence(): |
| |
| |
| |
| res = MathPolygraph.are_equivalent("x=5", "x=5") |
| assert res is True |
|
|
| @pytest.mark.asyncio |
| async def test_numerical_identity_check(): |
| |
| assert MathPolygraph.are_equivalent("2+3", "5") is True |
| |
| assert MathPolygraph.are_equivalent("2+3", "6") is False |
|
|
| @pytest.mark.asyncio |
| async def test_inequality_guard(): |
| |
| |
| assert MathPolygraph.are_equivalent("x > 0", "x > 0") is True |
|
|
| @pytest.mark.asyncio |
| async def test_rce_protection(): |
| |
| |
| |
| |
| |
| text = "__import__('os').system('echo hello')" |
| |
| ok, reason = await MathPolygraph._check_segment(text, 1) |
| assert not ok |
| assert "SYMPY_PARSE_ERROR" in reason |
|
|
| if __name__ == "__main__": |
| import pytest |
| pytest.main([__file__]) |
|
|