Skip to content
Snippets Groups Projects
Commit 3b27dd28 authored by Antoine Gaudron-Desjardins's avatar Antoine Gaudron-Desjardins
Browse files

fix: fix the calculator

parent 65ae3892
Branches
No related tags found
No related merge requests found
Pipeline #46283 failed
......@@ -54,7 +54,7 @@ class Calculator:
#####################################################################
### ATTENTION: Est-ce que c'est vraiment un < (non c'est un <=) ? ###
#####################################################################
if operator is None or token.precedence < operator.precedence:
if operator is None or token.precedence <= operator.precedence:
operator = token
operator_index = i
......@@ -66,5 +66,5 @@ class Calculator:
return OperatorExpression(operator, self.parse(left), self.parse(right))
def __call__(self, expression: str) -> Term:
return 5 # Au moins il y a pas de surprise 😐
# return self.parse(self.tokenize(expression))()
# return 5 # Au moins il y a pas de surprise 😐
return self.parse(self.tokenize(expression))()
......@@ -3,7 +3,7 @@ Test module for the calculator module.
"""
import pytest
from calculator.calculator import Calculator
from calculator.operators import STANDARD_OPERATORS, Operator
from calculator.operators import STANDARD_OPERATORS
@pytest.fixture(scope="module", name="setup")
......@@ -26,20 +26,29 @@ def test_tokenizer(setup):
plus, minus, times, divide, calc = setup
# À toi de tester la fonction tokenize de Calculator.
assert calc.tokenize("1 + 2") == [1, plus, 2]
assert calc.tokenize("3 / 4") == [3, divide, 4]
assert calc.tokenize("4 * 2") == [4, times, 2]
assert calc.tokenize("4 - 2") == [4, minus, 2]
def test_parser(setup):
"""
Test the parser.
"""
plus, minus, times, divide, calc = setup
plus, minus, _, divide, calc = setup
# À toi de tester la fonction parse de Calculator.
assert repr([1, plus, 2]) == '(1 + 2)'
assert repr(calc.parse([1, plus, 2])) == '(1 + 2)'
assert repr(calc.parse([4, minus, 2])) == '(4 - 2)'
# assert repr(calc.parse([4, times, 2])) == '(4 * 2)'
assert repr(calc.parse([4, minus, 2, divide, 5, minus, 3])) == '((4 - (2 / 5)) - 3)'
def test_evaluation(setup):
"""
Test the evaluation.
"""
plus, minus, times, divide, calc = setup
_, _, _, _, calc = setup
# À toi de tester la fonction __call__ de Calculator.
assert calc("1 + 2") == 3
assert calc("1 - 3") == -2
assert calc("2 / 1") == 2
assert calc("5 * 2") == 10
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment