~john-koepi/ubuntu/trusty/golang/default

« back to all changes in this revision

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

  • Committer: Package Import Robot
  • Author(s): Ondřej Surý, Ondřej Surý, Michael Stapelberg
  • Date: 2012-06-28 12:14:15 UTC
  • mfrom: (1.1.15)
  • mto: (3.1.5 experimental) (14.3.1 saucy)
  • mto: This revision was merged to the branch mainline in revision 24.
  • Revision ID: package-import@ubuntu.com-20120628121415-w1b0076ixkarr1ml
[ Ondřej Surý ]
* Imported Upstream version 1.0.2
* Update Vcs fields to reflect new git repository location
* Kill get-orig-source, since 1.0.0, the tarballs can be downloaded
  from webpage

[ Michael Stapelberg ]
* golang-mode: use debian-pkg-add-load-path-item (Closes: #664802)
* Add manpages (Closes: #632964)
* Use updated pt.po from Pedro Ribeiro (Closes: #674958)

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
package parser
6
6
 
7
7
import (
 
8
        "bytes"
8
9
        "fmt"
9
10
        "go/ast"
10
11
        "go/token"
11
12
        "os"
 
13
        "strings"
12
14
        "testing"
13
15
)
14
16
 
25
27
        for _, filename := range validFiles {
26
28
                _, err := ParseFile(fset, filename, nil, DeclarationErrors)
27
29
                if err != nil {
28
 
                        t.Errorf("ParseFile(%s): %v", filename, err)
 
30
                        t.Fatalf("ParseFile(%s): %v", filename, err)
29
31
                }
30
32
        }
31
33
}
70
72
        src := "a + b"
71
73
        x, err := ParseExpr(src)
72
74
        if err != nil {
73
 
                t.Errorf("ParseExpr(%s): %v", src, err)
 
75
                t.Fatalf("ParseExpr(%s): %v", src, err)
74
76
        }
75
77
        // sanity check
76
78
        if _, ok := x.(*ast.BinaryExpr); !ok {
81
83
        src = "a + *"
82
84
        _, err = ParseExpr(src)
83
85
        if err == nil {
84
 
                t.Errorf("ParseExpr(%s): %v", src, err)
 
86
                t.Fatalf("ParseExpr(%s): %v", src, err)
85
87
        }
86
88
 
87
89
        // it must not crash
93
95
func TestColonEqualsScope(t *testing.T) {
94
96
        f, err := ParseFile(fset, "", `package p; func f() { x, y, z := x, y, z }`, 0)
95
97
        if err != nil {
96
 
                t.Errorf("parse: %s", err)
 
98
                t.Fatal(err)
97
99
        }
98
100
 
99
101
        // RHS refers to undefined globals; LHS does not.
115
117
func TestVarScope(t *testing.T) {
116
118
        f, err := ParseFile(fset, "", `package p; func f() { var x, y, z = x, y, z }`, 0)
117
119
        if err != nil {
118
 
                t.Errorf("parse: %s", err)
 
120
                t.Fatal(err)
119
121
        }
120
122
 
121
123
        // RHS refers to undefined globals; LHS does not.
133
135
        }
134
136
}
135
137
 
 
138
func TestUnresolved(t *testing.T) {
 
139
        f, err := ParseFile(fset, "", `
 
140
package p
 
141
//
 
142
func f1a(int)
 
143
func f2a(byte, int, float)
 
144
func f3a(a, b int, c float)
 
145
func f4a(...complex)
 
146
func f5a(a s1a, b ...complex)
 
147
//
 
148
func f1b(*int)
 
149
func f2b([]byte, (int), *float)
 
150
func f3b(a, b *int, c []float)
 
151
func f4b(...*complex)
 
152
func f5b(a s1a, b ...[]complex)
 
153
//
 
154
type s1a struct { int }
 
155
type s2a struct { byte; int; s1a }
 
156
type s3a struct { a, b int; c float }
 
157
//
 
158
type s1b struct { *int }
 
159
type s2b struct { byte; int; *float }
 
160
type s3b struct { a, b *s3b; c []float }
 
161
`, 0)
 
162
        if err != nil {
 
163
                t.Fatal(err)
 
164
        }
 
165
 
 
166
        want := "int " + // f1a
 
167
                "byte int float " + // f2a
 
168
                "int float " + // f3a
 
169
                "complex " + // f4a
 
170
                "complex " + // f5a
 
171
                //
 
172
                "int " + // f1b
 
173
                "byte int float " + // f2b
 
174
                "int float " + // f3b
 
175
                "complex " + // f4b
 
176
                "complex " + // f5b
 
177
                //
 
178
                "int " + // s1a
 
179
                "byte int " + // s2a
 
180
                "int float " + // s3a
 
181
                //
 
182
                "int " + // s1a
 
183
                "byte int float " + // s2a
 
184
                "float " // s3a
 
185
 
 
186
        // collect unresolved identifiers
 
187
        var buf bytes.Buffer
 
188
        for _, u := range f.Unresolved {
 
189
                buf.WriteString(u.Name)
 
190
                buf.WriteByte(' ')
 
191
        }
 
192
        got := buf.String()
 
193
 
 
194
        if got != want {
 
195
                t.Errorf("\ngot:  %s\nwant: %s", got, want)
 
196
        }
 
197
}
 
198
 
136
199
var imports = map[string]bool{
137
200
        `"a"`:        true,
138
201
        "`a`":        true,
177
240
                }
178
241
        }
179
242
}
 
243
 
 
244
func TestCommentGroups(t *testing.T) {
 
245
        f, err := ParseFile(fset, "", `
 
246
package p /* 1a */ /* 1b */      /* 1c */ // 1d
 
247
/* 2a
 
248
*/
 
249
// 2b
 
250
const pi = 3.1415
 
251
/* 3a */ // 3b
 
252
/* 3c */ const e = 2.7182
 
253
 
 
254
// Example from issue 3139
 
255
func ExampleCount() {
 
256
        fmt.Println(strings.Count("cheese", "e"))
 
257
        fmt.Println(strings.Count("five", "")) // before & after each rune
 
258
        // Output:
 
259
        // 3
 
260
        // 5
 
261
}
 
262
`, ParseComments)
 
263
        if err != nil {
 
264
                t.Fatal(err)
 
265
        }
 
266
        expected := [][]string{
 
267
                {"/* 1a */", "/* 1b */", "/* 1c */", "// 1d"},
 
268
                {"/* 2a\n*/", "// 2b"},
 
269
                {"/* 3a */", "// 3b", "/* 3c */"},
 
270
                {"// Example from issue 3139"},
 
271
                {"// before & after each rune"},
 
272
                {"// Output:", "// 3", "// 5"},
 
273
        }
 
274
        if len(f.Comments) != len(expected) {
 
275
                t.Fatalf("got %d comment groups; expected %d", len(f.Comments), len(expected))
 
276
        }
 
277
        for i, exp := range expected {
 
278
                got := f.Comments[i].List
 
279
                if len(got) != len(exp) {
 
280
                        t.Errorf("got %d comments in group %d; expected %d", len(got), i, len(exp))
 
281
                        continue
 
282
                }
 
283
                for j, exp := range exp {
 
284
                        got := got[j].Text
 
285
                        if got != exp {
 
286
                                t.Errorf("got %q in group %d; expected %q", got, i, exp)
 
287
                        }
 
288
                }
 
289
        }
 
290
}
 
291
 
 
292
func getField(file *ast.File, fieldname string) *ast.Field {
 
293
        parts := strings.Split(fieldname, ".")
 
294
        for _, d := range file.Decls {
 
295
                if d, ok := d.(*ast.GenDecl); ok && d.Tok == token.TYPE {
 
296
                        for _, s := range d.Specs {
 
297
                                if s, ok := s.(*ast.TypeSpec); ok && s.Name.Name == parts[0] {
 
298
                                        if s, ok := s.Type.(*ast.StructType); ok {
 
299
                                                for _, f := range s.Fields.List {
 
300
                                                        for _, name := range f.Names {
 
301
                                                                if name.Name == parts[1] {
 
302
                                                                        return f
 
303
                                                                }
 
304
                                                        }
 
305
                                                }
 
306
                                        }
 
307
                                }
 
308
                        }
 
309
                }
 
310
        }
 
311
        return nil
 
312
}
 
313
 
 
314
// Don't use ast.CommentGroup.Text() - we want to see exact comment text.
 
315
func commentText(c *ast.CommentGroup) string {
 
316
        var buf bytes.Buffer
 
317
        if c != nil {
 
318
                for _, c := range c.List {
 
319
                        buf.WriteString(c.Text)
 
320
                }
 
321
        }
 
322
        return buf.String()
 
323
}
 
324
 
 
325
func checkFieldComments(t *testing.T, file *ast.File, fieldname, lead, line string) {
 
326
        f := getField(file, fieldname)
 
327
        if f == nil {
 
328
                t.Fatalf("field not found: %s", fieldname)
 
329
        }
 
330
        if got := commentText(f.Doc); got != lead {
 
331
                t.Errorf("got lead comment %q; expected %q", got, lead)
 
332
        }
 
333
        if got := commentText(f.Comment); got != line {
 
334
                t.Errorf("got line comment %q; expected %q", got, line)
 
335
        }
 
336
}
 
337
 
 
338
func TestLeadAndLineComments(t *testing.T) {
 
339
        f, err := ParseFile(fset, "", `
 
340
package p
 
341
type T struct {
 
342
        /* F1 lead comment */
 
343
        //
 
344
        F1 int  /* F1 */ // line comment
 
345
        // F2 lead
 
346
        // comment
 
347
        F2 int  // F2 line comment
 
348
        // f3 lead comment
 
349
        f3 int  // f3 line comment
 
350
}
 
351
`, ParseComments)
 
352
        if err != nil {
 
353
                t.Fatal(err)
 
354
        }
 
355
        checkFieldComments(t, f, "T.F1", "/* F1 lead comment *///", "/* F1 */// line comment")
 
356
        checkFieldComments(t, f, "T.F2", "// F2 lead// comment", "// F2 line comment")
 
357
        checkFieldComments(t, f, "T.f3", "// f3 lead comment", "// f3 line comment")
 
358
        ast.FileExports(f)
 
359
        checkFieldComments(t, f, "T.F1", "/* F1 lead comment *///", "/* F1 */// line comment")
 
360
        checkFieldComments(t, f, "T.F2", "// F2 lead// comment", "// F2 line comment")
 
361
        if getField(f, "T.f3") != nil {
 
362
                t.Error("not expected to find T.f3")
 
363
        }
 
364
}