Debian Codegraph @

gopls/internal/cache/testfuncs/match.go

raw symbols SCIP
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 testfuncs
6
7 import (
8 "fmt"
9 "strconv"
10 "strings"
11 )
12
13 // The functions in this file are copies of those from the testing package.
14 //
15 // https://cs.opensource.google/go/go/+/refs/tags/go1.22.5:src/testing/match.go
16
17 // uniqueName creates a unique name for the given parent and subname by affixing
18 // it with one or more counts, if necessary.
19 func (b *indexBuilder) uniqueName(parent, subname string) string {
20 base := parent + "/" + subname
21
22 for {
23 n := b.subNames[base]
24 if n < 0 {
25 panic("subtest count overflow")
26 }
27 b.subNames[base] = n + 1
28
29 if n == 0 && subname != "" {
30 prefix, nn := parseSubtestNumber(base)
31 if len(prefix) < len(base) && nn < b.subNames[prefix] {
32 // This test is explicitly named like "parent/subname#NN",
33 // and #NN was already used for the NNth occurrence of "parent/subname".
34 // Loop to add a disambiguating suffix.
35 continue
36 }
37 return base
38 }
39
40 name := fmt.Sprintf("%s#%02d", base, n)
41 if b.subNames[name] != 0 {
42 // This is the nth occurrence of base, but the name "parent/subname#NN"
43 // collides with the first occurrence of a subtest *explicitly* named
44 // "parent/subname#NN". Try the next number.
45 continue
46 }
47
48 return name
49 }
50 }
51
52 // parseSubtestNumber splits a subtest name into a "#%02d"-formatted int
53 // suffix (if present), and a prefix preceding that suffix (always).
54 func parseSubtestNumber(s string) (prefix string, nn int) {
55 i := strings.LastIndex(s, "#")
56 if i < 0 {
57 return s, 0
58 }
59
60 prefix, suffix := s[:i], s[i+1:]
61 if len(suffix) < 2 || (len(suffix) > 2 && suffix[0] == '0') {
62 // Even if suffix is numeric, it is not a possible output of a "%02" format
63 // string: it has either too few digits or too many leading zeroes.
64 return s, 0
65 }
66 if suffix == "00" {
67 if !strings.HasSuffix(prefix, "/") {
68 // We only use "#00" as a suffix for subtests named with the empty
69 // string — it isn't a valid suffix if the subtest name is non-empty.
70 return s, 0
71 }
72 }
73
74 n, err := strconv.ParseInt(suffix, 10, 32)
75 if err != nil || n < 0 {
76 return s, 0
77 }
78 return prefix, int(n)
79 }
80
81 // rewrite rewrites a subname to having only printable characters and no white
82 // space.
83 func rewrite(s string) string {
84 b := []byte{}
85 for _, r := range s {
86 switch {
87 case isSpace(r):
88 b = append(b, '_')
89 case !strconv.IsPrint(r):
90 s := strconv.QuoteRune(r)
91 b = append(b, s[1:len(s)-1]...)
92 default:
93 b = append(b, string(r)...)
94 }
95 }
96 return string(b)
97 }
98
99 func isSpace(r rune) bool {
100 if r < 0x2000 {
101 switch r {
102 // Note: not the same as Unicode Z class.
103 case '\t', '\n', '\v', '\f', '\r', ' ', 0x85, 0xA0, 0x1680:
104 return true
105 }
106 } else {
107 if r <= 0x200a {
108 return true
109 }
110 switch r {
111 case 0x2028, 0x2029, 0x202f, 0x205f, 0x3000:
112 return true
113 }
114 }
115 return false
116 }