| 1 | // Copyright 2026 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/internal/astutil_test`/ |
| 6 | |
| 7 | import ( |
| 8 | "go/astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/" |
| 9 | "go/parserscip-go gomod github.com/golang/go/src go1.25.0 `go/parser`/" |
| 10 | "go/tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/" |
| 11 | "strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/" |
| 12 | "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/" |
| 13 | "testingscip-go gomod github.com/golang/go/src go1.25.0 testing/" |
| 14 | |
| 15 | "golang.org/x/tools/internal/astutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/" |
| 16 | "golang.org/x/tools/internal/testenvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/" |
| 17 | ) |
| 18 | |
| 19 | func TestPosInStringLiteralscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil_test`/TestPosInStringLiteral().(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#) { |
| 20 | // Each string is Go source for a string literal with ^ marker at expected Pos. |
| 21 | tests := []string{ |
| 22 | `"^abc"`, |
| 23 | `"a^bc"`, |
| 24 | `"ab^c"`, |
| 25 | `"abc^"`, |
| 26 | `"a\n^b"`, |
| 27 | `"\n^"`, |
| 28 | `"a\000^"`, |
| 29 | `"\x61^"`, |
| 30 | `"\u0061^"`, |
| 31 | `"\U00000061^"`, |
| 32 | `"€^"`, |
| 33 | `"a€^b"`, |
| 34 | "`abc^`", |
| 35 | "`a\n^b`", |
| 36 | // normalization of \r carriage returns: |
| 37 | "`a\r\n^b`", |
| 38 | "`a\r\nb\r\nc\r\n^d`", |
| 39 | } |
| 40 | for _, test := range tests { |
| 41 | // The workaround for \r requires the go1.26 fix for https://go.dev/issue/76031. |
| 42 | if stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Containsscip-go gomod github.com/golang/go/src go1.25.0 strings/Contains().(test, "\r") && testenvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/.Go1Pointscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/Go1Point().() < 26 { |
| 43 | continue |
| 44 | } |
| 45 | |
| 46 | t.Logfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Logf().("input: %#q", test) |
| 47 | |
| 48 | // Parse. |
| 49 | const prefix = "package p; const _ = " |
| 50 | src := prefix + test |
| 51 | fset := tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.NewFileSetscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/NewFileSet().() |
| 52 | 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, "p.go", src, 0) |
| 53 | if err != nil { |
| 54 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Parse: %v", err) |
| 55 | continue |
| 56 | } |
| 57 | |
| 58 | // Find literal. |
| 59 | var lit *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.BasicLitscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BasicLit# |
| 60 | astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Inspectscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Inspect().(f, 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#) bool { |
| 61 | if b, ok := n.(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.BasicLitscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BasicLit#); ok { |
| 62 | lit = b |
| 63 | return false |
| 64 | } |
| 65 | return true |
| 66 | }) |
| 67 | if lit == nil { |
| 68 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("No literal") |
| 69 | continue |
| 70 | } |
| 71 | |
| 72 | // Find index of marker within logical value. |
| 73 | value, err := strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/.Unquotescip-go gomod github.com/golang/go/src go1.25.0 strconv/Unquote().(lit.Valuescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BasicLit#Value.) |
| 74 | if err != nil { |
| 75 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Unquote: %v", err) |
| 76 | continue |
| 77 | } |
| 78 | index := 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().(value, "^") |
| 79 | if index < 0 { |
| 80 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Value %q contains no marker", value) |
| 81 | continue |
| 82 | } |
| 83 | |
| 84 | // Convert logical index to file position. |
| 85 | pos, err := astutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/.PosInStringLiteralscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/PosInStringLiteral().(lit, index) |
| 86 | if err != nil { |
| 87 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("PosInStringLiteral(%d): %v", index, err) |
| 88 | continue |
| 89 | } |
| 90 | |
| 91 | // Check that cut offset in original src file is before marker. |
| 92 | offset := fset.Positionscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/FileSet#Position().(pos).Offsetscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/Position#Offset. |
| 93 | before, after := src[:offset], src[offset:] |
| 94 | t.Logfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Logf().("\t%q :: %q", before, after) |
| 95 | if !stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.25.0 strings/HasPrefix().(after, "^") { |
| 96 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("no marker at cut point") |
| 97 | continue |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | func TestOffsetInStringLiteralscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil_test`/TestOffsetInStringLiteral().(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#) { |
| 103 | // Each string is Go source for a string literal with | marker at expected |
| 104 | // physical pos, and $ marker at the expected logical unquoted index. |
| 105 | tests := []string{ |
| 106 | `"$|abc"`, |
| 107 | `"a$|bc"`, |
| 108 | `"ab$|c"`, |
| 109 | `"abc$|"`, |
| 110 | `"a\n$|b"`, |
| 111 | `"\n$|"`, |
| 112 | `"a\000$|"`, |
| 113 | `"\x61$|"`, |
| 114 | `"\u0061$|"`, |
| 115 | `"\U00000061$|"`, |
| 116 | `"$\U00|000061"`, |
| 117 | `"€$|"`, |
| 118 | `"a€$|b"`, |
| 119 | "`abc$|`", |
| 120 | "`a\n$|b`", |
| 121 | // normalization of \r carriage returns: |
| 122 | "`a\r\n$|b`", |
| 123 | "`a\r\nb\r\nc\r\n$|d`", |
| 124 | } |
| 125 | for _, test := range tests { |
| 126 | // The workaround for \r requires the go1.26 fix for https://go.dev/issue/76031. |
| 127 | if stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Containsscip-go gomod github.com/golang/go/src go1.25.0 strings/Contains().(test, "\r") && testenvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/.Go1Pointscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/testenv`/Go1Point().() < 26 { |
| 128 | continue |
| 129 | } |
| 130 | |
| 131 | t.Logfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Logf().("input: %#q", test) |
| 132 | |
| 133 | const prefix = "package p; const _ = " |
| 134 | src := prefix + test |
| 135 | |
| 136 | // Remove the physical marker to produce valid Go source. |
| 137 | offset := 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().(src, "|") |
| 138 | if offset < 0 { |
| 139 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Source %q contains no marker", src) |
| 140 | continue |
| 141 | } |
| 142 | src = src[:offset] + src[offset+1:] |
| 143 | |
| 144 | // Parse. |
| 145 | fset := tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.NewFileSetscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/NewFileSet().() |
| 146 | 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, "p.go", src, 0) |
| 147 | if err != nil { |
| 148 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Parse: %v", err) |
| 149 | continue |
| 150 | } |
| 151 | |
| 152 | // Find literal. |
| 153 | var lit *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.BasicLitscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BasicLit# |
| 154 | astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Inspectscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Inspect().(f, 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#) bool { |
| 155 | if b, ok := n.(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.BasicLitscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BasicLit#); ok { |
| 156 | lit = b |
| 157 | return false |
| 158 | } |
| 159 | return true |
| 160 | }) |
| 161 | if lit == nil { |
| 162 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("No literal") |
| 163 | continue |
| 164 | } |
| 165 | |
| 166 | tfile := fset.Filescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/FileSet#File().(f.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#Pos().()) |
| 167 | pos := tfile.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/File#Pos().(offset) |
| 168 | |
| 169 | // Convert file position to logical index. |
| 170 | index, err := astutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/.OffsetInStringLiteralscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/OffsetInStringLiteral().(lit, pos) |
| 171 | if err != nil { |
| 172 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("OffsetInStringLiteral(%d): %v", pos, err) |
| 173 | continue |
| 174 | } |
| 175 | |
| 176 | value, err := strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/.Unquotescip-go gomod github.com/golang/go/src go1.25.0 strconv/Unquote().(lit.Valuescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BasicLit#Value.) |
| 177 | if err != nil { |
| 178 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Unquote: %v", err) |
| 179 | continue |
| 180 | } |
| 181 | |
| 182 | if index < 0 || index > len(value) { |
| 183 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Returned index %d is out of bounds for logical string of length %d", index, len(value)) |
| 184 | continue |
| 185 | } |
| 186 | |
| 187 | // Check that cut offset in value is before marker. |
| 188 | before, after := value[:index], value[index:] |
| 189 | t.Logfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Logf().("\t%q :: %q", before, after) |
| 190 | if !stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.HasSuffixscip-go gomod github.com/golang/go/src go1.25.0 strings/HasSuffix().(before, "$") { |
| 191 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("no marker at logical cut point, got before=%q", before) |
| 192 | continue |
| 193 | } |
| 194 | } |
| 195 | } |