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.
13 "golang.org/x/tools/go/expect"
16 func TestMarker(t *testing.T) {
17 for _, tt := range []struct {
20 expectMarkers map[string]string
21 expectChecks map[string][]interface{}
24 filename: "testdata/test.go",
26 expectMarkers: map[string]string{
35 "LineComment": "someFunc",
37 "StringMarker": "\"hello\"",
39 expectChecks: map[string][]interface{}{
41 "StringAndInt": {"Number %d", int64(12)},
46 filename: "testdata/go.mod",
48 expectMarkers: map[string]string{
50 "βMarker": "require golang.org/modfile v0.0.0",
54 t.Run(tt.filename, func(t *testing.T) {
55 content, err := ioutil.ReadFile(tt.filename)
59 readFile := func(string) ([]byte, error) { return content, nil }
61 markers := make(map[string]token.Pos)
62 for name, tok := range tt.expectMarkers {
63 offset := bytes.Index(content, []byte(tok))
64 markers[name] = token.Pos(offset + 1)
65 end := bytes.Index(content[offset:], []byte(tok))
67 markers[name+"@"] = token.Pos(offset + end + 2)
71 fset := token.NewFileSet()
72 notes, err := expect.Parse(fset, tt.filename, content)
74 t.Fatalf("Failed to extract notes: %v", err)
76 if len(notes) != tt.expectNotes {
77 t.Errorf("Expected %v notes, got %v", tt.expectNotes, len(notes))
79 for _, n := range notes {
82 // A //@foo note associates the name foo with the position of the
83 // first match of "foo" on the current line.
84 checkMarker(t, fset, readFile, markers, n.Pos, n.Name, n.Name)
85 case n.Name == "mark":
86 // A //@mark(name, "pattern") note associates the specified name
87 // with the position on the first match of pattern on the current line.
89 t.Errorf("%v: expected 2 args to mark, got %v", fset.Position(n.Pos), len(n.Args))
92 ident, ok := n.Args[0].(expect.Identifier)
94 t.Errorf("%v: identifier, got %T", fset.Position(n.Pos), n.Args[0])
97 checkMarker(t, fset, readFile, markers, n.Pos, string(ident), n.Args[1])
99 case n.Name == "check":
100 // A //@check(args, ...) note specifies some hypothetical action to
101 // be taken by the test driver and its expected outcome.
102 // In this test, the action is to compare the arguments
103 // against expectChecks.
105 t.Errorf("%v: expected 1 args to check, got %v", fset.Position(n.Pos), len(n.Args))
108 ident, ok := n.Args[0].(expect.Identifier)
110 t.Errorf("%v: identifier, got %T", fset.Position(n.Pos), n.Args[0])
113 args, ok := tt.expectChecks[string(ident)]
115 t.Errorf("%v: unexpected check %v", fset.Position(n.Pos), ident)
118 if len(n.Args) != len(args)+1 {
119 t.Errorf("%v: expected %v args to check, got %v", fset.Position(n.Pos), len(args)+1, len(n.Args))
122 for i, got := range n.Args[1:] {
124 t.Errorf("%v: arg %d expected %v, got %v", fset.Position(n.Pos), i, args[i], got)
128 t.Errorf("Unexpected note %v at %v", n.Name, fset.Position(n.Pos))
135 func checkMarker(t *testing.T, fset *token.FileSet, readFile expect.ReadFile, markers map[string]token.Pos, pos token.Pos, name string, pattern interface{}) {
136 start, end, err := expect.MatchBefore(fset, readFile, pos, pattern)
138 t.Errorf("%v: MatchBefore failed: %v", fset.Position(pos), err)
141 if start == token.NoPos {
142 t.Errorf("%v: Pattern %v did not match", fset.Position(pos), pattern)
145 expectStart, ok := markers[name]
147 t.Errorf("%v: unexpected marker %v", fset.Position(pos), name)
150 if start != expectStart {
151 t.Errorf("%v: Expected %v got %v", fset.Position(pos), fset.Position(expectStart), fset.Position(start))
153 if expectEnd, ok := markers[name+"@"]; ok && end != expectEnd {
154 t.Errorf("%v: Expected end %v got %v", fset.Position(pos), fset.Position(expectEnd), fset.Position(end))