| 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 expect_test |
| 6 |
|
| 7 |
import ( |
| 8 |
"bytes" |
| 9 |
"go/token" |
| 10 |
"os" |
| 11 |
"reflect" |
| 12 |
"slices" |
| 13 |
"testing" |
| 14 |
|
| 15 |
"honnef.co/go/tools/internal/xtools-internal/expect" |
| 16 |
) |
| 17 |
|
| 18 |
func TestMarker(t *testing.T) { |
| 19 |
for _, tt := range []struct { |
| 20 |
filename string |
| 21 |
expectNotes int |
| 22 |
expectMarkers map[string]string |
| 23 |
expectChecks map[string][]any |
| 24 |
// expectChecks holds {"id": values} for each call check(id, values...). |
| 25 |
// Any named k=v arguments become a final map[string]any argument. |
| 26 |
}{ |
| 27 |
{ |
| 28 |
filename: "testdata/test.go", |
| 29 |
expectNotes: 14, |
| 30 |
expectMarkers: map[string]string{ |
| 31 |
"αSimpleMarker": "α", |
| 32 |
"OffsetMarker": "β", |
| 33 |
"RegexMarker": "γ", |
| 34 |
"εMultiple": "ε", |
| 35 |
"ζMarkers": "ζ", |
| 36 |
"ηBlockMarker": "η", |
| 37 |
"Declared": "η", |
| 38 |
"Comment": "ι", |
| 39 |
"LineComment": "someFunc", |
| 40 |
"NonIdentifier": "+", |
| 41 |
"StringMarker": "\"hello\"", |
| 42 |
}, |
| 43 |
expectChecks: map[string][]any{ |
| 44 |
"αSimpleMarker": nil, |
| 45 |
"StringAndInt": {"Number %d", int64(12)}, |
| 46 |
"Bool": {true}, |
| 47 |
"NamedArgs": {int64(1), true, expect.Identifier("a"), map[string]any{ |
| 48 |
"b": int64(1), |
| 49 |
"c": "3", |
| 50 |
"d": true, |
| 51 |
}}, |
| 52 |
}, |
| 53 |
}, |
| 54 |
{ |
| 55 |
filename: "testdata/go.fake.mod", |
| 56 |
expectNotes: 2, |
| 57 |
expectMarkers: map[string]string{ |
| 58 |
"αMarker": "αfake1α", |
| 59 |
"βMarker": "require golang.org/modfile v0.0.0", |
| 60 |
}, |
| 61 |
}, |
| 62 |
{ |
| 63 |
filename: "testdata/go.fake.work", |
| 64 |
expectNotes: 2, |
| 65 |
expectMarkers: map[string]string{ |
| 66 |
"αMarker": "1.23.0", |
| 67 |
"βMarker": "αβ", |
| 68 |
}, |
| 69 |
}, |
| 70 |
} { |
| 71 |
t.Run(tt.filename, func(t *testing.T) { |
| 72 |
content, err := os.ReadFile(tt.filename) |
| 73 |
if err != nil { |
| 74 |
t.Fatal(err) |
| 75 |
} |
| 76 |
readFile := func(string) ([]byte, error) { return content, nil } |
| 77 |
|
| 78 |
markers := make(map[string]token.Pos) |
| 79 |
for name, tok := range tt.expectMarkers { |
| 80 |
offset := bytes.Index(content, []byte(tok)) |
| 81 |
markers[name] = token.Pos(offset + 1) |
| 82 |
end := bytes.Index(content[offset:], []byte(tok)) |
| 83 |
if end > 0 { |
| 84 |
markers[name+"@"] = token.Pos(offset + end + 2) |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
fset := token.NewFileSet() |
| 89 |
notes, err := expect.Parse(fset, tt.filename, content) |
| 90 |
if err != nil { |
| 91 |
t.Fatalf("Failed to extract notes:\n%v", err) |
| 92 |
} |
| 93 |
if len(notes) != tt.expectNotes { |
| 94 |
t.Errorf("Expected %v notes, got %v", tt.expectNotes, len(notes)) |
| 95 |
} |
| 96 |
for _, n := range notes { |
| 97 |
switch { |
| 98 |
case n.Args == nil: |
| 99 |
// A //@foo note associates the name foo with the position of the |
| 100 |
// first match of "foo" on the current line. |
| 101 |
checkMarker(t, fset, readFile, markers, n.Pos, n.Name, n.Name) |
| 102 |
case n.Name == "mark": |
| 103 |
// A //@mark(name, "pattern") note associates the specified name |
| 104 |
// with the position on the first match of pattern on the current line. |
| 105 |
if len(n.Args) != 2 { |
| 106 |
t.Errorf("%v: expected 2 args to mark, got %v", fset.Position(n.Pos), len(n.Args)) |
| 107 |
continue |
| 108 |
} |
| 109 |
ident, ok := n.Args[0].(expect.Identifier) |
| 110 |
if !ok { |
| 111 |
t.Errorf("%v: got %v (%T), want identifier", fset.Position(n.Pos), n.Args[0], n.Args[0]) |
| 112 |
continue |
| 113 |
} |
| 114 |
checkMarker(t, fset, readFile, markers, n.Pos, string(ident), n.Args[1]) |
| 115 |
|
| 116 |
case n.Name == "check": |
| 117 |
// A //@check(args, ...) note specifies some hypothetical action to |
| 118 |
// be taken by the test driver and its expected outcome. |
| 119 |
// In this test, the action is to compare the arguments |
| 120 |
// against expectChecks. |
| 121 |
if len(n.Args) < 1 { |
| 122 |
t.Errorf("%v: expected 1 args to check, got %v", fset.Position(n.Pos), len(n.Args)) |
| 123 |
continue |
| 124 |
} |
| 125 |
ident, ok := n.Args[0].(expect.Identifier) |
| 126 |
if !ok { |
| 127 |
t.Errorf("%v: got %v (%T), want identifier", fset.Position(n.Pos), n.Args[0], n.Args[0]) |
| 128 |
continue |
| 129 |
} |
| 130 |
wantArgs, ok := tt.expectChecks[string(ident)] |
| 131 |
if !ok { |
| 132 |
t.Errorf("%v: unexpected check %v", fset.Position(n.Pos), ident) |
| 133 |
continue |
| 134 |
} |
| 135 |
gotArgs := n.Args[1:] |
| 136 |
if n.NamedArgs != nil { |
| 137 |
// Clip to avoid mutating Args' array. |
| 138 |
gotArgs = append(slices.Clip(gotArgs), n.NamedArgs) |
| 139 |
} |
| 140 |
|
| 141 |
if len(gotArgs) != len(wantArgs) { |
| 142 |
t.Errorf("%v: expected %v args to check, got %v", fset.Position(n.Pos), len(wantArgs), len(gotArgs)) |
| 143 |
continue |
| 144 |
} |
| 145 |
for i := range gotArgs { |
| 146 |
if !reflect.DeepEqual(wantArgs[i], gotArgs[i]) { |
| 147 |
t.Errorf("%v: arg %d: expected %#v, got %#v", fset.Position(n.Pos), i+1, wantArgs[i], gotArgs[i]) |
| 148 |
} |
| 149 |
} |
| 150 |
default: |
| 151 |
t.Errorf("Unexpected note %v at %v", n.Name, fset.Position(n.Pos)) |
| 152 |
} |
| 153 |
} |
| 154 |
}) |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
func checkMarker(t *testing.T, fset *token.FileSet, readFile expect.ReadFile, markers map[string]token.Pos, pos token.Pos, name string, pattern any) { |
| 159 |
start, end, err := expect.MatchBefore(fset.File(pos), readFile, pos, pattern) |
| 160 |
if err != nil { |
| 161 |
t.Errorf("%v: MatchBefore failed: %v", fset.Position(pos), err) |
| 162 |
return |
| 163 |
} |
| 164 |
if start == token.NoPos { |
| 165 |
t.Errorf("%v: Pattern %v did not match", fset.Position(pos), pattern) |
| 166 |
return |
| 167 |
} |
| 168 |
expectStart, ok := markers[name] |
| 169 |
if !ok { |
| 170 |
t.Errorf("%v: unexpected marker %v", fset.Position(pos), name) |
| 171 |
return |
| 172 |
} |
| 173 |
if start != expectStart { |
| 174 |
t.Errorf("%v: Expected %v got %v", fset.Position(pos), fset.Position(expectStart), fset.Position(start)) |
| 175 |
} |
| 176 |
if expectEnd, ok := markers[name+"@"]; ok && end != expectEnd { |
| 177 |
t.Errorf("%v: Expected end %v got %v", fset.Position(pos), fset.Position(expectEnd), fset.Position(end)) |
| 178 |
} |
| 179 |
} |