-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
217 lines (204 loc) · 4.38 KB
/
main_test.go
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package main
import (
"fmt"
"os"
"path"
"path/filepath"
"testing"
"uBasic/ast"
"uBasic/ast/astutil"
"uBasic/errors"
"uBasic/lexer"
"uBasic/parser"
"uBasic/sem"
"uBasic/source"
)
type TI struct {
src string
}
var testData = []*TI{
{`' this is a comment `},
{`Print Chr(65)`},
{`For t = 10 To 0 Step -2
Debug.Print t
Next t`},
{`Dim b As String`},
{`Dim b As Boolean`},
{`Dim aa As Long`},
{`Debug.Print ""`},
}
func PrintNode(n ast.Node) error {
fmt.Println(n.String())
return nil
}
func Test1(t *testing.T) {
pass := true
for _, testCase := range testData {
l := lexer.New(testCase.src)
p := parser.New(l)
file := p.ParseFile()
if file != nil {
f := func(n ast.Node) error {
t.Log(n.String())
return nil
}
astutil.Walk(file, f)
}
}
if !pass {
t.Fail()
}
}
func Test2(t *testing.T) {
pass := true
// read files from samples directory
files, err := os.ReadDir("./testdata/samples")
if err != nil {
t.Log(err)
pass = false
}
for _, file := range files {
if filepath.Ext(file.Name()) == ".bas" {
t.Log(file.Name())
// read file
data, err := os.ReadFile("./testdata/samples/" + file.Name())
if err != nil {
t.Log(err)
pass = false
} else {
l := lexer.New(string(data))
p := parser.New(l)
file := p.ParseFile()
if file != nil {
f := func(n ast.Node) error {
t.Log(n.String())
return nil
}
astutil.Walk(file, f)
}
}
}
}
if !pass {
t.Fail()
}
}
var directories = []string{
"testdata/samples",
// "testdata/incorrect/parser",
"testdata/incorrect/semantic",
// "testdata/incorrect",
"testdata/noisy",
"testdata/noisy/advanced",
"testdata/noisy/medium",
"testdata/noisy/simple",
"testdata/quiet",
// "testdata/quiet/lexer",
// "testdata/quiet/parser",
"testdata/quiet/rtl",
}
func Test3(t *testing.T) {
pass := true
for _, dir := range directories {
files, err := os.ReadDir(dir)
if err != nil {
t.Log(err)
pass = false
}
for _, f := range files {
if filepath.Ext(f.Name()) == ".bas" {
filename := path.Join(dir, f.Name())
t.Log(filename)
// read file
data, err := os.ReadFile(filename)
if err != nil {
t.Log(err)
pass = false
} else {
l := lexer.New(string(data))
p := parser.New(l)
file := p.ParseFile()
if file == nil {
for _, err := range p.Errors() {
e := err.(*errors.Error)
e.Source = &source.Source{Input: string(data), Name: filename}
t.Log(err.Error())
}
} else {
file.Name = filename
if _, err := sem.Check(file); err != nil {
pass = false
e := err.(*errors.Error)
e.Source = &source.Source{Input: string(data), Name: filename}
t.Log(err.Error())
} else {
t.Log("File: ", filename, " passed semantic analysis.")
}
}
}
}
}
}
if !pass {
t.Fail()
}
}
// func Test4(t *testing.T) {
// pass := true
// for _, dir := range directories {
// files, err := os.ReadDir(dir)
// if err != nil {
// t.Log(err)
// pass = false
// }
// for _, f := range files {
// if filepath.Ext(f.Name()) == ".bas" {
// filename := path.Join(dir, f.Name())
// t.Log(filename)
// // read file
// data, err := os.ReadFile(filename)
// if err != nil {
// t.Log(err)
// pass = false
// } else {
// s := &source.Source{Input: string(data), Name: filename}
// l := lexer.New(string(data))
// p := parser.New(l)
// file := p.ParseFile()
// if file == nil {
// for _, err := range p.Errors() {
// e := err.(*errors.Error)
// e.Src = s
// t.Log(err.Error())
// }
// } else {
// file.Name = filename
// if _, err := sem.Save(file); err != nil {
// pass = false
// e := err.(*errors.Error)
// e.Src = s
// t.Log(err.Error())
// } else {
// file2, err := ast.LoadFile(filename + "x")
// if err != nil {
// pass = false
// t.Log(err.Error())
// } else {
// errNode := file.EqualsNode(file2)
// if errNode != nil {
// pass = false
// t.Log("Files are not equal : ", filename, " at ", errNode.Token().Position.String()+" : "+errNode.String())
// } else {
// t.Log("Files are equal: " + filename)
// }
// }
// }
// }
// }
// }
// }
// }
// if !pass {
// t.Fail()
// }
// }