| 1 | // Copyright 2013 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 astutil_test |
| 6 | |
| 7 | // This file defines tests of PathEnclosingInterval. |
| 8 | |
| 9 | // TODO(adonovan): exhaustive tests that run over the whole input |
| 10 | // tree, not just handcrafted examples. |
| 11 | |
| 12 | import ( |
| 13 | "bytes" |
| 14 | "fmt" |
| 15 | "go/ast" |
| 16 | "go/parser" |
| 17 | "go/token" |
| 18 | "strings" |
| 19 | "testing" |
| 20 | |
| 21 | "golang.org/x/tools/go/ast/astutil" |
| 22 | ) |
| 23 | |
| 24 | // pathToString returns a string containing the concrete types of the |
| 25 | // nodes in path. |
| 26 | func pathToString(path []ast.Node) string { |
| 27 | var buf bytes.Buffer |
| 28 | fmt.Fprint(&buf, "[") |
| 29 | for i, n := range path { |
| 30 | if i > 0 { |
| 31 | fmt.Fprint(&buf, " ") |
| 32 | } |
| 33 | fmt.Fprint(&buf, strings.TrimPrefix(fmt.Sprintf("%T", n), "*ast.")) |
| 34 | } |
| 35 | fmt.Fprint(&buf, "]") |
| 36 | return buf.String() |
| 37 | } |
| 38 | |
| 39 | // findInterval parses input and returns the [start, end) positions of |
| 40 | // the first occurrence of substr in input. f==nil indicates failure; |
| 41 | // an error has already been reported in that case. |
| 42 | func findInterval(t *testing.T, fset *token.FileSet, input, substr string) (f *ast.File, start, end token.Pos) { |
| 43 | f, err := parser.ParseFile(fset, "<input>", input, 0) |
| 44 | if err != nil { |
| 45 | t.Errorf("parse error: %s", err) |
| 46 | return |
| 47 | } |
| 48 | |
| 49 | i := strings.Index(input, substr) |
| 50 | if i < 0 { |
| 51 | t.Errorf("%q is not a substring of input", substr) |
| 52 | f = nil |
| 53 | return |
| 54 | } |
| 55 | |
| 56 | filePos := fset.File(f.Package) |
| 57 | return f, filePos.Pos(i), filePos.Pos(i + len(substr)) |
| 58 | } |
| 59 | |
| 60 | // Common input for following tests. |
| 61 | const input = ` |
| 62 | // Hello. |
| 63 | package main |
| 64 | import "fmt" |
| 65 | func f() {} |
| 66 | func main() { |
| 67 | z := (x + y) // add them |
| 68 | f() // NB: ExprStmt and its CallExpr have same Pos/End |
| 69 | } |
| 70 | |
| 71 | func g[A any, P interface{ctype1| ~ctype2}](a1 A, p1 P) {} |
| 72 | |
| 73 | type PT[T constraint] struct{ t T } |
| 74 | |
| 75 | func (r recv) method(p param) {} |
| 76 | |
| 77 | var v GT[targ1] |
| 78 | |
| 79 | var h = g[ targ2, targ3] |
| 80 | ` |
| 81 | |
| 82 | func TestPathEnclosingInterval_Exact(t *testing.T) { |
| 83 | type testCase struct { |
| 84 | substr string // first occurrence of this string indicates interval |
| 85 | node string // complete text of expected containing node |
| 86 | } |
| 87 | |
| 88 | dup := func(s string) testCase { return testCase{s, s} } |
| 89 | // For the exact tests, we check that a substring is mapped to |
| 90 | // the canonical string for the node it denotes. |
| 91 | tests := []testCase{ |
| 92 | {"package", |
| 93 | input[11 : len(input)-1]}, |
| 94 | {"\npack", |
| 95 | input[11 : len(input)-1]}, |
| 96 | dup("main"), |
| 97 | {"import", |
| 98 | "import \"fmt\""}, |
| 99 | dup("\"fmt\""), |
| 100 | {"\nfunc f() {}\n", |
| 101 | "func f() {}"}, |
| 102 | {"x ", |
| 103 | "x"}, |
| 104 | {" y", |
| 105 | "y"}, |
| 106 | dup("z"), |
| 107 | {" + ", |
| 108 | "x + y"}, |
| 109 | {" :=", |
| 110 | "z := (x + y)"}, |
| 111 | dup("x + y"), |
| 112 | dup("(x + y)"), |
| 113 | {" (x + y) ", |
| 114 | "(x + y)"}, |
| 115 | {" (x + y) // add", |
| 116 | "(x + y)"}, |
| 117 | {"func", |
| 118 | "func f() {}"}, |
| 119 | dup("func f() {}"), |
| 120 | {"\nfun", |
| 121 | "func f() {}"}, |
| 122 | {" f", |
| 123 | "f"}, |
| 124 | dup("[A any, P interface{ctype1| ~ctype2}]"), |
| 125 | {"[", "[A any, P interface{ctype1| ~ctype2}]"}, |
| 126 | dup("A"), |
| 127 | {" any", "any"}, |
| 128 | dup("ctype1"), |
| 129 | {"|", "ctype1| ~ctype2"}, |
| 130 | dup("ctype2"), |
| 131 | {"~", "~ctype2"}, |
| 132 | dup("~ctype2"), |
| 133 | {" ~ctype2", "~ctype2"}, |
| 134 | {"]", "[A any, P interface{ctype1| ~ctype2}]"}, |
| 135 | dup("a1"), |
| 136 | dup("a1 A"), |
| 137 | dup("(a1 A, p1 P)"), |
| 138 | dup("type PT[T constraint] struct{ t T }"), |
| 139 | dup("PT"), |
| 140 | dup("[T constraint]"), |
| 141 | dup("constraint"), |
| 142 | dup("targ1"), |
| 143 | {" targ2", "targ2"}, |
| 144 | dup("g[ targ2, targ3]"), |
| 145 | } |
| 146 | for _, test := range tests { |
| 147 | f, start, end := findInterval(t, new(token.FileSet), input, test.substr) |
| 148 | if f == nil { |
| 149 | continue |
| 150 | } |
| 151 | |
| 152 | path, exact := astutil.PathEnclosingInterval(f, start, end) |
| 153 | if !exact { |
| 154 | t.Errorf("PathEnclosingInterval(%q) not exact", test.substr) |
| 155 | continue |
| 156 | } |
| 157 | |
| 158 | if len(path) == 0 { |
| 159 | if test.node != "" { |
| 160 | t.Errorf("PathEnclosingInterval(%q).path: got [], want %q", |
| 161 | test.substr, test.node) |
| 162 | } |
| 163 | continue |
| 164 | } |
| 165 | |
| 166 | if got := input[path[0].Pos():path[0].End()]; got != test.node { |
| 167 | t.Errorf("PathEnclosingInterval(%q): got %q, want %q (path was %s)", |
| 168 | test.substr, got, test.node, pathToString(path)) |
| 169 | continue |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | func TestPathEnclosingInterval_Paths(t *testing.T) { |
| 175 | type testCase struct { |
| 176 | substr string // first occurrence of this string indicates interval |
| 177 | path string // the pathToString(),exact of the expected path |
| 178 | } |
| 179 | // For these tests, we check only the path of the enclosing |
| 180 | // node, but not its complete text because it's often quite |
| 181 | // large when !exact. |
| 182 | tests := []testCase{ |
| 183 | {"// add", |
| 184 | "[BlockStmt FuncDecl File],false"}, |
| 185 | {"(x + y", |
| 186 | "[ParenExpr AssignStmt BlockStmt FuncDecl File],false"}, |
| 187 | {"x +", |
| 188 | "[BinaryExpr ParenExpr AssignStmt BlockStmt FuncDecl File],false"}, |
| 189 | {"z := (x", |
| 190 | "[AssignStmt BlockStmt FuncDecl File],false"}, |
| 191 | {"func f", |
| 192 | "[FuncDecl File],false"}, |
| 193 | {"func f()", |
| 194 | "[FuncDecl File],false"}, |
| 195 | {" f()", |
| 196 | "[FuncDecl File],false"}, |
| 197 | {"() {}", |
| 198 | "[FuncDecl File],false"}, |
| 199 | {"// Hello", |
| 200 | "[File],false"}, |
| 201 | {" f", |
| 202 | "[Ident FuncDecl File],true"}, |
| 203 | {"func ", |
| 204 | "[FuncDecl File],true"}, |
| 205 | {"mai", |
| 206 | "[Ident File],true"}, |
| 207 | {"f() // NB", |
| 208 | "[CallExpr ExprStmt BlockStmt FuncDecl File],true"}, |
| 209 | {" any", "[Ident Field FieldList FuncType FuncDecl File],true"}, |
| 210 | {"|", "[BinaryExpr Field FieldList InterfaceType Field FieldList FuncType FuncDecl File],true"}, |
| 211 | {"ctype2", |
| 212 | "[Ident UnaryExpr BinaryExpr Field FieldList InterfaceType Field FieldList FuncType FuncDecl File],true"}, |
| 213 | {"a1", "[Ident Field FieldList FuncType FuncDecl File],true"}, |
| 214 | {"PT[T constraint]", "[TypeSpec GenDecl File],false"}, |
| 215 | {"[T constraint]", "[FieldList TypeSpec GenDecl File],true"}, |
| 216 | {"targ2", "[Ident IndexListExpr ValueSpec GenDecl File],true"}, |
| 217 | {"p param", "[Field FieldList FuncType FuncDecl File],true"}, // FuncType is present for FuncDecl.Params (etc) |
| 218 | {"r recv", "[Field FieldList FuncDecl File],true"}, // no FuncType for FuncDecl.Recv |
| 219 | } |
| 220 | for _, test := range tests { |
| 221 | f, start, end := findInterval(t, new(token.FileSet), input, test.substr) |
| 222 | if f == nil { |
| 223 | continue |
| 224 | } |
| 225 | |
| 226 | path, exact := astutil.PathEnclosingInterval(f, start, end) |
| 227 | if got := fmt.Sprintf("%s,%v", pathToString(path), exact); got != test.path { |
| 228 | t.Errorf("PathEnclosingInterval(%q): got %q, want %q", |
| 229 | test.substr, got, test.path) |
| 230 | continue |
| 231 | } |
| 232 | } |
| 233 | } |