-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregexpx_test.go
158 lines (142 loc) · 4.15 KB
/
regexpx_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
package regexpx_test
import (
"regexp"
"testing"
rx "github.com/yargevad/regexpx"
)
var testMatch = rx.RegexpSet{
regexp.MustCompile(`^abc+$`),
regexp.MustCompile(`^abc+d$`),
}
type MatchTest struct {
Input string
Match bool
Index int
}
func TestMatch(t *testing.T) {
for _, test := range []MatchTest{
{"", false, -1},
{"a", false, -1},
{"abc", true, 0},
{"abcc", true, 0},
{"abcd", true, 1},
{"abcde", false, -1},
} {
actual, idx := testMatch.Match(test.Input)
if actual != test.Match {
t.Fatalf("string %q expected %t actual %t", test.Input, test.Match, actual)
} else if idx != test.Index {
t.Fatalf("string %q expected index %d, actual %d", test.Input, test.Index, idx)
}
}
}
type SplitTest struct {
Input string
Index int
Output []string
}
var testSplit = rx.RegexpSet{
regexp.MustCompile(`[aeiou]`),
}
func TestSplit(t *testing.T) {
for _, test := range []SplitTest{
{"paqerisotu", 0, []string{"p", "q", "r", "s", "t", ""}},
{"fffffff", -1, nil},
} {
out, idx := testSplit.Split(test.Input, -1)
if idx != test.Index {
t.Fatalf("string %q expected index %d, actual %d", test.Input, test.Index, idx)
} else if len(out) != len(test.Output) {
t.Fatalf("string %q expected length %d, actual %d", test.Input, len(test.Output), len(out))
} else {
for i, elt := range out {
if test.Output[i] != elt {
t.Fatalf("string %q expected %q, actual %q", test.Input, test.Output, out)
}
}
}
}
}
type ReplaceTest struct {
Input string
Replace string
Index int
Output string
}
var testReplace = rx.RegexpSet{
regexp.MustCompile(`foo`),
regexp.MustCompile(`y`),
regexp.MustCompile(`(baz)`),
}
func TestReplace(t *testing.T) {
for _, test := range []ReplaceTest{
{"afoobfooc", "", 0, "abc"},
{"xbarybarz", "", 1, "xbarbarz"},
{"abazbbazc", "($1)", 2, "a(baz)b(baz)c"},
{"abarbbarc", "", -1, "abarbbarc"},
} {
out, idx := testReplace.Replace(test.Input, test.Replace)
if idx != test.Index {
t.Fatalf("string %q expected index %d, actual %d", test.Input, test.Index, idx)
} else if test.Output != out {
t.Fatalf("string %q expected %q, actual %q", test.Input, test.Output, out)
}
}
}
type ReplaceSubmatchTest struct {
Input string
Replace string
Index int
Output string
Matches []string
}
var testReplaceSubmatch = rx.RegexpSet{
regexp.MustCompile(`(cd)`),
}
func TestReplaceSubmatch(t *testing.T) {
for _, test := range []ReplaceSubmatchTest{
{"abcdef", "", 0, "abef", []string{"cd", "cd"}},
} {
out, idx, matches := testReplaceSubmatch.ReplaceSubmatch(test.Input, test.Replace)
if idx != test.Index {
t.Fatalf("string %q expected index %d, actual %d", test.Input, test.Index, idx)
} else if test.Output != out {
t.Fatalf("string %q expected %q, actual %q", test.Input, test.Output, out)
} else if len(test.Matches) != len(matches) {
t.Fatalf("string %q expected length %d, actual %d", test.Input, test.Matches, matches)
} else {
for i, m := range matches {
if test.Matches[i] != m {
t.Fatalf("string %q expected matches %q, actual %q", test.Input, test.Matches, matches)
}
}
}
}
}
var testSuffixes = rx.RegexpSet{
regexp.MustCompile(`(?i)\s*\b(jr\.?)(?:(,)|$)`),
regexp.MustCompile(`(?i)\s*\b(sr\.?)(?:(,)|$)`),
regexp.MustCompile(`(?i)\s*\b(iii?)(?:(,)|$)`),
regexp.MustCompile(`(?i)\s*\b(iv)(?:(,)|$)`),
}
func TestSuffixes(t *testing.T) {
for _, test := range []ReplaceSubmatchTest{
{"DOE JR, JOHN J", "$2", 0, "DOE, JOHN J", []string{" JR,", "JR", ","}},
{"Doe, Jane G Jr", "$2", 0, "Doe, Jane G", []string{" Jr", "Jr", ""}},
} {
out, idx, matches := testSuffixes.ReplaceSubmatch(test.Input, test.Replace)
if idx != test.Index {
t.Fatalf("string %q expected index %d, actual %d", test.Input, test.Index, idx)
} else if test.Output != out {
t.Fatalf("string %q expected %q, actual %q", test.Input, test.Output, out)
} else if len(test.Matches) != len(matches) {
t.Fatalf("string %q expected length %d, actual %d", test.Input, len(test.Matches), len(matches))
} else {
for i, m := range matches {
if test.Matches[i] != m {
t.Fatalf("string %q expected matches %q, actual %q", test.Input, test.Matches, matches)
}
}
}
}
}