|
| 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_test |
| 6 | + |
| 7 | +import ( |
| 8 | + "go/ast" |
| 9 | + "go/parser" |
| 10 | + "go/token" |
| 11 | + "strconv" |
| 12 | + "strings" |
| 13 | + "testing" |
| 14 | + |
| 15 | + "golang.org/x/tools/internal/astutil" |
| 16 | + "golang.org/x/tools/internal/testenv" |
| 17 | +) |
| 18 | + |
| 19 | +func TestPosInStringLiteral(t *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 strings.Contains(test, "\r") && testenv.Go1Point() < 26 { |
| 43 | + continue |
| 44 | + } |
| 45 | + |
| 46 | + t.Logf("input: %#q", test) |
| 47 | + |
| 48 | + // Parse. |
| 49 | + const prefix = "package p; const _ = " |
| 50 | + src := prefix + test |
| 51 | + fset := token.NewFileSet() |
| 52 | + f, err := parser.ParseFile(fset, "p.go", src, 0) |
| 53 | + if err != nil { |
| 54 | + t.Errorf("Parse: %v", err) |
| 55 | + continue |
| 56 | + } |
| 57 | + |
| 58 | + // Find literal. |
| 59 | + var lit *ast.BasicLit |
| 60 | + ast.Inspect(f, func(n ast.Node) bool { |
| 61 | + if b, ok := n.(*ast.BasicLit); ok { |
| 62 | + lit = b |
| 63 | + return false |
| 64 | + } |
| 65 | + return true |
| 66 | + }) |
| 67 | + if lit == nil { |
| 68 | + t.Errorf("No literal") |
| 69 | + continue |
| 70 | + } |
| 71 | + |
| 72 | + // Find index of marker within logical value. |
| 73 | + value, err := strconv.Unquote(lit.Value) |
| 74 | + if err != nil { |
| 75 | + t.Errorf("Unquote: %v", err) |
| 76 | + continue |
| 77 | + } |
| 78 | + index := strings.Index(value, "^") |
| 79 | + if index < 0 { |
| 80 | + t.Errorf("Value %q contains no marker", value) |
| 81 | + continue |
| 82 | + } |
| 83 | + |
| 84 | + // Convert logical index to file position. |
| 85 | + pos, err := astutil.PosInStringLiteral(lit, index) |
| 86 | + if err != nil { |
| 87 | + t.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.Position(pos).Offset |
| 93 | + before, after := src[:offset], src[offset:] |
| 94 | + t.Logf("\t%q :: %q", before, after) |
| 95 | + if !strings.HasPrefix(after, "^") { |
| 96 | + t.Errorf("no marker at cut point") |
| 97 | + continue |
| 98 | + } |
| 99 | + } |
| 100 | +} |
0 commit comments