-
Notifications
You must be signed in to change notification settings - Fork 4
/
regexp_test.go
221 lines (218 loc) · 6.15 KB
/
regexp_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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package dads
import (
"regexp"
"strings"
"testing"
)
func TestREs(t *testing.T) {
var testCases = []struct {
res string
str string
expected map[string]string
}{
{
res: GitCommitPattern.String(),
str: "commit a8ea74789413b83a44a3edaf064c6be976968d9f",
expected: map[string]string{
"commit": "a8ea74789413b83a44a3edaf064c6be976968d9f",
"parents": "",
"refs": "",
},
},
{
res: GitCommitPattern.String(),
str: "commit 0979a920f7b2a5278fccea0d71c875d60bb8e3a3 363a8f133dc3adcb149e1c9e9ad801840ef39538",
expected: map[string]string{
"commit": "0979a920f7b2a5278fccea0d71c875d60bb8e3a3",
"parents": "363a8f133dc3adcb149e1c9e9ad801840ef39538",
"refs": "",
},
},
{
res: GitCommitPattern.String(),
str: "commit 294c2079398417d6b046f4a11eccd9a21a7e51a6 0979a920f7b2a5278fccea0d71c875d60bb8e3a3 (HEAD -> refs/heads/master, refs/remotes/origin/master, refs/remotes/origin/HEAD)",
expected: map[string]string{
"commit": "294c2079398417d6b046f4a11eccd9a21a7e51a6",
"parents": "0979a920f7b2a5278fccea0d71c875d60bb8e3a3",
"refs": "HEAD -> refs/heads/master, refs/remotes/origin/master, refs/remotes/origin/HEAD",
},
},
{
res: GitHeaderPattern.String(),
str: "Author: Łukasz Gryglicki <[email protected]>",
expected: map[string]string{
"name": "Author",
"value": "Łukasz Gryglicki <[email protected]>",
},
},
{
res: GitMessagePattern.String(),
str: " this is a message ",
expected: map[string]string{
"msg": "this is a message ",
},
},
{
res: GitTrailerPattern.String(),
str: "Signed-off-by:\tŁukasz Gryglicki <[email protected]>",
expected: map[string]string{
"name": "Signed-off-by",
"value": "Łukasz Gryglicki <[email protected]>",
},
},
{
res: GitActionPattern.String(),
str: "::100644 100644 100644 aaf94db0 e82bf6fa e82bf6fa MC\t\t.circleci/deployments/develop/api.deployment.yml.erb",
expected: map[string]string{
"sc": "::",
"modes": "100644 100644 100644 ",
"indexes": "aaf94db0 e82bf6fa e82bf6fa ",
"action": "MC",
"file": ".circleci/deployments/develop/api.deployment.yml.erb",
"newfile": "",
},
},
{
res: GitActionPattern.String(),
str: ":000000 100644 0000000 66fd13c A\t.gitignore",
expected: map[string]string{
"sc": ":",
"modes": "000000 100644 ",
"indexes": "0000000 66fd13c ",
"action": "A",
"file": ".gitignore",
"newfile": "",
},
},
{
res: GitStatsPattern.String(),
str: "2 4 .circleci/deployments/{production => prod}/api.deployment.yml.erb",
expected: map[string]string{
"added": "2",
"removed": "4",
"file": ".circleci/deployments/{production => prod}/api.deployment.yml.erb",
},
},
{
res: GitAuthorsPattern.String(),
str: "Lukasz Gryglicki and Justyna Gryglicka <[email protected]>",
expected: map[string]string{
"first_authors": "Lukasz Gryglicki",
"last_author": "Justyna Gryglicka",
"email": "<[email protected]>",
},
},
{
res: GitAuthorsPattern.String(),
str: "Lukasz Gryglicki, Alicja Gryglicka, Krzysztof Gryglicki and Justyna Gryglicka <[email protected]>",
expected: map[string]string{
"first_authors": "Lukasz Gryglicki, Alicja Gryglicka, Krzysztof Gryglicki",
"last_author": "Justyna Gryglicka",
"email": "<[email protected]>",
},
},
{
res: GitAuthorsPattern.String(),
str: "Lukasz Gryglicki <[email protected]> and Justyna Gryglicka <[email protected]> <[email protected]>",
expected: map[string]string{
"first_authors": "Lukasz Gryglicki <[email protected]>",
"last_author": "Justyna Gryglicka <[email protected]>",
"email": "<[email protected]>",
},
},
{
res: GitCoAuthorsPattern.String(),
str: "Co-authored-by:Lukasz Gryglicki<[email protected]>",
expected: map[string]string{
"first_authors": "Lukasz Gryglicki",
"email": "[email protected]",
},
},
{
res: GitCoAuthorsPattern.String(),
str: "Co-authored-by:Lukasz Gryglicki<[email protected]>\nCo-authored-by:Justyna Gryglicka<[email protected]>",
expected: map[string]string{
"first_authors": "Lukasz Gryglicki",
"email": "[email protected]",
},
},
}
sameResult := func(a1, a2 map[string]string) bool {
m1 := make(map[[2]string]struct{})
m2 := make(map[[2]string]struct{})
for k, v := range a1 {
m1[[2]string{k, v}] = struct{}{}
}
for k, v := range a2 {
m2[[2]string{k, v}] = struct{}{}
}
for k := range m1 {
_, ok := m2[k]
if !ok {
return false
}
}
for k := range m2 {
_, ok := m1[k]
if !ok {
return false
}
}
return true
}
for index, test := range testCases {
re := regexp.MustCompile(test.res)
got := MatchGroups(re, test.str)
if !sameResult(got, test.expected) {
t.Errorf("test number %d, expected '%s' matching '%s' result %v, got %v", index+1, test.str, test.res, test.expected, got)
}
}
}
func TestArrayREs(t *testing.T) {
var testCases = []struct {
res string
str string
expected map[string][]string
}{
{
res: GitCoAuthorsPattern.String(),
str: "Co-authored-by:Lukasz Gryglicki<[email protected]>\nCo-authored-by:Justyna Gryglicka<[email protected]>\n",
expected: map[string][]string{
"first_authors": {"Lukasz Gryglicki", "Justyna Gryglicka"},
"email": {"[email protected]", "[email protected]"},
},
},
}
sameResult := func(a1, a2 map[string][]string) bool {
m1 := make(map[[2]string]struct{})
m2 := make(map[[2]string]struct{})
for k, va := range a1 {
v := strings.Join(va, ",")
m1[[2]string{k, v}] = struct{}{}
}
for k, va := range a2 {
v := strings.Join(va, ",")
m2[[2]string{k, v}] = struct{}{}
}
for k := range m1 {
_, ok := m2[k]
if !ok {
return false
}
}
for k := range m2 {
_, ok := m1[k]
if !ok {
return false
}
}
return true
}
for index, test := range testCases {
re := regexp.MustCompile(test.res)
got := MatchGroupsArray(re, test.str)
if !sameResult(got, test.expected) {
t.Errorf("test number %d, expected '%s' matching '%s' result %v, got %v", index+1, test.str, test.res, test.expected, got)
}
}
}