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