| 1 | // Copyright 2024 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 inspector_testscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector_test`/ |
| 6 | |
| 7 | import ( |
| 8 | "go/astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/" |
| 9 | "iterscip-go gomod github.com/golang/go/src go1.25.0 iter/" |
| 10 | "slicesscip-go gomod github.com/golang/go/src go1.25.0 slices/" |
| 11 | "testingscip-go gomod github.com/golang/go/src go1.25.0 testing/" |
| 12 | |
| 13 | "golang.org/x/tools/go/ast/inspectorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/" |
| 14 | ) |
| 15 | |
| 16 | // TestPreorderSeq checks PreorderSeq against Preorder. |
| 17 | func TestPreorderSeqscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector_test`/TestPreorderSeq().(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#) { |
| 18 | inspect := inspectorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/.Newscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/New().(netFilesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector_test`/netFiles.) |
| 19 | |
| 20 | nodeFilter := []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#{(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.FuncDeclscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#)(nil), (*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.FuncLitscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncLit#)(nil)} |
| 21 | |
| 22 | // reference implementation |
| 23 | var want []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# |
| 24 | inspect.Preorderscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Inspector#Preorder().(nodeFilter, func(n 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#) { |
| 25 | want = append(want, n) |
| 26 | }) |
| 27 | |
| 28 | // Check entire sequence. |
| 29 | got := slicesscip-go gomod github.com/golang/go/src go1.25.0 slices/.Collectscip-go gomod github.com/golang/go/src go1.25.0 slices/Collect().(inspect.PreorderSeqscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Inspector#PreorderSeq().(nodeFilter...)) |
| 30 | comparescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector_test`/compare().(t, got, want) |
| 31 | |
| 32 | // Check that break works. |
| 33 | got = firstNscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector_test`/firstN().(10, inspect.PreorderSeqscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Inspector#PreorderSeq().(nodeFilter...)) |
| 34 | comparescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector_test`/compare().(t, got, want[:10]) |
| 35 | } |
| 36 | |
| 37 | // TestAll checks All against Preorder. |
| 38 | func TestAllscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector_test`/TestAll().(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#) { |
| 39 | inspect := inspectorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/.Newscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/New().(netFilesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector_test`/netFiles.) |
| 40 | |
| 41 | // reference implementation |
| 42 | var want []*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.CallExprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CallExpr# |
| 43 | inspect.Preorderscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Inspector#Preorder().([]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#{(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.CallExprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CallExpr#)(nil)}, func(n 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#) { |
| 44 | want = append(want, n.(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.CallExprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CallExpr#)) |
| 45 | }) |
| 46 | |
| 47 | // Check entire sequence. |
| 48 | got := slicesscip-go gomod github.com/golang/go/src go1.25.0 slices/.Collectscip-go gomod github.com/golang/go/src go1.25.0 slices/Collect().(inspectorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/.Allscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/All().[*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.CallExprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CallExpr#](inspect)) |
| 49 | comparescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector_test`/compare().(t, got, want) |
| 50 | |
| 51 | // Check that break works. |
| 52 | got = firstNscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector_test`/firstN().(10, inspectorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/.Allscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/All().[*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.CallExprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CallExpr#](inspect)) |
| 53 | comparescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector_test`/compare().(t, got, want[:10]) |
| 54 | } |
| 55 | |
| 56 | // firstN(n, seq), returns a slice of up to n elements of seq. |
| 57 | func firstNscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector_test`/firstN().[T any](n int, seq iterscip-go gomod github.com/golang/go/src go1.25.0 iter/.Seqscip-go gomod github.com/golang/go/src go1.25.0 iter/Seq#[T]) (res []T) { |
| 58 | for x := range seq { |
| 59 | res = append(res, x) |
| 60 | if len(res) == n { |
| 61 | break |
| 62 | } |
| 63 | } |
| 64 | return res |
| 65 | } |
| 66 | |
| 67 | // BenchmarkAllCalls is like BenchmarkInspectCalls, |
| 68 | // but using the single-type filtering iterator, All. |
| 69 | // (The iterator adds about 5-15%.) |
| 70 | func BenchmarkAllCallsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector_test`/BenchmarkAllCalls().(b *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Bscip-go gomod github.com/golang/go/src go1.25.0 testing/B#) { |
| 71 | inspect := inspectorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/.Newscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/New().(netFilesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector_test`/netFiles.) |
| 72 | |
| 73 | // Measure marginal cost of traversal. |
| 74 | var ncalls int |
| 75 | for b.Loopscip-go gomod github.com/golang/go/src go1.25.0 testing/B#Loop().() { |
| 76 | for range inspectorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/.Allscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/All().[*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.CallExprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CallExpr#](inspect) { |
| 77 | ncalls++ |
| 78 | } |
| 79 | } |
| 80 | } |