Select Git revision
test_calculator.py
Forked from an inaccessible project.
test_calculator.py 1.51 KiB
"""
Test module for the calculator module.
"""
import pytest
from calculator.calculator import Calculator
from calculator.operators import STANDARD_OPERATORS, Operator
@pytest.fixture(scope="module", name="setup")
def fixture_setup():
"""
Setup the test suite, by instantiating the calculator and the operators.
"""
plus = STANDARD_OPERATORS['+']
minus = STANDARD_OPERATORS['-']
times = STANDARD_OPERATORS['*']
divide = STANDARD_OPERATORS['/']
calculator = Calculator(STANDARD_OPERATORS)
yield plus, minus, times, divide, calculator
def test_tokenizer(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.
def test_parser(setup):
"""
Test the parser.
"""
_, _, _, _, 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.
"""
_, _, _, _, 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