From 0238bc6e2951281197b2fe0eb12b727a0a333cb4 Mon Sep 17 00:00:00 2001 From: CodingBunny06 <210141639+CodingBunny06@users.noreply.github.com> Date: Sun, 8 Nov 2026 13:48:44 +0530 Subject: [PATCH] Improve and organise equation parser tests --- test_equation_parser.py | 42 ------------------ tests/test_equation_parser.py | 81 +++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 42 deletions(-) delete mode 100644 test_equation_parser.py create mode 100644 tests/test_equation_parser.py diff --git a/test_equation_parser.py b/test_equation_parser.py deleted file mode 100644 index 3095bfd..0000000 --- a/test_equation_parser.py +++ /dev/null @@ -1,42 +0,0 @@ - -from equation_parser import parse_equation, EquationValidationError -import sympy as sp - - -def test_basic_equation(): - expression = parse_equation("x**2 + y**2") - assert expression == sp.Symbol("x")**2 + sp.Symbol("y")**2 - - -def test_caret_notation(): - expression = parse_equation("x^2 + y^2") - assert expression == sp.Symbol("x")**2 + sp.Symbol("y")**2 - - -def test_math_function(): - expression = parse_equation("sin(x) + cos(y)") - assert expression == sp.sin(sp.Symbol("x")) + sp.cos(sp.Symbol("y")) - - -def test_invalid_variable(): - try: - parse_equation("x**2 + z**2") - assert False, "Expected validation error" - except EquationValidationError: - pass - - -def test_unsafe_input(): - try: - parse_equation("print(x)") - assert False, "Expected validation error" - except EquationValidationError: - pass - - -def test_empty_input(): - try: - parse_equation("") - assert False, "Expected validation error" - except EquationValidationError: - pass diff --git a/tests/test_equation_parser.py b/tests/test_equation_parser.py new file mode 100644 index 0000000..423d465 --- /dev/null +++ b/tests/test_equation_parser.py @@ -0,0 +1,81 @@ +from equation_parser import x, y +import pytest +import sympy as sp + +from equation_parser import parse_equation, EquationValidationError + + +def test_basic_equation(): + expression = parse_equation("x**2 + y**2") + assert expression == sp.Symbol("x")**2 + sp.Symbol("y")**2 + + +def test_caret_notation(): + expression = parse_equation("x^2 + y^2") + assert expression == sp.Symbol("x")**2 + sp.Symbol("y")**2 + + +def test_math_function(): + expression = parse_equation("sin(x) + cos(y)") + assert expression == sp.sin(sp.Symbol("x")) + sp.cos(sp.Symbol("y")) + + +def test_invalid_variable(): + with pytest.raises(EquationValidationError): + parse_equation("x**2 + z**2") + + +def test_unsafe_input(): + with pytest.raises(EquationValidationError): + parse_equation("print(x)") + + +def test_empty_input(): + with pytest.raises(EquationValidationError): + parse_equation("") + + +def test_allowed_constants(): + expression = parse_equation("x + pi + E") + assert expression == x + sp.pi + sp.E + + +def test_all_supported_functions(): + expression = parse_equation( + "tan(x) + exp(y) + log(x) + sqrt(y) + abs(x)" + ) + + assert expression == ( + sp.tan(x) + sp.exp(y) + sp.log(x) + + sp.sqrt(y) + sp.Abs(x) + ) + + +def test_unsupported_function(): + with pytest.raises(EquationValidationError): + parse_equation("factorial(x)") + + +def test_unsupported_operator(): + with pytest.raises(EquationValidationError): + parse_equation("x // y") + + +def test_multiple_function_arguments(): + with pytest.raises(EquationValidationError): + parse_equation("sin(x, y)") + + +def test_exponent_limit(): + with pytest.raises(EquationValidationError): + parse_equation("x**101") + + +def test_boolean_input(): + with pytest.raises(EquationValidationError): + parse_equation("True + x") + + +def test_invalid_syntax(): + with pytest.raises(EquationValidationError): + parse_equation("x + * y")