Debian Codegraph @

go/ast/astutil/enclosing_test.go

raw symbols SCIP
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_testscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/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 "bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/"
14 "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/"
15 "go/astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/"
16 "go/parserscip-go gomod github.com/golang/go/src go1.25.0 `go/parser`/"
17 "go/tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/"
18 "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/"
19 "testingscip-go gomod github.com/golang/go/src go1.25.0 testing/"
20
21 "golang.org/x/tools/go/ast/astutilscip-go gomod golang.org/x/tools . `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 pathToStringscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/astutil_test`/pathToString().(path []astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Nodescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#) string {
27 var buf bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.25.0 bytes/Buffer#
28 fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Fprintscip-go gomod github.com/golang/go/src go1.25.0 fmt/Fprint().(&buf, "[")
29 for i, n := range path {
30 if i > 0 {
31 fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Fprintscip-go gomod github.com/golang/go/src go1.25.0 fmt/Fprint().(&buf, " ")
32 }
33 fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Fprintscip-go gomod github.com/golang/go/src go1.25.0 fmt/Fprint().(&buf, stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.TrimPrefixscip-go gomod github.com/golang/go/src go1.25.0 strings/TrimPrefix().(fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprintf().("%T", n), "*ast."))
34 }
35 fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Fprintscip-go gomod github.com/golang/go/src go1.25.0 fmt/Fprint().(&buf, "]")
36 return buf.Stringscip-go gomod github.com/golang/go/src go1.25.0 bytes/Buffer#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 findIntervalscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/astutil_test`/findInterval().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#, fset *tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.FileSetscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/FileSet#, input, substr string) (f *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Filescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#, start, end tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/Pos#) {
43 f, err := parserscip-go gomod github.com/golang/go/src go1.25.0 `go/parser`/.ParseFilescip-go gomod github.com/golang/go/src go1.25.0 `go/parser`/ParseFile().(fset, "<input>", input, 0)
44 if err != nil {
45 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("parse error: %s", err)
46 return
47 }
48
49 i := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Indexscip-go gomod github.com/golang/go/src go1.25.0 strings/Index().(input, substr)
50 if i < 0 {
51 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("%q is not a substring of input", substr)
52 f = nil
53 return
54 }
55
56 filePos := fset.Filescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/FileSet#File().(f.Packagescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#Package.)
57 return f, filePos.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/File#Pos().(i), filePos.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/File#Pos().(i + len(substr))
58 }
59
60 // Common input for following tests.
61 const inputscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/astutil_test`/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_Exactscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/astutil_test`/TestPathEnclosingInterval_Exact().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 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 inputscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/astutil_test`/input.[11 : len(inputscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/astutil_test`/input.)-1]},
94 {"\npack",
95 inputscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/astutil_test`/input.[11 : len(inputscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/astutil_test`/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 := findIntervalscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/astutil_test`/findInterval().(t, new(tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.FileSetscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/FileSet#), inputscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/astutil_test`/input., test.substr)
148 if f == nil {
149 continue
150 }
151
152 path, exact := astutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/astutil`/.PathEnclosingIntervalscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/astutil`/PathEnclosingInterval().(f, start, end)
153 if !exact {
154 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("PathEnclosingInterval(%q) not exact", test.substr)
155 continue
156 }
157
158 if len(path) == 0 {
159 if test.node != "" {
160 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("PathEnclosingInterval(%q).path: got [], want %q",
161 test.substr, test.node)
162 }
163 continue
164 }
165
166 if got := inputscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/astutil_test`/input.[path[0].Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#Pos().():path[0].Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#End().()]; got != test.node {
167 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("PathEnclosingInterval(%q): got %q, want %q (path was %s)",
168 test.substr, got, test.node, pathToStringscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/astutil_test`/pathToString().(path))
169 continue
170 }
171 }
172 }
173
174 func TestPathEnclosingInterval_Pathsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/astutil_test`/TestPathEnclosingInterval_Paths().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 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 := findIntervalscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/astutil_test`/findInterval().(t, new(tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.FileSetscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/FileSet#), inputscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/astutil_test`/input., test.substr)
222 if f == nil {
223 continue
224 }
225
226 path, exact := astutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/astutil`/.PathEnclosingIntervalscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/astutil`/PathEnclosingInterval().(f, start, end)
227 if got := fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprintf().("%s,%v", pathToStringscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/astutil_test`/pathToString().(path), exact); got != test.path {
228 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("PathEnclosingInterval(%q): got %q, want %q",
229 test.substr, got, test.path)
230 continue
231 }
232 }
233 }