-
Notifications
You must be signed in to change notification settings - Fork 549
/
parser_test.go
348 lines (315 loc) · 8.2 KB
/
parser_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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*
* Copyright (c) 2021 The GoPlus Authors (goplus.org). All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package parser
import (
"io/fs"
"testing"
"github.com/goplus/gop/ast"
"github.com/goplus/gop/token"
fsx "github.com/qiniu/x/http/fs"
)
// -----------------------------------------------------------------------------
type fileInfo struct {
*fsx.FileInfo
}
func (p fileInfo) Info() (fs.FileInfo, error) {
return nil, fs.ErrNotExist
}
func TestFilter(t *testing.T) {
d := fsx.NewFileInfo("foo.go", 10)
if filter(d, func(fi fs.FileInfo) bool {
return false
}) {
t.Fatal("TestFilter: true?")
}
if !filter(d, func(fi fs.FileInfo) bool {
return true
}) {
t.Fatal("TestFilter: false?")
}
d2 := fileInfo{d}
if filter(d2, func(fi fs.FileInfo) bool {
return true
}) {
t.Fatal("TestFilter: true?")
}
}
func TestAssert(t *testing.T) {
defer func() {
if e := recover(); e != "go/parser internal error: panic msg" {
t.Fatal("TestAssert:", e)
}
}()
assert(false, "panic msg")
}
func panicMsg(e interface{}) string {
switch v := e.(type) {
case string:
return v
case error:
return v.Error()
}
return ""
}
func testErrCode(t *testing.T, code string, errExp, panicExp string) {
defer func() {
if e := recover(); e != nil {
if panicMsg(e) != panicExp {
t.Fatal("testErrCode panic:", e)
}
}
}()
t.Helper()
fset := token.NewFileSet()
_, err := Parse(fset, "/foo/bar.gop", code, 0)
if err == nil || err.Error() != errExp {
t.Fatal("testErrCode error:", err)
}
}
func testErrCodeParseExpr(t *testing.T, code string, errExp, panicExp string) {
defer func() {
if e := recover(); e != nil {
if panicMsg(e) != panicExp {
t.Fatal("testErrCodeParseExpr panic:", e)
}
}
}()
t.Helper()
_, err := ParseExpr(code)
if err == nil || err.Error() != errExp {
t.Fatal("testErrCodeParseExpr error:", err)
}
}
func testClassErrCode(t *testing.T, code string, errExp, panicExp string) {
defer func() {
if e := recover(); e != nil {
if panicMsg(e) != panicExp {
t.Fatal("testErrCode panic:", e)
}
}
}()
t.Helper()
fset := token.NewFileSet()
_, err := Parse(fset, "/foo/bar.gox", code, ParseGoPlusClass)
if err == nil || err.Error() != errExp {
t.Fatal("testErrCode error:", err)
}
}
func TestErrLabel(t *testing.T) {
testErrCode(t, `a.x:`, `/foo/bar.gop:1:4: illegal label declaration`, ``)
}
func TestErrTuple(t *testing.T) {
testErrCode(t, `println (1,2)*2`, `/foo/bar.gop:1:9: tuple is not supported`, ``)
testErrCode(t, `println 2*(1,2)`, `/foo/bar.gop:1:13: expected ')', found ','`, ``)
testErrCode(t, `func test() (int,int) { return (100,100)`, `/foo/bar.gop:1:32: tuple is not supported`, ``)
}
func TestErrOperand(t *testing.T) {
testErrCode(t, `a :=`, `/foo/bar.gop:1:5: expected operand, found 'EOF'`, ``)
}
func TestErrMissingComma(t *testing.T) {
testErrCode(t, `func a(b int c)`, `/foo/bar.gop:1:14: missing ',' in parameter list`, ``)
}
func TestErrLambda(t *testing.T) {
testErrCode(t, `func test(v string, f func( int)) {
}
test "hello" => {
println "lambda",x
}
`, `/foo/bar.gop:3:6: expected 'IDENT', found "hello"`, ``)
testErrCode(t, `func test(v string, f func( int)) {
}
test "hello", "x" => {
println "lambda",x
}
`, `/foo/bar.gop:3:15: expected 'IDENT', found "x"`, ``)
testErrCode(t, `func test(v string, f func( int)) {
}
test "hello", ("x") => {
println "lambda",x
}
`, `/foo/bar.gop:3:16: expected 'IDENT', found "x"`, ``)
testErrCode(t, `func test(v string, f func(int,int)) {
}
test "hello", (x, "y") => {
println "lambda",x,y
}
`, `/foo/bar.gop:3:19: expected 'IDENT', found "y"`, ``)
}
func TestErrTooManyParseExpr(t *testing.T) {
testErrCodeParseExpr(t, `func() int {
var
var
var
var
var
var
var
var
var
var
var
var
}()
`, `3:3: expected 'IDENT', found 'var' (and 10 more errors)`, ``)
}
func TestErrTooMany(t *testing.T) {
testErrCode(t, `
func f() { var }
func g() { var }
func h() { var }
func h() { var }
func h() { var }
func h() { var }
func h() { var }
func h() { var }
func h() { var }
func h() { var }
func h() { var }
func h() { var }
`, `/foo/bar.gop:2:16: expected 'IDENT', found '}' (and 10 more errors)`, ``)
}
func TestErrInFunc(t *testing.T) {
testErrCode(t, `func test() {
a,
}`, `/foo/bar.gop:2:2: expected 1 expression (and 2 more errors)`, ``)
testErrCode(t, `func test() {
a.test, => {
}
}`, `/foo/bar.gop:2:10: expected operand, found '=>' (and 1 more errors)`, ``)
testErrCode(t, `func test() {
,
}
}`, `/foo/bar.gop:2:3: expected statement, found ',' (and 1 more errors)`, ``)
}
// -----------------------------------------------------------------------------
func TestClassErrCode(t *testing.T) {
testClassErrCode(t, `var (
A,B
v int
)
`, `/foo/bar.gox:2:2: missing variable type or initialization`, ``)
testClassErrCode(t, `var (
A.*B
v int
)
`, `/foo/bar.gox:2:4: expected 'IDENT', found '*' (and 2 more errors)`, ``)
testClassErrCode(t, `var (
[]A
v int
)
`, `/foo/bar.gox:2:2: expected 'IDENT', found '['`, ``)
testClassErrCode(t, `var (
*[]A
v int
)
`, `/foo/bar.gox:2:3: expected 'IDENT', found '['`, ``)
testClassErrCode(t, `var (
v int = 10
)
`, `/foo/bar.gox:2:8: syntax error: cannot assign value to field in class file`, ``)
testClassErrCode(t, `var (
v = 10
)
`, `/foo/bar.gox:2:4: syntax error: cannot assign value to field in class file`, ``)
testClassErrCode(t, `
var (
)
const c = 100
const d
`, `/foo/bar.gox:5:7: missing constant value`, ``)
}
func TestErrGlobal(t *testing.T) {
testErrCode(t, `func test() {}
}`, `/foo/bar.gop:2:1: expected statement, found '}'`, ``)
}
func TestErrCompositeLiteral(t *testing.T) {
testErrCode(t, `println (T[int]){a: 1, b: 2}
`, `/foo/bar.gop:1:10: cannot parenthesize type in composite literal`, ``)
}
func TestErrSelectorExpr(t *testing.T) {
testErrCode(t, `
x.
*p
`, `/foo/bar.gop:3:1: expected selector or type assertion, found '*'`, ``)
}
func TestErrStringLitEx(t *testing.T) {
testErrCode(t, `
println "${ ... }"
`, "/foo/bar.gop:2:13: expected operand, found '...'", ``)
testErrCode(t, `
println "${b"
`, "/foo/bar.gop:2:11: invalid $ expression: ${ doesn't end with }", ``)
testErrCode(t, `
println "$a${b}"
`, "/foo/bar.gop:2:10: invalid $ expression: neither `${ ... }` nor `$$`", ``)
}
func TestErrStringLiteral(t *testing.T) {
testErrCode(t, `run "
`, `/foo/bar.gop:1:5: string literal not terminated`, ``)
}
func TestErrFieldDecl(t *testing.T) {
testErrCode(t, `
type T struct {
*(Foo)
}
`, `/foo/bar.gop:3:3: cannot parenthesize embedded type`, ``)
testErrCode(t, `
type T struct {
(Foo)
}
`, `/foo/bar.gop:3:2: cannot parenthesize embedded type`, ``)
testErrCode(t, `
type T struct {
(*Foo)
}
`, `/foo/bar.gop:3:2: cannot parenthesize embedded type`, ``)
}
func TestParseFieldDecl(t *testing.T) {
var p parser
p.init(token.NewFileSet(), "/foo/bar.gop", []byte(`type T struct {
}
`), 0)
p.parseFieldDecl(nil)
}
func TestCheckExpr(t *testing.T) {
var p parser
p.init(token.NewFileSet(), "/foo/bar.gop", []byte(``), 0)
p.checkExpr(&ast.Ellipsis{})
p.checkExpr(&ast.ElemEllipsis{})
p.checkExpr(&ast.StarExpr{})
p.checkExpr(&ast.IndexListExpr{})
p.checkExpr(&ast.FuncType{})
p.checkExpr(&ast.FuncLit{})
}
func TestErrFuncDecl(t *testing.T) {
testErrCode(t, `func test()
{
}
`, `/foo/bar.gop:2:1: unexpected semicolon or newline before {`, ``)
testErrCode(t, `func test() +1
`, `/foo/bar.gop:1:13: expected ';', found '+'`, ``)
testErrCode(t, `
func (a T) +{}
`, `/foo/bar.gop:2:12: expected type, found '+'`, ``)
testErrCode(t, `func +(a T, b T) {}
`, `/foo/bar.gop:1:6: overload operator can only have one parameter`, ``)
}
func TestNumberUnitLit(t *testing.T) {
var p parser
p.checkExpr(&ast.NumberUnitLit{})
p.toIdent(&ast.NumberUnitLit{})
}
// -----------------------------------------------------------------------------