OSDN Git Service

libgo: Update to weekly.2011-11-01.
[pf3gnuchains/gcc-fork.git] / libgo / go / go / parser / parser_test.go
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.
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 = []interface{}{
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 TestColonEqualsScope(t *testing.T) {
140         f, err := ParseFile(fset, "", `package p; func f() { x, y, z := x, y, z }`, 0)
141         if err != nil {
142                 t.Errorf("parse: %s", err)
143         }
144
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 {
148                 id := v.(*ast.Ident)
149                 if id.Obj != nil {
150                         t.Errorf("rhs %s has Obj, should not", id.Name)
151                 }
152         }
153         for _, v := range as.Lhs {
154                 id := v.(*ast.Ident)
155                 if id.Obj == nil {
156                         t.Errorf("lhs %s does not have Obj, should", id.Name)
157                 }
158         }
159 }
160
161 func TestVarScope(t *testing.T) {
162         f, err := ParseFile(fset, "", `package p; func f() { var x, y, z = x, y, z }`, 0)
163         if err != nil {
164                 t.Errorf("parse: %s", err)
165         }
166
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 {
170                 id := v.(*ast.Ident)
171                 if id.Obj != nil {
172                         t.Errorf("rhs %s has Obj, should not", id.Name)
173                 }
174         }
175         for _, id := range as.Names {
176                 if id.Obj == nil {
177                         t.Errorf("lhs %s does not have Obj, should", id.Name)
178                 }
179         }
180 }