-
-
Notifications
You must be signed in to change notification settings - Fork 402
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add basic tests for
tools.calculation
module
Co-authored-by: dgw <[email protected]>
- Loading branch information
Showing
1 changed file
with
38 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |