~ubuntu-branches/ubuntu/vivid/golang/vivid

« back to all changes in this revision

Viewing changes to src/pkg/go/parser/parser_test.go

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2013-08-20 14:06:23 UTC
  • mfrom: (14.1.23 saucy-proposed)
  • Revision ID: package-import@ubuntu.com-20130820140623-b414jfxi3m0qkmrq
Tags: 2:1.1.2-2ubuntu1
* Merge from Debian unstable (LP: #1211749, #1202027). Remaining changes:
  - 016-armhf-elf-header.patch: Use correct ELF header for armhf binaries.
  - d/control,control.cross: Update Breaks/Replaces for Ubuntu
    versions to ensure smooth upgrades, regenerate control file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
68
68
 
69
69
func TestParseExpr(t *testing.T) {
70
70
        // just kicking the tires:
71
 
        // a valid expression
 
71
        // a valid arithmetic expression
72
72
        src := "a + b"
73
73
        x, err := ParseExpr(src)
74
74
        if err != nil {
79
79
                t.Errorf("ParseExpr(%s): got %T, expected *ast.BinaryExpr", src, x)
80
80
        }
81
81
 
 
82
        // a valid type expression
 
83
        src = "struct{x *int}"
 
84
        x, err = ParseExpr(src)
 
85
        if err != nil {
 
86
                t.Fatalf("ParseExpr(%s): %v", src, err)
 
87
        }
 
88
        // sanity check
 
89
        if _, ok := x.(*ast.StructType); !ok {
 
90
                t.Errorf("ParseExpr(%s): got %T, expected *ast.StructType", src, x)
 
91
        }
 
92
 
82
93
        // an invalid expression
83
94
        src = "a + *"
84
95
        _, err = ParseExpr(src)
135
146
        }
136
147
}
137
148
 
 
149
func TestObjects(t *testing.T) {
 
150
        const src = `
 
151
package p
 
152
import fmt "fmt"
 
153
const pi = 3.14
 
154
type T struct{}
 
155
var x int
 
156
func f() { L: }
 
157
`
 
158
 
 
159
        f, err := ParseFile(fset, "", src, 0)
 
160
        if err != nil {
 
161
                t.Fatal(err)
 
162
        }
 
163
 
 
164
        objects := map[string]ast.ObjKind{
 
165
                "p":   ast.Bad, // not in a scope
 
166
                "fmt": ast.Bad, // not resolved yet
 
167
                "pi":  ast.Con,
 
168
                "T":   ast.Typ,
 
169
                "x":   ast.Var,
 
170
                "int": ast.Bad, // not resolved yet
 
171
                "f":   ast.Fun,
 
172
                "L":   ast.Lbl,
 
173
        }
 
174
 
 
175
        ast.Inspect(f, func(n ast.Node) bool {
 
176
                if ident, ok := n.(*ast.Ident); ok {
 
177
                        obj := ident.Obj
 
178
                        if obj == nil {
 
179
                                if objects[ident.Name] != ast.Bad {
 
180
                                        t.Errorf("no object for %s", ident.Name)
 
181
                                }
 
182
                                return true
 
183
                        }
 
184
                        if obj.Name != ident.Name {
 
185
                                t.Errorf("names don't match: obj.Name = %s, ident.Name = %s", obj.Name, ident.Name)
 
186
                        }
 
187
                        kind := objects[ident.Name]
 
188
                        if obj.Kind != kind {
 
189
                                t.Errorf("%s: obj.Kind = %s; want %s", ident.Name, obj.Kind, kind)
 
190
                        }
 
191
                }
 
192
                return true
 
193
        })
 
194
}
 
195
 
138
196
func TestUnresolved(t *testing.T) {
139
197
        f, err := ParseFile(fset, "", `
140
198
package p