Skip to content

Commit

Permalink
test: add basic tests for tools.calculation module
Browse files Browse the repository at this point in the history
Co-authored-by: dgw <[email protected]>
  • Loading branch information
SnoopJ and dgw committed Nov 3, 2023
1 parent 2c9fa85 commit 1bb5d45
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions test/tools/test_tools_calculation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"""Tests Sopel's calculation tools"""
from __future__ import annotations

import ast
import operator

import pytest

from sopel.tools.calculation import EquationEvaluator, ExpressionEvaluator


def test_expression_eval():
"""Ensure ExpressionEvaluator respects limited operator set."""
OPS = {
ast.Add: operator.add,
ast.Sub: operator.sub,
}
evaluator = ExpressionEvaluator(bin_ops=OPS)

assert evaluator("1 + 1") == 2
assert evaluator("43 - 1") == 42
assert evaluator("1 + 1 - 2") == 0

with pytest.raises(ExpressionEvaluator.Error):
evaluator("2 * 2")


def test_equation_eval():
"""Test that EquationEvaluator correctly parses input and calculates results."""
evaluator = EquationEvaluator()

assert evaluator("1 + 1") == 2
assert evaluator("43 - 1") == 42
assert evaluator("(((1 + 1 + 2) * 3 / 5) ** 8 - 13) // 21 % 35") == 16.0
assert evaluator("-42") == -42
assert evaluator("-(-42)") == 42
assert evaluator("+42") == 42
assert evaluator("3 ^ 2") == 9

0 comments on commit 1bb5d45

Please sign in to comment.