Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found
Select Git revision
Loading items

Target

Select target project
  • octolel/cicd
  • bruliette/cicd-2022-juju
  • desjardins/cicd
  • greets/cicd
  • 2022faureal/cicd
  • 2022flamama/cicd
  • 2022thirionma/cicd
  • siestetactique/cicd
  • 2021khlauthu/cicd
  • aubinx/cicd
  • 2022jacquinar/cicd
  • gravlax/cicd
12 results
Select Git revision
Loading items
Show changes
Commits on Source (2)
...@@ -31,4 +31,13 @@ pylint: ...@@ -31,4 +31,13 @@ pylint:
# À toi de nous rajouter un petit job pour faire des tests unitaires # À toi de nous rajouter un petit job pour faire des tests unitaires
pytest: pytest:
## Bon courage stage: test
dependencies:
- download_dependencies
needs:
- download_dependencies
before_script:
- source .venv/bin/activate
- pip install pytest
script:
- pytest calculator
...@@ -54,7 +54,7 @@ class Calculator: ...@@ -54,7 +54,7 @@ class Calculator:
##################################################################### #####################################################################
### 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 = token
operator_index = i operator_index = i
......
...@@ -21,6 +21,6 @@ class Operator: ...@@ -21,6 +21,6 @@ class Operator:
STANDARD_OPERATORS = { STANDARD_OPERATORS = {
'+': Operator('+', 1, lambda a, b: a + b), '+': Operator('+', 1, lambda a, b: a + b),
'-': 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), '/': Operator('/', 2, lambda a, b: a / b),
} }
...@@ -3,7 +3,7 @@ Test module for the calculator module. ...@@ -3,7 +3,7 @@ Test module for the calculator module.
""" """
import pytest import pytest
from calculator.calculator import Calculator from calculator.calculator import Calculator
from calculator.operators import Operator from calculator.operators import STANDARD_OPERATORS, Operator
@pytest.fixture(scope="module", name="setup") @pytest.fixture(scope="module", name="setup")
...@@ -11,20 +11,19 @@ def fixture_setup(): ...@@ -11,20 +11,19 @@ def fixture_setup():
""" """
Setup the test suite, by instantiating the calculator and the operators. Setup the test suite, by instantiating the calculator and the operators.
""" """
plus = Operator('+', 1, lambda a, b: a + b)
minus = Operator('-', 1, lambda a, b: a - b) plus = STANDARD_OPERATORS['+']
times = Operator('*', 2, lambda a, b: a * b) minus = STANDARD_OPERATORS['-']
divide = Operator('/', 2, lambda a, b: a / b) times = STANDARD_OPERATORS['*']
calculator = Calculator( divide = STANDARD_OPERATORS['/']
operators={'+': plus, '-': minus, '*': times, '/': divide}) calculator = Calculator(STANDARD_OPERATORS)
yield plus, minus, times, divide, calculator yield plus, minus, times, divide, calculator
def test_tokenizer(setup): def test_tokenizer(setup):
"""
Test the tokenizer.
"""
plus, minus, times, divide, calc = setup plus, minus, times, divide, calc = setup
assert calc.tokenize("1 + 2 + 4 / 2") == [1,plus,2,plus,4,divide,2]
assert calc.tokenize("1 * 0 - 4 / 2") == [1,times,0,minus,4,divide,2]
# À toi de tester la fonction tokenize de Calculator. # À toi de tester la fonction tokenize de Calculator.
...@@ -32,13 +31,23 @@ def test_parser(setup): ...@@ -32,13 +31,23 @@ def test_parser(setup):
""" """
Test the parser. Test the parser.
""" """
plus, minus, times, divide, calc = setup _, _, _, _, calc = setup
# À toi de tester la fonction parse de Calculator. 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): def test_evaluation(setup):
""" """
Test the evaluation. 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 + 2 * 3") == 7
assert calc("1 + 2 * 3 / 4") == 2.5
assert calc("1 + 2 * 3 / 4 - 5") == -2.5