diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index faa48b7ee9fbb0a376a4b24abfcfddc86b77c05e..f3ed343b2bd95db1a3c0622646f4a0d8cc427cb3 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -4,7 +4,6 @@ stages: - dependencies - lint - test - - deploy download_dependencies: stage: dependencies @@ -31,4 +30,13 @@ pylint: # À toi de nous rajouter un petit job pour faire des tests unitaires pytest: - ## Bon courage + stage: test + dependencies: + - download_dependencies + needs: + - download_dependencies + before_script: + - source .venv/bin/activate + script: + - pip install pytest + - pytest calculator diff --git a/calculator/calculator.py b/calculator/calculator.py index ab4570f177c87b43aa675b77bb094d01a156c3e9..6c15ff7a20988dfde81ca82c0ce322fdc9065624 100644 --- a/calculator/calculator.py +++ b/calculator/calculator.py @@ -52,9 +52,9 @@ class Calculator: for i, token in enumerate(tokens): if isinstance(token, Operator): ##################################################################### - ### ATTENTION: Est-ce que c'est vraiment un < (non c'est un <=) ? ### + ### 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 diff --git a/calculator/operators.py b/calculator/operators.py index 5a48950224b9194e4e5f42e3f1dca4a71c6bf132..8da0d85ed561f26108755cd1b3709280f3cee336 100644 --- a/calculator/operators.py +++ b/calculator/operators.py @@ -22,5 +22,5 @@ STANDARD_OPERATORS = { '+': Operator('+', 1, lambda a, b: a + b), '-': Operator('-', 1, lambda a, b: a - b), '*': Operator('×', 2, lambda a, b: a * b), - '/': Operator('/', 2, lambda a, b: a % b), + '/': Operator('/', 2, lambda a, b: a / b), } diff --git a/calculator/test_calculator.py b/calculator/test_calculator.py index 03a63c3e5153f50bc4238e13d9cde8681cb4bd68..a7a07e2fd96657b51df6b87b0ca86bc3e218bf36 100644 --- a/calculator/test_calculator.py +++ b/calculator/test_calculator.py @@ -25,20 +25,34 @@ def test_tokenizer(setup): Test the tokenizer. """ 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("1 + 2 * 3") == [1, plus, 2, times, 3] + assert calc.tokenize( + "1 + 2 * 3 / 4") == [1, plus, 2, times, 3, divide, 4] + assert calc.tokenize( + "1 + 2 * 3 / 4 - 5") == [1, plus, 2, times, 3, divide, 4, minus, 5] def test_parser(setup): """ Test the parser. """ - plus, minus, times, divide, calc = setup - # À toi de tester la fonction parse de Calculator. + _, _, _, _, calc = setup + assert repr(calc.parse(calc.tokenize("1 + 2"))) == '(1 + 2)' + assert repr(calc.parse(calc.tokenize("1 + 2 * 3")) + ) == '(1 + (2 * 3))' + assert repr(calc.parse(calc.tokenize( + "1 + 2 * 3 / 4"))) == '(1 + ((2 * 3) / 4))' + assert repr(calc.parse(calc.tokenize( + "1 + 2 * 3 / 4 - 5"))) == '((1 + ((2 * 3) / 4)) - 5)' def test_evaluation(setup): """ Test the evaluation. """ - plus, minus, times, divide, calc = setup - # À toi de tester la fonction __call__ de Calculator. + _, _, _, _, calc = setup + assert calc("1 + 2") == 3 + assert calc("1 + 2 * 3") == 7 + assert calc("1 + 2 * 3 / 4") == 2.5 + assert calc("1 + 2 * 3 / 4 - 5") == -2.5