-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathanchor_test.go
More file actions
109 lines (95 loc) · 2.62 KB
/
anchor_test.go
File metadata and controls
109 lines (95 loc) · 2.62 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
package coregex
import (
"regexp"
"testing"
)
// TestAnchorInFindAll tests that ^ anchor only matches at position 0
// This is a regression test for issue #14
func TestAnchorInFindAll(t *testing.T) {
tests := []struct {
pattern string
input string
}{
{"^", "12345"},
{"(^)", "12345"},
{"(^)|($)", "12345"},
{"($)|(^)", "12345"},
{"^test", "test hello test"},
{"^[a-z]+", "hello world"},
}
for _, tt := range tests {
t.Run(tt.pattern, func(t *testing.T) {
// stdlib reference
re := regexp.MustCompile(tt.pattern)
expected := re.FindAllStringIndex(tt.input, -1)
// coregex
cre := MustCompile(tt.pattern)
got := cre.FindAllStringIndex(tt.input, -1)
// Compare
if len(got) != len(expected) {
t.Errorf("FindAllStringIndex(%q, %q): got %d matches, want %d matches\n got: %v\n want: %v",
tt.pattern, tt.input, len(got), len(expected), got, expected)
return
}
for i := range got {
if got[i][0] != expected[i][0] || got[i][1] != expected[i][1] {
t.Errorf("FindAllStringIndex(%q, %q)[%d]: got %v, want %v",
tt.pattern, tt.input, i, got[i], expected[i])
}
}
})
}
}
// TestAnchorInReplaceAll tests that ^ anchor works correctly in ReplaceAll
func TestAnchorInReplaceAll(t *testing.T) {
tests := []struct {
pattern string
input string
repl string
}{
{"^", "12345", "x"},
{"(^)", "12345", "x"},
{"(^)|($)", "12345", "x"},
{"^test", "test hello test", "START"},
{"^[a-z]+", "hello world", "WORD"},
}
for _, tt := range tests {
t.Run(tt.pattern, func(t *testing.T) {
// stdlib reference
re := regexp.MustCompile(tt.pattern)
expected := re.ReplaceAllString(tt.input, tt.repl)
// coregex
cre := MustCompile(tt.pattern)
got := cre.ReplaceAllString(tt.input, tt.repl)
if got != expected {
t.Errorf("ReplaceAllString(%q, %q, %q):\n got: %q\n want: %q",
tt.pattern, tt.input, tt.repl, got, expected)
}
})
}
}
// TestAnchorWithCaptures tests ^ anchor with capture groups in ReplaceAll
func TestAnchorWithCaptures(t *testing.T) {
tests := []struct {
pattern string
input string
repl string
}{
{"(^)(.+)", "hello", "[$1$2]"},
{"^([a-z]+)", "hello world", "WORD:$1"},
}
for _, tt := range tests {
t.Run(tt.pattern, func(t *testing.T) {
// stdlib reference
re := regexp.MustCompile(tt.pattern)
expected := re.ReplaceAllString(tt.input, tt.repl)
// coregex
cre := MustCompile(tt.pattern)
got := cre.ReplaceAllString(tt.input, tt.repl)
if got != expected {
t.Errorf("ReplaceAllString(%q, %q, %q):\n got: %q\n want: %q",
tt.pattern, tt.input, tt.repl, got, expected)
}
})
}
}