-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy patherror_test.go
More file actions
136 lines (115 loc) · 3.19 KB
/
error_test.go
File metadata and controls
136 lines (115 loc) · 3.19 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package coregex
import (
"regexp"
"strings"
"testing"
"github.com/coregx/coregex/meta"
)
// TestErrorMessageFormat verifies that error messages match stdlib format.
// stdlib returns *syntax.Error directly with format: "error parsing regexp: ..."
func TestErrorMessageFormat(t *testing.T) {
patterns := []string{
"[invalid",
`\`,
"(abc",
"*abc",
`\8`,
}
for _, pattern := range patterns {
t.Run(pattern, func(t *testing.T) {
_, stdlibErr := regexp.Compile(pattern)
_, ourErr := Compile(pattern)
if stdlibErr == nil {
t.Skip("stdlib accepts this pattern")
}
if ourErr == nil {
t.Fatalf("Compile(%q) expected error, got nil", pattern)
}
// Error messages should match exactly
if ourErr.Error() != stdlibErr.Error() {
t.Errorf("error message mismatch:\n got: %q\n want: %q",
ourErr.Error(), stdlibErr.Error())
}
})
}
}
// TestMustCompilePanicFormat verifies MustCompile panic message matches stdlib format.
func TestMustCompilePanicFormat(t *testing.T) {
pattern := "[invalid"
// Get stdlib panic format for comparison
var stdlibPanic string
func() {
defer func() {
if r := recover(); r != nil {
stdlibPanic = r.(string)
}
}()
regexp.MustCompile(pattern)
}()
// Get our panic format
var ourPanic string
func() {
defer func() {
if r := recover(); r != nil {
ourPanic = r.(string)
}
}()
MustCompile(pattern)
}()
// Both should start with "regexp: Compile(`"
wantPrefix := "regexp: Compile(`"
if !strings.HasPrefix(stdlibPanic, wantPrefix) {
t.Logf("Note: stdlib panic format: %s", stdlibPanic)
}
if !strings.HasPrefix(ourPanic, wantPrefix) {
t.Errorf("MustCompile panic should start with %q, got: %s", wantPrefix, ourPanic)
}
// Should contain the pattern in backticks
if !strings.Contains(ourPanic, "`"+pattern+"`") {
t.Errorf("MustCompile panic should contain pattern in backticks, got: %s", ourPanic)
}
}
// TestConfigErrorPrefix verifies config errors use "regexp:" prefix.
func TestConfigErrorPrefix(t *testing.T) {
// Create invalid config
config := meta.DefaultConfig()
config.MaxDFAStates = 0 // Invalid: must be > 0
_, err := CompileWithConfig("abc", config)
if err == nil {
t.Fatal("expected error for invalid config")
}
errMsg := err.Error()
if !strings.HasPrefix(errMsg, "regexp:") {
t.Errorf("config error should start with 'regexp:', got: %s", errMsg)
}
}
// TestCompileErrorVsStdlib compares error behavior with stdlib.
func TestCompileErrorVsStdlib(t *testing.T) {
invalidPatterns := []string{
"[",
"(",
"*",
`\`,
"(?P<>abc)", // empty capture name
}
for _, pattern := range invalidPatterns {
t.Run(pattern, func(t *testing.T) {
_, stdlibErr := regexp.Compile(pattern)
_, ourErr := Compile(pattern)
// Both should error
if stdlibErr == nil && ourErr == nil {
return // Both accept it, OK
}
if stdlibErr != nil && ourErr == nil {
t.Errorf("stdlib rejects %q but we accept it", pattern)
}
// Our error should match stdlib format exactly
if ourErr != nil && stdlibErr != nil {
if ourErr.Error() != stdlibErr.Error() {
t.Errorf("error message mismatch:\n got: %q\n want: %q",
ourErr.Error(), stdlibErr.Error())
}
}
})
}
}