1 |
747 |
jeremybenn |
// Copyright 2009 The Go Authors. All rights reserved.
|
2 |
|
|
// Use of this source code is governed by a BSD-style
|
3 |
|
|
// license that can be found in the LICENSE file.
|
4 |
|
|
|
5 |
|
|
package parser
|
6 |
|
|
|
7 |
|
|
import (
|
8 |
|
|
"go/ast"
|
9 |
|
|
"go/token"
|
10 |
|
|
"os"
|
11 |
|
|
"testing"
|
12 |
|
|
)
|
13 |
|
|
|
14 |
|
|
var fset = token.NewFileSet()
|
15 |
|
|
|
16 |
|
|
var illegalInputs = []interface{}{
|
17 |
|
|
nil,
|
18 |
|
|
3.14,
|
19 |
|
|
[]byte(nil),
|
20 |
|
|
"foo!",
|
21 |
|
|
`package p; func f() { if /* should have condition */ {} };`,
|
22 |
|
|
`package p; func f() { if ; /* should have condition */ {} };`,
|
23 |
|
|
`package p; func f() { if f(); /* should have condition */ {} };`,
|
24 |
|
|
`package p; const c; /* should have constant value */`,
|
25 |
|
|
`package p; func f() { if _ = range x; true {} };`,
|
26 |
|
|
`package p; func f() { switch _ = range x; true {} };`,
|
27 |
|
|
`package p; func f() { for _ = range x ; ; {} };`,
|
28 |
|
|
`package p; func f() { for ; ; _ = range x {} };`,
|
29 |
|
|
`package p; func f() { for ; _ = range x ; {} };`,
|
30 |
|
|
`package p; func f() { switch t = t.(type) {} };`,
|
31 |
|
|
`package p; func f() { switch t, t = t.(type) {} };`,
|
32 |
|
|
`package p; func f() { switch t = t.(type), t {} };`,
|
33 |
|
|
`package p; var a = [1]int; /* illegal expression */`,
|
34 |
|
|
`package p; var a = [...]int; /* illegal expression */`,
|
35 |
|
|
`package p; var a = struct{} /* illegal expression */`,
|
36 |
|
|
`package p; var a = func(); /* illegal expression */`,
|
37 |
|
|
`package p; var a = interface{} /* illegal expression */`,
|
38 |
|
|
`package p; var a = []int /* illegal expression */`,
|
39 |
|
|
`package p; var a = map[int]int /* illegal expression */`,
|
40 |
|
|
`package p; var a = chan int; /* illegal expression */`,
|
41 |
|
|
`package p; var a = []int{[]int}; /* illegal expression */`,
|
42 |
|
|
`package p; var a = ([]int); /* illegal expression */`,
|
43 |
|
|
`package p; var a = a[[]int:[]int]; /* illegal expression */`,
|
44 |
|
|
`package p; var a = <- chan int; /* illegal expression */`,
|
45 |
|
|
`package p; func f() { select { case _ <- chan int: } };`,
|
46 |
|
|
}
|
47 |
|
|
|
48 |
|
|
func TestParseIllegalInputs(t *testing.T) {
|
49 |
|
|
for _, src := range illegalInputs {
|
50 |
|
|
_, err := ParseFile(fset, "", src, 0)
|
51 |
|
|
if err == nil {
|
52 |
|
|
t.Errorf("ParseFile(%v) should have failed", src)
|
53 |
|
|
}
|
54 |
|
|
}
|
55 |
|
|
}
|
56 |
|
|
|
57 |
|
|
var validPrograms = []string{
|
58 |
|
|
"package p\n",
|
59 |
|
|
`package p;`,
|
60 |
|
|
`package p; import "fmt"; func f() { fmt.Println("Hello, World!") };`,
|
61 |
|
|
`package p; func f() { if f(T{}) {} };`,
|
62 |
|
|
`package p; func f() { _ = (<-chan int)(x) };`,
|
63 |
|
|
`package p; func f() { _ = (<-chan <-chan int)(x) };`,
|
64 |
|
|
`package p; func f(func() func() func());`,
|
65 |
|
|
`package p; func f(...T);`,
|
66 |
|
|
`package p; func f(float, ...int);`,
|
67 |
|
|
`package p; func f(x int, a ...int) { f(0, a...); f(1, a...,) };`,
|
68 |
|
|
`package p; func f(int,) {};`,
|
69 |
|
|
`package p; func f(...int,) {};`,
|
70 |
|
|
`package p; func f(x ...int,) {};`,
|
71 |
|
|
`package p; type T []int; var a []bool; func f() { if a[T{42}[0]] {} };`,
|
72 |
|
|
`package p; type T []int; func g(int) bool { return true }; func f() { if g(T{42}[0]) {} };`,
|
73 |
|
|
`package p; type T []int; func f() { for _ = range []int{T{42}[0]} {} };`,
|
74 |
|
|
`package p; var a = T{{1, 2}, {3, 4}}`,
|
75 |
|
|
`package p; func f() { select { case <- c: case c <- d: case c <- <- d: case <-c <- d: } };`,
|
76 |
|
|
`package p; func f() { select { case x := (<-c): } };`,
|
77 |
|
|
`package p; func f() { if ; true {} };`,
|
78 |
|
|
`package p; func f() { switch ; {} };`,
|
79 |
|
|
`package p; func f() { for _ = range "foo" + "bar" {} };`,
|
80 |
|
|
}
|
81 |
|
|
|
82 |
|
|
func TestParseValidPrograms(t *testing.T) {
|
83 |
|
|
for _, src := range validPrograms {
|
84 |
|
|
_, err := ParseFile(fset, "", src, SpuriousErrors)
|
85 |
|
|
if err != nil {
|
86 |
|
|
t.Errorf("ParseFile(%q): %v", src, err)
|
87 |
|
|
}
|
88 |
|
|
}
|
89 |
|
|
}
|
90 |
|
|
|
91 |
|
|
var validFiles = []string{
|
92 |
|
|
"parser.go",
|
93 |
|
|
"parser_test.go",
|
94 |
|
|
}
|
95 |
|
|
|
96 |
|
|
func TestParse3(t *testing.T) {
|
97 |
|
|
for _, filename := range validFiles {
|
98 |
|
|
_, err := ParseFile(fset, filename, nil, DeclarationErrors)
|
99 |
|
|
if err != nil {
|
100 |
|
|
t.Errorf("ParseFile(%s): %v", filename, err)
|
101 |
|
|
}
|
102 |
|
|
}
|
103 |
|
|
}
|
104 |
|
|
|
105 |
|
|
func nameFilter(filename string) bool {
|
106 |
|
|
switch filename {
|
107 |
|
|
case "parser.go":
|
108 |
|
|
case "interface.go":
|
109 |
|
|
case "parser_test.go":
|
110 |
|
|
default:
|
111 |
|
|
return false
|
112 |
|
|
}
|
113 |
|
|
return true
|
114 |
|
|
}
|
115 |
|
|
|
116 |
|
|
func dirFilter(f os.FileInfo) bool { return nameFilter(f.Name()) }
|
117 |
|
|
|
118 |
|
|
func TestParse4(t *testing.T) {
|
119 |
|
|
path := "."
|
120 |
|
|
pkgs, err := ParseDir(fset, path, dirFilter, 0)
|
121 |
|
|
if err != nil {
|
122 |
|
|
t.Fatalf("ParseDir(%s): %v", path, err)
|
123 |
|
|
}
|
124 |
|
|
if len(pkgs) != 1 {
|
125 |
|
|
t.Errorf("incorrect number of packages: %d", len(pkgs))
|
126 |
|
|
}
|
127 |
|
|
pkg := pkgs["parser"]
|
128 |
|
|
if pkg == nil {
|
129 |
|
|
t.Errorf(`package "parser" not found`)
|
130 |
|
|
return
|
131 |
|
|
}
|
132 |
|
|
for filename := range pkg.Files {
|
133 |
|
|
if !nameFilter(filename) {
|
134 |
|
|
t.Errorf("unexpected package file: %s", filename)
|
135 |
|
|
}
|
136 |
|
|
}
|
137 |
|
|
}
|
138 |
|
|
|
139 |
|
|
func TestParseExpr(t *testing.T) {
|
140 |
|
|
// just kicking the tires:
|
141 |
|
|
// a valid expression
|
142 |
|
|
src := "a + b"
|
143 |
|
|
x, err := ParseExpr(src)
|
144 |
|
|
if err != nil {
|
145 |
|
|
t.Errorf("ParseExpr(%s): %v", src, err)
|
146 |
|
|
}
|
147 |
|
|
// sanity check
|
148 |
|
|
if _, ok := x.(*ast.BinaryExpr); !ok {
|
149 |
|
|
t.Errorf("ParseExpr(%s): got %T, expected *ast.BinaryExpr", src, x)
|
150 |
|
|
}
|
151 |
|
|
|
152 |
|
|
// an invalid expression
|
153 |
|
|
src = "a + *"
|
154 |
|
|
_, err = ParseExpr(src)
|
155 |
|
|
if err == nil {
|
156 |
|
|
t.Errorf("ParseExpr(%s): %v", src, err)
|
157 |
|
|
}
|
158 |
|
|
|
159 |
|
|
// it must not crash
|
160 |
|
|
for _, src := range validPrograms {
|
161 |
|
|
ParseExpr(src)
|
162 |
|
|
}
|
163 |
|
|
}
|
164 |
|
|
|
165 |
|
|
func TestColonEqualsScope(t *testing.T) {
|
166 |
|
|
f, err := ParseFile(fset, "", `package p; func f() { x, y, z := x, y, z }`, 0)
|
167 |
|
|
if err != nil {
|
168 |
|
|
t.Errorf("parse: %s", err)
|
169 |
|
|
}
|
170 |
|
|
|
171 |
|
|
// RHS refers to undefined globals; LHS does not.
|
172 |
|
|
as := f.Decls[0].(*ast.FuncDecl).Body.List[0].(*ast.AssignStmt)
|
173 |
|
|
for _, v := range as.Rhs {
|
174 |
|
|
id := v.(*ast.Ident)
|
175 |
|
|
if id.Obj != nil {
|
176 |
|
|
t.Errorf("rhs %s has Obj, should not", id.Name)
|
177 |
|
|
}
|
178 |
|
|
}
|
179 |
|
|
for _, v := range as.Lhs {
|
180 |
|
|
id := v.(*ast.Ident)
|
181 |
|
|
if id.Obj == nil {
|
182 |
|
|
t.Errorf("lhs %s does not have Obj, should", id.Name)
|
183 |
|
|
}
|
184 |
|
|
}
|
185 |
|
|
}
|
186 |
|
|
|
187 |
|
|
func TestVarScope(t *testing.T) {
|
188 |
|
|
f, err := ParseFile(fset, "", `package p; func f() { var x, y, z = x, y, z }`, 0)
|
189 |
|
|
if err != nil {
|
190 |
|
|
t.Errorf("parse: %s", err)
|
191 |
|
|
}
|
192 |
|
|
|
193 |
|
|
// RHS refers to undefined globals; LHS does not.
|
194 |
|
|
as := f.Decls[0].(*ast.FuncDecl).Body.List[0].(*ast.DeclStmt).Decl.(*ast.GenDecl).Specs[0].(*ast.ValueSpec)
|
195 |
|
|
for _, v := range as.Values {
|
196 |
|
|
id := v.(*ast.Ident)
|
197 |
|
|
if id.Obj != nil {
|
198 |
|
|
t.Errorf("rhs %s has Obj, should not", id.Name)
|
199 |
|
|
}
|
200 |
|
|
}
|
201 |
|
|
for _, id := range as.Names {
|
202 |
|
|
if id.Obj == nil {
|
203 |
|
|
t.Errorf("lhs %s does not have Obj, should", id.Name)
|
204 |
|
|
}
|
205 |
|
|
}
|
206 |
|
|
}
|