GoPLS Viewer

Home|gopls/go/packages/packagestest/expect_test.go
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
5package packagestest_test
6
7import (
8    "go/token"
9    "testing"
10
11    "golang.org/x/tools/go/expect"
12    "golang.org/x/tools/go/packages/packagestest"
13)
14
15func TestExpect(t *testing.T) {
16    exported := packagestest.Export(tpackagestest.GOPATH, []packagestest.Module{{
17        Name:  "golang.org/fake",
18        Filespackagestest.MustCopyFileTree("testdata"),
19    }})
20    defer exported.Cleanup()
21    checkCount := 0
22    if err := exported.Expect(map[string]interface{}{
23        "check": func(srctarget token.Position) {
24            checkCount++
25        },
26        "boolArg": func(n *expect.Noteyesno 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.Notei int64) {
35            if i != 42 {
36                t.Errorf("Expected intarg to be 42")
37            }
38        },
39        "stringArg": func(n *expect.Notename expect.Identifiervalue string) {
40            if string(name) != value {
41                t.Errorf("Got string arg %v expected %v"valuename)
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.Notep 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"wantCheckcheckCount)
70    }
71}
72
MembersX
testing
packagestest
TestExpect
TestExpect.t
TestExpect.exported
TestExpect.checkCount
TestExpect.err
TestExpect.wantCheck
Members
X