test_evaluate.py 1023 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import pytest
  2. from acpi_backlight._evaluate import evaluate_expression
  3. @pytest.mark.parametrize(
  4. ("expr_str", "names", "expected"),
  5. [
  6. ("0", {}, 0),
  7. ("0.0", {}, 0),
  8. ("1.0", {}, 1),
  9. ("-1", {}, -1),
  10. ("0.1 + 0.2", {}, 0.3),
  11. ("0.3 - 0.2", {}, 0.1),
  12. ("0.2 * 3", {}, 0.6),
  13. ("0.6 / 3", {}, 0.2),
  14. ("0.6 / 3 + 0.1", {}, 0.3),
  15. ("(0.6 - 0.2) / 2", {}, 0.2),
  16. ("b", {"b": 0.4}, 0.4),
  17. ("-b", {"b": 0.3}, -0.3),
  18. ("0.1 + b", {"b": 0.2}, 0.3),
  19. ],
  20. )
  21. def test_evaluate_expression(expr_str, names, expected):
  22. assert expected == pytest.approx(evaluate_expression(expr_str, names))
  23. @pytest.mark.parametrize(
  24. "expr_str",
  25. [
  26. 'read("/proc/cpuinfo")',
  27. "os.exit(42)",
  28. 'os.system("echo evil")',
  29. "0.__class__",
  30. "None.__class__",
  31. 'eval("1")',
  32. ],
  33. )
  34. def test_evaluate_expression_fail(expr_str):
  35. with pytest.raises(Exception):
  36. evaluate_expression(expr_str, {})