@@ -18,6 +18,7 @@ package main
18
18
19
19
import (
20
20
"errors"
21
+ "regexp"
21
22
"testing"
22
23
23
24
"k8s.io/gengo/v2/types"
@@ -121,7 +122,6 @@ func TestRuleOptionalAndRequired(t *testing.T) {
121
122
name string
122
123
comments []string
123
124
wantMsg string
124
- wantErr bool
125
125
}{
126
126
{
127
127
name : "no comments" ,
@@ -139,22 +139,37 @@ func TestRuleOptionalAndRequired(t *testing.T) {
139
139
wantMsg : "" ,
140
140
},
141
141
{
142
- name : "optional and required" ,
142
+ name : "optional required" ,
143
143
comments : []string {"+k8s:optional" , "+k8s:required" },
144
- wantMsg : " conflicting tags: {+k8s:optional, +k8s:required}" ,
144
+ wantMsg : ` conflicting tags: {\ +k8s:optional, \ +k8s:required}` ,
145
145
},
146
146
{
147
- name : "optional, empty, required" ,
147
+ name : "required optional" ,
148
+ comments : []string {"+k8s:optional" , "+k8s:required" },
149
+ wantMsg : `conflicting tags: {\+k8s:optional, \+k8s:required}` ,
150
+ },
151
+ {
152
+ name : "optional empty required" ,
148
153
comments : []string {"+k8s:optional" , "" , "+k8s:required" },
149
- wantMsg : "conflicting tags: {+k8s:optional, +k8s:required}" ,
154
+ wantMsg : `conflicting tags: {\+k8s:optional, \+k8s:required}` ,
155
+ },
156
+ {
157
+ name : "empty required empty empty optional empty" ,
158
+ comments : []string {"" , "+k8s:optional" , "" , "" , "+k8s:required" , "" },
159
+ wantMsg : `conflicting tags: {\+k8s:optional, \+k8s:required}` ,
150
160
},
151
161
}
152
162
153
163
for _ , tt := range tests {
154
164
t .Run (tt .name , func (t * testing.T ) {
155
- msg , _ := ruleOptionalAndRequired (tt .comments )
156
- if msg != tt .wantMsg {
157
- t .Errorf ("ruleOptionalAndRequired() msg = %v, wantMsg %v" , msg , tt .wantMsg )
165
+ msg , err := ruleOptionalAndRequired (tt .comments )
166
+ if err != nil {
167
+ t .Errorf ("unexpected error: %v" , err )
168
+ } else if tt .wantMsg != "" {
169
+ re := regexp .MustCompile (tt .wantMsg )
170
+ if ! re .MatchString (msg ) {
171
+ t .Errorf ("message:\n \t %s\n does not match:\n \t %s" , msg , re .String ())
172
+ }
158
173
}
159
174
})
160
175
}
@@ -182,22 +197,37 @@ func TestRuleRequiredAndDefault(t *testing.T) {
182
197
wantMsg : "" ,
183
198
},
184
199
{
185
- name : "required and default" ,
200
+ name : "required default" ,
186
201
comments : []string {"+k8s:required" , "+default=somevalue" },
187
- wantMsg : "conflicting tags: {+k8s:required, +default}" ,
202
+ wantMsg : `conflicting tags: {\+default, \+k8s:required}` ,
203
+ },
204
+ {
205
+ name : "default required" ,
206
+ comments : []string {"+default=somevalue" , "+k8s:required" },
207
+ wantMsg : `conflicting tags: {\+default, \+k8s:required}` ,
188
208
},
189
209
{
190
- name : "required, empty, default" ,
210
+ name : "required empty default" ,
191
211
comments : []string {"+k8s:required" , "" , "+default=somevalue" },
192
- wantMsg : "conflicting tags: {+k8s:required, +default}" ,
212
+ wantMsg : `conflicting tags: {\+default, \+k8s:required}` ,
213
+ },
214
+ {
215
+ name : "empty default empty empty required empty" ,
216
+ comments : []string {"" , "+default=somevalue" , "" , "" , "+k8s:required" , "" },
217
+ wantMsg : `conflicting tags: {\+default, \+k8s:required}` ,
193
218
},
194
219
}
195
220
196
221
for _ , tt := range tests {
197
222
t .Run (tt .name , func (t * testing.T ) {
198
- msg , _ := ruleRequiredAndDefault (tt .comments )
199
- if msg != tt .wantMsg {
200
- t .Errorf ("ruleRequiredAndDefault() msg = %v, wantMsg %v" , msg , tt .wantMsg )
223
+ msg , err := ruleRequiredAndDefault (tt .comments )
224
+ if err != nil {
225
+ t .Errorf ("unexpected error: %v" , err )
226
+ } else if tt .wantMsg != "" {
227
+ re := regexp .MustCompile (tt .wantMsg )
228
+ if ! re .MatchString (msg ) {
229
+ t .Errorf ("message:\n \t %s\n does not match:\n \t %s" , msg , re .String ())
230
+ }
201
231
}
202
232
})
203
233
}
@@ -209,7 +239,6 @@ func TestConflictingTagsRule(t *testing.T) {
209
239
comments []string
210
240
tags []string
211
241
wantMsg string
212
- wantErr bool
213
242
}{
214
243
{
215
244
name : "no comments" ,
@@ -227,32 +256,32 @@ func TestConflictingTagsRule(t *testing.T) {
227
256
name : "tag1, empty, tag2" ,
228
257
comments : []string {"+tag1" , "" , "+tag2" },
229
258
tags : []string {"+tag1" , "+tag2" },
230
- wantMsg : " conflicting tags: {+tag1, +tag2}" ,
259
+ wantMsg : ` conflicting tags: {\ +tag1, \ +tag2}` ,
231
260
},
232
261
{
233
- name : "3 tags" ,
234
- comments : []string {"tag1 " , "+tag2 " , "+tag3 =value" },
262
+ name : "3 lines 2 tags match " ,
263
+ comments : []string {"tag3 " , "+tag1 " , "+tag2 =value" },
235
264
tags : []string {"+tag1" , "+tag2" , "+tag3" },
236
- wantMsg : " conflicting tags: {+tag1, +tag2, +tag3}" ,
265
+ wantMsg : ` conflicting tags: {\ +tag1, \ +tag2}` ,
237
266
},
238
267
{
239
- name : "less than 2 tags" ,
240
- comments : []string {"+tag1" },
241
- tags : []string {"+tag1" },
242
- wantMsg : "" ,
243
- wantErr : true ,
268
+ name : "3 tags all match" ,
269
+ comments : []string {"+tag3" , "+tag1" , "+tag2=value" },
270
+ tags : []string {"+tag1" , "+tag2" , "+tag3" },
271
+ wantMsg : `conflicting tags: {\+tag1, \+tag2, \+tag3}` ,
244
272
},
245
273
}
246
274
247
275
for _ , tt := range tests {
248
276
t .Run (tt .name , func (t * testing.T ) {
249
- msg , err := conflictingTagsRule (tt .comments , tt .tags ... )
250
- if (err != nil ) != tt .wantErr {
251
- t .Errorf ("conflictingTagsRule() error = %v, wantErr %v" , err , tt .wantErr )
252
- return
253
- }
254
- if msg != tt .wantMsg {
255
- t .Errorf ("conflictingTagsRule() msg = %v, wantMsg %v" , msg , tt .wantMsg )
277
+ msg , err := conflictingTagsRule ("test" , tt .tags ... )(tt .comments )
278
+ if err != nil {
279
+ t .Errorf ("unexpected error: %v" , err )
280
+ } else if tt .wantMsg != "" {
281
+ re := regexp .MustCompile (tt .wantMsg )
282
+ if ! re .MatchString (msg ) {
283
+ t .Errorf ("message:\n \t %s\n does not match:\n \t %s" , msg , re .String ())
284
+ }
256
285
}
257
286
})
258
287
}
0 commit comments