1 // 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.
14 var fset = token.NewFileSet()
16 var illegalInputs = []interface{}{
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: } };`,
48 func TestParseIllegalInputs(t *testing.T) {
49 for _, src := range illegalInputs {
50 _, err := ParseFile(fset, "", src, 0)
52 t.Errorf("ParseFile(%v) should have failed", src)
57 var validPrograms = []interface{}{
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" {} };`,
82 func TestParseValidPrograms(t *testing.T) {
83 for _, src := range validPrograms {
84 _, err := ParseFile(fset, "", src, SpuriousErrors)
86 t.Errorf("ParseFile(%q): %v", src, err)
91 var validFiles = []string{
96 func TestParse3(t *testing.T) {
97 for _, filename := range validFiles {
98 _, err := ParseFile(fset, filename, nil, DeclarationErrors)
100 t.Errorf("ParseFile(%s): %v", filename, err)
105 func nameFilter(filename string) bool {
109 case "parser_test.go":
116 func dirFilter(f *os.FileInfo) bool { return nameFilter(f.Name) }
118 func TestParse4(t *testing.T) {
120 pkgs, err := ParseDir(fset, path, dirFilter, 0)
122 t.Fatalf("ParseDir(%s): %v", path, err)
125 t.Errorf("incorrect number of packages: %d", len(pkgs))
127 pkg := pkgs["parser"]
129 t.Errorf(`package "parser" not found`)
132 for filename := range pkg.Files {
133 if !nameFilter(filename) {
134 t.Errorf("unexpected package file: %s", filename)
139 func TestColonEqualsScope(t *testing.T) {
140 f, err := ParseFile(fset, "", `package p; func f() { x, y, z := x, y, z }`, 0)
142 t.Errorf("parse: %s", err)
145 // RHS refers to undefined globals; LHS does not.
146 as := f.Decls[0].(*ast.FuncDecl).Body.List[0].(*ast.AssignStmt)
147 for _, v := range as.Rhs {
150 t.Errorf("rhs %s has Obj, should not", id.Name)
153 for _, v := range as.Lhs {
156 t.Errorf("lhs %s does not have Obj, should", id.Name)
161 func TestVarScope(t *testing.T) {
162 f, err := ParseFile(fset, "", `package p; func f() { var x, y, z = x, y, z }`, 0)
164 t.Errorf("parse: %s", err)
167 // RHS refers to undefined globals; LHS does not.
168 as := f.Decls[0].(*ast.FuncDecl).Body.List[0].(*ast.DeclStmt).Decl.(*ast.GenDecl).Specs[0].(*ast.ValueSpec)
169 for _, v := range as.Values {
172 t.Errorf("rhs %s has Obj, should not", id.Name)
175 for _, id := range as.Names {
177 t.Errorf("lhs %s does not have Obj, should", id.Name)