Repository navigation
Expand file tree
/
Copy pathtest_expression_parser.py
More file actions
118 lines (107 loc) · 3.04 KB
/
Copy pathtest_expression_parser.py
File metadata and controls
118 lines (107 loc) · 3.04 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import pytest
from app.expression import (
BaseNode,
BinaryOpNode,
BinaryOperation,
Operation,
UnaryOperation,
UnitaryOpNode,
)
from app.expression_parser import (
_add_node_to_stack,
_is_operation,
_symbol_to_op,
parse_from_string,
)
@pytest.mark.parametrize(
"symbol,outcome",
[
("+", True),
("-", True),
("^", True),
("/", True),
("*", True),
("√", True),
(".", False),
(" ", False),
(",", False),
("1", False),
],
)
def test_is_operation(symbol, outcome):
assert _is_operation(symbol) == outcome
@pytest.mark.parametrize(
"symbol,outcome",
[
("+", BinaryOperation.ADD),
("-", BinaryOperation.SUBTRACT),
("^", BinaryOperation.POW),
("/", BinaryOperation.DIVIDE),
("*", BinaryOperation.MULTIPLY),
],
)
def test_get_op_from_symbol_binary_ops(symbol, outcome):
result = _symbol_to_op(symbol)
assert isinstance(result, Operation)
assert result == outcome
@pytest.mark.parametrize(
"symbol,is_unary,outcome",
[
("√", True, UnaryOperation.SQRT),
("-", True, UnaryOperation.MINUS),
("-", False, BinaryOperation.SUBTRACT),
],
)
def test_get_op_from_symbol_unary_ops(symbol, is_unary, outcome):
result = _symbol_to_op(symbol, is_unary=is_unary)
assert isinstance(result, Operation)
assert result == outcome
# @pytest.mark.parametrize(
# "current_stack,new_node,expected",
# [
# ([None], BaseNode(lhs=2), [None, BaseNode(lhs=2)]),
# (
# [None, BaseNode(lhs=2), BinaryOperation.ADD],
# BaseNode(lhs=2),
# [
# None,
# BinaryOpNode(
# op=BinaryOperation.ADD, lhs=BaseNode(lhs=2), rhs=BaseNode(lhs=2)
# ),
# ],
# ),
# (
# [None, UnaryOperation.MINUS],
# BaseNode(lhs=2),
# [None, UnitaryOpNode(lhs=BaseNode(lhs=2), op=UnaryOperation.MINUS)],
# ),
# ],
# )
# def test_add_node_to_stack(current_stack, new_node, expected):
# new_stack = _add_node_to_stack(new_node, current_stack)
# assert new_stack == expected
# @pytest.mark.parametrize(
# "expression, expected",
# [
# ("1", BaseNode(lhs=1)),
# (
# "1+2",
# BinaryOpNode(
# lhs=BaseNode(lhs=1), op=BinaryOperation.ADD, rhs=BaseNode(lhs=2)
# ),
# ),
# (
# "1+2*4",
# BinaryOpNode(
# lhs=BinaryOpNode(
# lhs=BaseNode(lhs=1), op=BinaryOperation.ADD, rhs=BaseNode(lhs=2)
# ),
# op=BinaryOperation.MULTIPLY,
# rhs=BaseNode(lhs=4),
# ),
# ), # This is actually incorrect, but program doesn't handle operation precedence yet
# ],
# )
# def test_parse_expression(expression, expected):
# parsed_expression = parse_from_string(expression)
# assert parsed_expression == expected