| 1 | // Copyright 2018 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 packagestest_test |
| 6 | |
| 7 | import ( |
| 8 | "go/token" |
| 9 | "testing" |
| 10 | |
| 11 | "golang.org/x/tools/go/expect" |
| 12 | "golang.org/x/tools/go/packages/packagestest" |
| 13 | ) |
| 14 | |
| 15 | func TestExpect(t *testing.T) { |
| 16 | exported := packagestest.Export(t, packagestest.GOPATH, []packagestest.Module{{ |
| 17 | Name: "golang.org/fake", |
| 18 | Files: packagestest.MustCopyFileTree("testdata"), |
| 19 | }}) |
| 20 | defer exported.Cleanup() |
| 21 | checkCount := 0 |
| 22 | if err := exported.Expect(map[string]interface{}{ |
| 23 | "check": func(src, target token.Position) { |
| 24 | checkCount++ |
| 25 | }, |
| 26 | "boolArg": func(n *expect.Note, yes, no bool) { |
| 27 | if !yes { |
| 28 | t.Errorf("Expected boolArg first param to be true") |
| 29 | } |
| 30 | if no { |
| 31 | t.Errorf("Expected boolArg second param to be false") |
| 32 | } |
| 33 | }, |
| 34 | "intArg": func(n *expect.Note, i int64) { |
| 35 | if i != 42 { |
| 36 | t.Errorf("Expected intarg to be 42") |
| 37 | } |
| 38 | }, |
| 39 | "stringArg": func(n *expect.Note, name expect.Identifier, value string) { |
| 40 | if string(name) != value { |
| 41 | t.Errorf("Got string arg %v expected %v", value, name) |
| 42 | } |
| 43 | }, |
| 44 | "directNote": func(n *expect.Note) {}, |
| 45 | "range": func(r packagestest.Range) { |
| 46 | if r.Start == token.NoPos || r.Start == 0 { |
| 47 | t.Errorf("Range had no valid starting position") |
| 48 | } |
| 49 | if r.End == token.NoPos || r.End == 0 { |
| 50 | t.Errorf("Range had no valid ending position") |
| 51 | } else if r.End <= r.Start { |
| 52 | t.Errorf("Range ending was not greater than start") |
| 53 | } |
| 54 | }, |
| 55 | "checkEOF": func(n *expect.Note, p token.Pos) { |
| 56 | if p <= n.Pos { |
| 57 | t.Errorf("EOF was before the checkEOF note") |
| 58 | } |
| 59 | }, |
| 60 | }); err != nil { |
| 61 | t.Fatal(err) |
| 62 | } |
| 63 | // We expect to have walked the @check annotations in all .go files, |
| 64 | // including _test.go files (XTest or otherwise). But to have walked the |
| 65 | // non-_test.go files only once. Hence wantCheck = 3 (testdata/test.go) + 1 |
| 66 | // (testdata/test_test.go) + 1 (testdata/x_test.go) |
| 67 | wantCheck := 7 |
| 68 | if wantCheck != checkCount { |
| 69 | t.Fatalf("Expected @check count of %v; got %v", wantCheck, checkCount) |
| 70 | } |
| 71 | } |
| 72 |
Members