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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
|
package regex
import (
"testing"
)
func TestIsLiteralPattern(t *testing.T) {
tests := []struct {
pattern string
expected bool
}{
// Literal patterns
{"ERROR", true},
{"hello world", true},
{"test123", true},
{"user@example.com", false}, // Contains @ which could be confused in some contexts
{"192.168.1.1", false}, // Contains dots
{"path/to/file", true},
{"key=value", true},
{"JSON-data", true},
{"_underscore_", true},
// Non-literal patterns (contain regex metacharacters)
{".*", false},
{"test.*", false},
{"^start", false},
{"end$", false},
{"[abc]", false},
{"a+b", false},
{"a?b", false},
{"a*b", false},
{"(group)", false},
{"a|b", false},
{"test\\d", false},
{"test{3}", false},
{"test.log", false}, // Contains dot
}
for _, tt := range tests {
t.Run(tt.pattern, func(t *testing.T) {
got := isLiteralPattern(tt.pattern)
if got != tt.expected {
t.Errorf("isLiteralPattern(%q) = %v, want %v", tt.pattern, got, tt.expected)
}
})
}
}
func TestLiteralMatching(t *testing.T) {
tests := []struct {
pattern string
text string
match bool
}{
{"ERROR", "This is an ERROR message", true},
{"ERROR", "This is an error message", false}, // Case sensitive
{"WARNING", "This is an ERROR message", false},
{"test", "testing 123", true},
{"test", "Test 123", false}, // Case sensitive
}
for _, tt := range tests {
t.Run(tt.pattern, func(t *testing.T) {
// Test with Default flag
r, err := New(tt.pattern, Default)
if err != nil {
t.Fatalf("Failed to create regex: %v", err)
}
// Verify it's detected as literal
if !r.isLiteral {
t.Errorf("Pattern %q should be detected as literal", tt.pattern)
}
// Test string matching
got := r.MatchString(tt.text)
if got != tt.match {
t.Errorf("MatchString(%q, %q) = %v, want %v", tt.pattern, tt.text, got, tt.match)
}
// Test byte matching
gotBytes := r.Match([]byte(tt.text))
if gotBytes != tt.match {
t.Errorf("Match(%q, %q) = %v, want %v", tt.pattern, tt.text, gotBytes, tt.match)
}
})
}
// Test with Invert flag
t.Run("InvertFlag", func(t *testing.T) {
r, err := New("ERROR", Invert)
if err != nil {
t.Fatalf("Failed to create regex: %v", err)
}
if !r.isLiteral {
t.Error("Pattern should be detected as literal")
}
// Should NOT match when pattern is present
if r.MatchString("This is an ERROR message") {
t.Error("Inverted match should return false when pattern is present")
}
// Should match when pattern is absent
if !r.MatchString("This is a normal message") {
t.Error("Inverted match should return true when pattern is absent")
}
})
}
func TestRegexCompatibility(t *testing.T) {
// Ensure literal matching produces same results as regex matching
patterns := []string{
"ERROR",
"WARNING",
"user123",
"test-data",
}
texts := []string{
"This is an ERROR message",
"WARNING: something happened",
"User user123 logged in",
"Processing test-data file",
"No match here",
}
for _, pattern := range patterns {
// Create literal regex
literalRegex, err := New(pattern, Default)
if err != nil {
t.Fatalf("Failed to create literal regex: %v", err)
}
// Force creation of a non-literal regex for comparison
// We'll do this by adding a harmless regex character that doesn't change the meaning
regexPattern := "(?:" + pattern + ")"
regexRegex, err := New(regexPattern, Default)
if err != nil {
t.Fatalf("Failed to create regex: %v", err)
}
// The literal version should be optimized
if !literalRegex.isLiteral {
t.Errorf("Pattern %q should be literal", pattern)
}
// The regex version should not be optimized
if regexRegex.isLiteral {
t.Errorf("Pattern %q should not be literal", regexPattern)
}
// Both should produce same match results
for _, text := range texts {
literalMatch := literalRegex.MatchString(text)
// Test specific expected matches
expectedMatch := false
switch pattern {
case "ERROR":
expectedMatch = text == "This is an ERROR message"
case "WARNING":
expectedMatch = text == "WARNING: something happened"
case "user123":
expectedMatch = text == "User user123 logged in"
case "test-data":
expectedMatch = text == "Processing test-data file"
}
if literalMatch != expectedMatch {
t.Errorf("Pattern %q matching text %q: got %v, want %v", pattern, text, literalMatch, expectedMatch)
}
}
}
}
func TestSerializationWithLiteral(t *testing.T) {
// Test that serialization preserves literal optimization hint
r, err := New("ERROR", Default)
if err != nil {
t.Fatalf("Failed to create regex: %v", err)
}
if !r.isLiteral {
t.Error("Pattern should be detected as literal")
}
// Serialize
serialized, err := r.Serialize()
if err != nil {
t.Fatalf("Failed to serialize: %v", err)
}
// Should contain literal flag
if !contains(serialized, "literal") {
t.Errorf("Serialized form should contain 'literal' flag: %s", serialized)
}
// Deserialize
deserialized, err := Deserialize(serialized)
if err != nil {
t.Fatalf("Failed to deserialize: %v", err)
}
// Should still be literal
if !deserialized.isLiteral {
t.Error("Deserialized regex should maintain literal flag")
}
// Should match the same
testStr := "This is an ERROR message"
if r.MatchString(testStr) != deserialized.MatchString(testStr) {
t.Error("Original and deserialized regex should produce same match results")
}
}
// Helper function since we can't use strings.Contains in the regex package
func contains(s, substr string) bool {
for i := 0; i <= len(s)-len(substr); i++ {
if s[i:i+len(substr)] == substr {
return true
}
}
return false
}
|