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