-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocessing_test.go
76 lines (71 loc) · 1.64 KB
/
processing_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
package main
import (
"regexp"
"testing"
"github.com/stretchr/testify/assert"
)
func TestReplace(t *testing.T) {
tests := []struct {
name string
regex *regexp.Regexp
content string
replacement string
group uint8
expected string
wantsReplace bool
}{
{
name: "match and replace",
regex: regexp.MustCompile("b*"),
content: "aaabbbccc",
replacement: "x",
expected: "aaaxccc",
wantsReplace: true,
},
{
name: "no match",
regex: regexp.MustCompile("b*"),
content: "aaaccc",
replacement: "x",
expected: "aaaccc",
wantsReplace: false,
},
{
name: "in group replacement (middle)",
regex: regexp.MustCompile("0(b*)0"),
content: "aaa0bbb0ccc",
replacement: "x",
expected: "aaa0x0ccc",
group: 1,
wantsReplace: true,
},
{
name: "in group replacement (start)",
regex: regexp.MustCompile("(b*)00"),
content: "aaabbb00ccc",
replacement: "x",
expected: "aaax00ccc",
group: 1,
wantsReplace: true,
},
{
name: "in group replacement (end)",
regex: regexp.MustCompile("00(b*)"),
content: "aaa00bbbccc",
replacement: "x",
expected: "aaa00xccc",
group: 1,
wantsReplace: true,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
res, replaced := replace(test.content, test.regex, &Replacement{
Replacement: test.replacement,
Group: test.group,
})
assert.Equal(t, test.wantsReplace, replaced)
assert.Equal(t, test.expected, res)
})
}
}