-
Notifications
You must be signed in to change notification settings - Fork 83
/
replace_test.go
172 lines (157 loc) · 4.65 KB
/
replace_test.go
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
package regexp2
import (
"strconv"
"testing"
)
func TestReplace_Basic(t *testing.T) {
re := MustCompile(`test`, 0)
str, err := re.Replace("this is a test", "unit", -1, -1)
if err != nil {
t.Fatalf("Unexpected err: %v", err)
}
if want, got := "this is a unit", str; want != got {
t.Fatalf("Replace failed, wanted %v, got %v", want, got)
}
}
func TestReplace_NamedGroup(t *testing.T) {
re := MustCompile(`[^ ]+\s(?<time>)`, 0)
str, err := re.Replace("08/10/99 16:00", "${time}", -1, -1)
if err != nil {
t.Fatalf("Unexpected err: %v", err)
}
if want, got := "16:00", str; want != got {
t.Fatalf("Replace failed, wanted %v, got %v", want, got)
}
}
func TestReplace_IgnoreCaseUpper(t *testing.T) {
re := MustCompile(`dog`, IgnoreCase)
str, err := re.Replace("my dog has fleas", "CAT", -1, -1)
if err != nil {
t.Fatalf("Unexpected err: %v", err)
}
if want, got := "my CAT has fleas", str; want != got {
t.Fatalf("Replace failed, wanted %v, got %v", want, got)
}
}
func TestReplace_IgnoreCaseLower(t *testing.T) {
re := MustCompile(`olang`, IgnoreCase)
str, err := re.Replace("GoLAnG", "olang", -1, -1)
if err != nil {
t.Fatalf("Unexpected err: %v", err)
}
if want, got := "Golang", str; want != got {
t.Fatalf("Replace failed, wanted %v, got %v", want, got)
}
}
func TestReplace_NumberGroup(t *testing.T) {
re := MustCompile(`D\.(.+)`, None)
str, err := re.Replace("D.Bau", "David $1", -1, -1)
if err != nil {
t.Fatalf("Unexpected err: %v", err)
}
if want, got := "David Bau", str; want != got {
t.Fatalf("Replace failed, wanted %v, got %v", want, got)
}
}
func TestReplace_LimitCount(t *testing.T) {
re := MustCompile(`a`, None)
str, err := re.Replace("aaaaa", "b", 0, 2)
if err != nil {
t.Fatalf("Unexpected err: %v", err)
}
if want, got := "bbaaa", str; want != got {
t.Fatalf("Replace failed, wanted %v, got %v", want, got)
}
}
func TestReplace_LimitCountSlice(t *testing.T) {
re := MustCompile(`a`, None)
myStr := "aaaaa"
str, err := re.Replace(myStr, "b", 3, 2)
if err != nil {
t.Fatalf("Unexpected err: %v", err)
}
if want, got := "aaabb", str; want != got {
t.Fatalf("Replace failed, wanted %v, got %v", want, got)
}
}
func TestReplace_BeginBeforeAfterEnd(t *testing.T) {
re := MustCompile(`a`, None)
myStr := "a test a blah and a"
str, err := re.Replace(myStr, "stuff", -1, -1)
if err != nil {
t.Fatalf("Unexpected err: %v", err)
}
if want, got := "stuff test stuff blstuffh stuffnd stuff", str; want != got {
t.Fatalf("Replace failed, wanted %v, got %v", want, got)
}
}
func TestReplace_BadSyntax(t *testing.T) {
re := MustCompile(`a`, None)
myStr := "this is a test"
_, err := re.Replace(myStr, `$5000000000`, -1, -1)
if err == nil {
t.Fatalf("Expected err")
}
}
func TestReplaceFunc_Basic(t *testing.T) {
re := MustCompile(`test`, None)
str, err := re.ReplaceFunc("this is a test", func(m Match) string { return "unit" }, -1, -1)
if err != nil {
t.Fatalf("Unexpected err: %v", err)
}
if want, got := "this is a unit", str; want != got {
t.Fatalf("Replace failed, wanted %v, got %v", want, got)
}
}
func TestReplaceFunc_Multiple(t *testing.T) {
re := MustCompile(`test`, None)
count := 0
str, err := re.ReplaceFunc("This test is another test for stuff", func(m Match) string {
count++
return strconv.Itoa(count)
}, -1, -1)
if err != nil {
t.Fatalf("Unexpected err: %v", err)
}
if want, got := "This 1 is another 2 for stuff", str; want != got {
t.Fatalf("Replace failed, wanted %v, got %v", want, got)
}
}
func TestReplaceFunc_Groups(t *testing.T) {
re := MustCompile(`test(?<sub>ing)?`, None)
count := 0
str, err := re.ReplaceFunc("This testing is another test testingly junk", func(m Match) string {
count++
if m.GroupByName("sub").Length > 0 {
// we have an "ing", make it negative
return strconv.Itoa(count * -1)
}
return strconv.Itoa(count)
}, -1, -1)
if err != nil {
t.Fatalf("Unexpected err: %v", err)
}
if want, got := "This -1 is another 2 -3ly junk", str; want != got {
t.Fatalf("Replace failed, wanted %v, got %v", want, got)
}
}
func TestReplace_RefNumsDollarAmbiguous(t *testing.T) {
re := MustCompile("(123)hello(789)", None)
res, err := re.Replace("123hello789", "$1456$2", -1, -1)
if err != nil {
t.Fatal(err)
}
if want, got := "$1456789", res; want != got {
t.Fatalf("Wrong result: %s", got)
}
}
func TestReplace_NestedGroups(t *testing.T) {
re := MustCompile(`(\p{Sc}\s?)?(\d+\.?((?<=\.)\d+)?)(?(1)|\s?\p{Sc})?`, None)
res, err := re.Replace("$17.43 €2 16.33 £0.98 0.43 £43 12€ 17", "$2", -1, -1)
if err != nil {
t.Fatal(err)
}
if want, got := "17.43 2 16.33 0.98 0.43 43 12 17", res; want != got {
t.Fatalf("Wrong result: %s", got)
}
}