forked from davidcallanan/py-simple-math-interpreter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser_test.py
83 lines (70 loc) · 1.85 KB
/
parser_test.py
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
import unittest
from tokens import Token, TokenType
from parser_ import Parser
from nodes import *
class TestParser(unittest.TestCase):
def test_empty(self):
tokens = []
node = Parser(tokens).parse()
self.assertEqual(node, None)
def test_numbers(self):
tokens = [Token(TokenType.NUMBER, 51.2)]
node = Parser(tokens).parse()
self.assertEqual(node, NumberNode(51.2))
def test_single_operations(self):
tokens = [
Token(TokenType.NUMBER, 27),
Token(TokenType.PLUS),
Token(TokenType.NUMBER, 14),
]
node = Parser(tokens).parse()
self.assertEqual(node, AddNode(NumberNode(27), NumberNode(14)))
tokens = [
Token(TokenType.NUMBER, 27),
Token(TokenType.MINUS),
Token(TokenType.NUMBER, 14),
]
node = Parser(tokens).parse()
self.assertEqual(node, SubtractNode(NumberNode(27), NumberNode(14)))
tokens = [
Token(TokenType.NUMBER, 27),
Token(TokenType.MULTIPLY),
Token(TokenType.NUMBER, 14),
]
node = Parser(tokens).parse()
self.assertEqual(node, MultiplyNode(NumberNode(27), NumberNode(14)))
tokens = [
Token(TokenType.NUMBER, 27),
Token(TokenType.DIVIDE),
Token(TokenType.NUMBER, 14),
]
node = Parser(tokens).parse()
self.assertEqual(node, DivideNode(NumberNode(27), NumberNode(14)))
def test_full_expression(self):
tokens = [
Token(TokenType.NUMBER, 27),
Token(TokenType.PLUS),
Token(TokenType.LPAREN),
Token(TokenType.NUMBER, 43),
Token(TokenType.DIVIDE),
Token(TokenType.NUMBER, 36),
Token(TokenType.MINUS),
Token(TokenType.NUMBER, 48),
Token(TokenType.RPAREN),
Token(TokenType.MULTIPLY),
Token(TokenType.NUMBER, 51),
]
node = Parser(tokens).parse()
self.assertEqual(node, AddNode(
NumberNode(27),
MultiplyNode(
SubtractNode(
DivideNode(
NumberNode(43),
NumberNode(36)
),
NumberNode(48)
),
NumberNode(51)
)
))