diff --git a/calculator/calculator.py b/calculator/calculator.py index 2faf03019c9cc84f65195d180eaafc267bbdf146..12cb46493c7dbacfe6fa059c360fde87a282bf0c 100644 --- a/calculator/calculator.py +++ b/calculator/calculator.py @@ -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))() diff --git a/calculator/test_calculator.py b/calculator/test_calculator.py index 8197bb7054addbe9f3f305420f9346302eaab824..6d92456e3ec96a32f647256718e3617e385185ae 100644 --- a/calculator/test_calculator.py +++ b/calculator/test_calculator.py @@ -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