-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_parse.py
74 lines (69 loc) · 2 KB
/
test_parse.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
from trax_ast import *
from trax_parser import parse
def test_parse_struct():
code = """
struct Point {
x;
y;
}
"""
result = parse(code)
assert result == [Struct('Point', [Field('x'), Field('y')])]
def test_parse_return_stmt():
code = """
fn Int:square() {
return self * self;
}
"""
result = parse(code)
assert result == [Method('Int', 'square', [],
Block([
Return(MethodCall(Qualified(['self']), '*', [Qualified(['self'])]))
]))]
def test_parse_method():
code = """
fn Point:new(x, y) {
self.x = x;
self.y = y;
}
"""
result = parse(code)
assert result == [Method('Point', 'new', ['x', 'y'],
Block([
Assign(Qualified(['self', 'x']), Qualified(['x'])),
Assign(Qualified(['self', 'y']), Qualified(['y']))
]))]
def test_parse_for_loop():
code = """
fn List:map(f) {
for item in self {
f call(item);
}
}
"""
result = parse(code)
assert result == [Method('List', 'map', ['f'],
Block([
ForLoop('item', Qualified(['self']),
Block([
ExprStmt(MethodCall(Qualified(['f']), 'call', [Qualified(['item'])]))
]))
]))]
def test_parse_if_else():
code = """
fn Int:max(b) {
if self > b {
self;
} else {
b;
}
}
"""
result = parse(code)
assert result == [Method('Int', 'max', ['b'],
Block([
IfElse(
MethodCall(Qualified(['self']), '>', [Qualified(['b'])]),
Block([ExprStmt(Qualified(['self']))]),
Block([ExprStmt(Qualified(['b']))]))
]))]