File size: 1,525 Bytes
c1eb5fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
"""Tests for the math solver used by capability_router.

Previously this file printed results and asserted nothing, so it passed no
matter what the solver returned — and its bare `from math_solver import`
only resolved when pytest ran from inside this directory, which broke
collection for the whole suite.
"""
from .math_solver import extract_math_expression, is_math_question, solve_math_problem


def test_recognises_math_questions():
    assert is_math_question("2x+4=0 find x")
    assert is_math_question("solve for x: 3x - 6 = 0")


def test_ignores_non_math_questions():
    assert not is_math_question("When does the next term start?")
    assert not is_math_question("Who is my academic adviser?")


def test_extracts_the_equation_from_surrounding_words():
    # The equation should survive being embedded in a sentence, with or
    # without a space before the trailing instruction.
    assert "2x+4=0" in extract_math_expression("2x+4=0 find x").replace(" ", "")
    assert "2x+4=0" in extract_math_expression("2x+4=0find x").replace(" ", "")
    assert "3x-6=0" in extract_math_expression("solve for x: 3x - 6 = 0").replace(" ", "")


def test_solves_a_linear_equation():
    answer, steps = solve_math_problem("2x+4=0 find x")
    # 2x + 4 = 0  ->  x = -2
    assert "-2" in answer
    assert isinstance(steps, list) and steps


def test_returns_working_steps():
    _, steps = solve_math_problem("solve for x: 3x - 6 = 0")
    assert isinstance(steps, list)
    assert all(isinstance(s, str) for s in steps)