-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanonymous_test.go
More file actions
107 lines (78 loc) · 2.69 KB
/
Copy pathanonymous_test.go
File metadata and controls
107 lines (78 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package feature_test
import (
"fmt"
"path/filepath"
"strings"
"testing"
"github.com/mpyw/feature"
)
// TestAnonymousKeyCallSite tests that anonymous keys capture the correct call site information.
func TestAnonymousKeyCallSite(t *testing.T) {
t.Parallel()
t.Run("New without name returns call site info", func(t *testing.T) {
t.Parallel()
key := feature.New[int]()
assertAnonymousKeyFormat(t, key.String(), "anonymous_test.go", 19)
})
t.Run("NewBool returns call site info", func(t *testing.T) {
t.Parallel()
key := feature.NewBool()
assertAnonymousKeyFormat(t, key.String(), "anonymous_test.go", 26)
})
t.Run("NewNamed with empty name returns call site info", func(t *testing.T) {
t.Parallel()
key := feature.NewNamed[int]("")
assertAnonymousKeyFormat(t, key.String(), "anonymous_test.go", 33)
})
t.Run("NewNamedBool with empty name returns call site info", func(t *testing.T) {
t.Parallel()
key := feature.NewNamedBool("")
assertAnonymousKeyFormat(t, key.String(), "anonymous_test.go", 40)
})
}
// assertAnonymousKeyFormat verifies that an anonymous key string has the correct format
// with the expected filename and line number.
//
//nolint:unparam // explicitly keeping wantFile and wantLine for clarity
func assertAnonymousKeyFormat(t *testing.T, str, wantFile string, wantLine int) {
t.Helper()
// Should have format "anonymous(/full/path/to/file.go:line)@0x..."
if !strings.HasPrefix(str, "anonymous(") {
t.Errorf("String() = %q, want prefix %q", str, "anonymous(")
return
}
if !strings.Contains(str, "@0x") {
t.Errorf("String() = %q, want to contain %q", str, "@0x")
return
}
// Extract the file path and line number from the string
// Format: anonymous(/path/to/feature_test.go:123)@0xaddress
start := strings.Index(str, "(")
end := strings.LastIndex(str, ")")
if start == -1 || end == -1 || start >= end {
t.Errorf("String() = %q, could not extract file:line info", str)
return
}
fileLineInfo := str[start+1 : end]
lastColon := strings.LastIndex(fileLineInfo, ":")
if lastColon == -1 {
t.Errorf("String() = %q, could not find colon in file:line info %q", str, fileLineInfo)
return
}
filePath := fileLineInfo[:lastColon]
lineStr := fileLineInfo[lastColon+1:]
// Verify filename
baseName := filepath.Base(filePath)
if baseName != wantFile {
t.Errorf("filepath.Base(filePath) = %q, want %q (full path: %q)", baseName, wantFile, filePath)
}
// Verify line number
var gotLine int
if _, err := fmt.Sscanf(lineStr, "%d", &gotLine); err != nil {
t.Errorf("could not parse line number from %q: %v", lineStr, err)
return
}
if gotLine != wantLine {
t.Errorf("line number = %d, want %d (full string: %q)", gotLine, wantLine, str)
}
}