| 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 |
"honnef.co/go/tools/internal/xtools-internal/astutil" |
| 12 |
"honnef.co/go/tools/internal/xtools-internal/expect" |
| 13 |
"honnef.co/go/tools/internal/xtools-internal/packagestest" |
| 14 |
) |
| 15 |
|
| 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"), |
| 20 |
}}) |
| 21 |
defer exported.Cleanup() |
| 22 |
checkCount := 0 |
| 23 |
if err := exported.Expect(map[string]any{ |
| 24 |
"check": func(src, target token.Position) { |
| 25 |
checkCount++ |
| 26 |
}, |
| 27 |
"boolArg": func(n *expect.Note, yes, no bool) { |
| 28 |
if !yes { |
| 29 |
t.Errorf("Expected boolArg first param to be true") |
| 30 |
} |
| 31 |
if no { |
| 32 |
t.Errorf("Expected boolArg second param to be false") |
| 33 |
} |
| 34 |
}, |
| 35 |
"intArg": func(n *expect.Note, i int64) { |
| 36 |
if i != 42 { |
| 37 |
t.Errorf("Expected intarg to be 42") |
| 38 |
} |
| 39 |
}, |
| 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) |
| 43 |
} |
| 44 |
}, |
| 45 |
"directNote": func(n *expect.Note) {}, |
| 46 |
"range": func(r astutil.Range) { |
| 47 |
if r.Start == token.NoPos { |
| 48 |
t.Errorf("Range had no valid starting position") |
| 49 |
} |
| 50 |
if r.EndPos == token.NoPos { |
| 51 |
t.Errorf("Range had no valid ending position") |
| 52 |
} else if r.EndPos <= r.Start { |
| 53 |
t.Errorf("Range ending was not greater than start") |
| 54 |
} |
| 55 |
}, |
| 56 |
"checkEOF": func(n *expect.Note, p token.Pos) { |
| 57 |
if p <= n.Pos { |
| 58 |
t.Errorf("EOF was before the checkEOF note") |
| 59 |
} |
| 60 |
}, |
| 61 |
}); err != nil { |
| 62 |
t.Fatal(err) |
| 63 |
} |
| 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) |
| 68 |
wantCheck := 7 |
| 69 |
if wantCheck != checkCount { |
| 70 |
t.Fatalf("Expected @check count of %v; got %v", wantCheck, checkCount) |
| 71 |
} |
| 72 |
} |