test_evaluate.py 1.0 KB

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