@@ -0,0 +1,77 @@
8+func TestRegexCache(t *testing.T) {
9+ cache := newRegexCache()
11+ // Test basic caching
12+ pattern := "test.*pattern"
13+ regex1, err := cache.get(pattern)
15+ t.Fatalf("Failed to compile regex: %v", err)
18+ regex2, err := cache.get(pattern)
20+ t.Fatalf("Failed to get cached regex: %v", err)
23+ // Should be the same instance (cached)
24+ if regex1 != regex2 {
25+ t.Error("Expected cached regex to be the same instance")
28+ // Test that it actually works
29+ if !regex1.MatchString("test123pattern") {
30+ t.Error("Regex should match test string")
34+func TestGlobToRegexCaching(t *testing.T) {
35+ // Test that globToRegex uses pre-compiled regex
36+ pattern1 := globToRegex("*.{js,ts}")
38+ // Should not panic and should work correctly
39+ regex1, err := regexp.Compile(pattern1)
41+ t.Fatalf("Failed to compile glob regex: %v", err)
44+ if !regex1.MatchString("test.js") {
45+ t.Error("Glob regex should match .js files")
47+ if !regex1.MatchString("test.ts") {
48+ t.Error("Glob regex should match .ts files")
50+ if regex1.MatchString("test.go") {
51+ t.Error("Glob regex should not match .go files")
55+// Benchmark to show performance improvement
56+func BenchmarkRegexCacheVsCompile(b *testing.B) {
57+ cache := newRegexCache()
58+ pattern := "test.*pattern.*[0-9]+"
60+ b.Run("WithCache", func(b *testing.B) {
62+ _, err := cache.get(pattern)
69+ b.Run("WithoutCache", func(b *testing.B) {
71+ _, err := regexp.Compile(pattern)