Skip to content

Commit c39ff79

Browse files
committed
Various tests for the parser
1 parent b137011 commit c39ff79

File tree

1 file changed

+146
-0
lines changed

1 file changed

+146
-0
lines changed

pkg/parser/parser_test.go

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
package parser
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func TestParseNumbers(t *testing.T) {
9+
tests := []struct {
10+
input string
11+
want []ASTNode
12+
}{
13+
{"42", []ASTNode{&NumberNode{Value: "42"}}},
14+
{"3.14", []ASTNode{&NumberNode{Value: "3.14"}}},
15+
}
16+
17+
for _, tt := range tests {
18+
l, err := NewLexer(tt.input)
19+
if err != nil {
20+
t.Fatalf("Failed to tokenize input `%s`: %v", tt.input, err)
21+
continue
22+
}
23+
p, err := NewParser(l.Tokens)
24+
if err != nil {
25+
t.Fatalf("Failed to initialize parser with tokens from input `%s`: %v", tt.input, err)
26+
continue
27+
}
28+
if !reflect.DeepEqual(p.Nodes, tt.want) {
29+
t.Errorf("Failed to parse expression. Got `%v`, expected `%v`.", p.Nodes, tt.want)
30+
}
31+
}
32+
}
33+
34+
func TestParseUnaryOperations(t *testing.T) {
35+
tests := []struct {
36+
input string
37+
want []ASTNode
38+
}{
39+
{"! 8", []ASTNode{&UnaryOpNode{Operand: &NumberNode{Value: "8"}, Op: FACT}}},
40+
{"! ! 4", []ASTNode{
41+
&UnaryOpNode{
42+
Operand: &UnaryOpNode{Operand: &NumberNode{Value: "4"}, Op: FACT},
43+
Op: FACT,
44+
},
45+
}},
46+
}
47+
48+
for _, tt := range tests {
49+
l, err := NewLexer(tt.input)
50+
if err != nil {
51+
t.Fatalf("Failed to tokenize input `%s`: %v", tt.input, err)
52+
}
53+
p, err := NewParser(l.Tokens)
54+
if err != nil {
55+
t.Fatalf("Failed to initialize parser with tokens from input `%s`: %v", tt.input, err)
56+
}
57+
if !reflect.DeepEqual(p.Nodes, tt.want) {
58+
t.Errorf("Failed to parse binary operation. Got `%v`, expected `%v`.", p.Nodes, tt.want)
59+
}
60+
}
61+
}
62+
63+
func TestParseBinaryOperations(t *testing.T) {
64+
tests := []struct {
65+
input string
66+
want []ASTNode
67+
}{
68+
{"+ 30 55.0", []ASTNode{
69+
&BinaryOpNode{
70+
Left: &NumberNode{Value: "30"},
71+
Op: ADD,
72+
Right: &NumberNode{Value: "55.0"},
73+
},
74+
}},
75+
{"^ 2 ! 3", []ASTNode{
76+
&BinaryOpNode{
77+
Left: &NumberNode{Value: "2"},
78+
Op: POW,
79+
Right: &UnaryOpNode{Operand: &NumberNode{Value: "3"}, Op: FACT},
80+
},
81+
}},
82+
{"^ 1000 ! 0", []ASTNode{
83+
&BinaryOpNode{
84+
Left: &NumberNode{Value: "1000"},
85+
Op: POW,
86+
Right: &UnaryOpNode{Operand: &NumberNode{Value: "0"}, Op: FACT},
87+
},
88+
}},
89+
}
90+
91+
for _, tt := range tests {
92+
l, err := NewLexer(tt.input)
93+
if err != nil {
94+
t.Fatalf("Failed to tokenize input `%s`: %v", tt.input, err)
95+
}
96+
p, err := NewParser(l.Tokens)
97+
if err != nil {
98+
t.Fatalf("Failed to initialize parser with tokens from input `%s`: %v", tt.input, err)
99+
}
100+
if !reflect.DeepEqual(p.Nodes, tt.want) {
101+
t.Errorf("Failed to parse binary operation. Got `%v`, expected `%v`.", p.Nodes, tt.want)
102+
}
103+
}
104+
}
105+
106+
func TestParseVariableDeclaration(t *testing.T) {
107+
tests := []struct {
108+
input string
109+
want []ASTNode
110+
}{
111+
{"pi = 3.14", []ASTNode{
112+
&VariableDeclNode{
113+
Variable: &IdentifierNode{Value: "pi"},
114+
Value: &NumberNode{Value: "3.14"},
115+
},
116+
}},
117+
{"currYear = - ^ 2 11 24", []ASTNode{
118+
&VariableDeclNode{
119+
Variable: &IdentifierNode{Value: "currYear"},
120+
Value: &BinaryOpNode{
121+
Left: &BinaryOpNode{
122+
Left: &NumberNode{Value: "2"},
123+
Op: POW,
124+
Right: &NumberNode{Value: "11"},
125+
},
126+
Op: SUB,
127+
Right: &NumberNode{Value: "24"},
128+
},
129+
},
130+
}},
131+
}
132+
133+
for _, tt := range tests {
134+
l, err := NewLexer(tt.input)
135+
if err != nil {
136+
t.Fatalf("Failed to tokenize input `%s`: %v", tt.input, err)
137+
}
138+
p, err := NewParser(l.Tokens)
139+
if err != nil {
140+
t.Fatalf("Failed to initialize parser with tokens from input `%s`: %v", tt.input, err)
141+
}
142+
if !reflect.DeepEqual(p.Nodes, tt.want) {
143+
t.Errorf("Failed to parse variable declaration. Got `%v`, expected `%v`.", p.Nodes, tt.want)
144+
}
145+
}
146+
}

0 commit comments

Comments
 (0)