@@ -207,16 +207,14 @@ func (a Assertable) Assert(t *testing.T, option ...Assertion) {
207
207
slog .F ("duration" , cmdResult .Duration ),
208
208
)
209
209
210
- for _ , o := range option {
211
- o . Valid (t , & cmdResult )
210
+ for _ , assertion := range option {
211
+ assertion (t , & cmdResult )
212
212
}
213
213
}
214
214
215
215
// Assertion specifies an assertion on the given CommandResult.
216
- // Pass custom Assertion types to cover special cases.
217
- type Assertion interface {
218
- Valid (t * testing.T , r * CommandResult )
219
- }
216
+ // Pass custom Assertion functions to cover special cases.
217
+ type Assertion func (t * testing.T , r * CommandResult )
220
218
221
219
// Named is an optional extension of Assertion that provides a helpful label
222
220
// to *testing.T
@@ -231,20 +229,6 @@ type CommandResult struct {
231
229
Duration time.Duration
232
230
}
233
231
234
- type simpleFuncAssert struct {
235
- valid func (t * testing.T , r * CommandResult )
236
- name string
237
- }
238
-
239
- func (s simpleFuncAssert ) Valid (t * testing.T , r * CommandResult ) {
240
- slog .Helper ()
241
- s .valid (t , r )
242
- }
243
-
244
- func (s simpleFuncAssert ) Name () string {
245
- return s .name
246
- }
247
-
248
232
// Success asserts that the command exited with an exit code of 0
249
233
func Success () Assertion {
250
234
slog .Helper ()
@@ -253,92 +237,68 @@ func Success() Assertion {
253
237
254
238
// Error asserts that the command exited with a nonzero exit code
255
239
func Error () Assertion {
256
- return simpleFuncAssert {
257
- valid : func (t * testing.T , r * CommandResult ) {
258
- slog .Helper ()
259
- assert .True (t , "exit code is nonzero" , r .ExitCode != 0 )
260
- },
261
- name : fmt .Sprintf ("error" ),
240
+ return func (t * testing.T , r * CommandResult ) {
241
+ slog .Helper ()
242
+ assert .True (t , "exit code is nonzero" , r .ExitCode != 0 )
262
243
}
263
244
}
264
245
265
246
// ExitCodeIs asserts that the command exited with the given code
266
247
func ExitCodeIs (code int ) Assertion {
267
- return simpleFuncAssert {
268
- valid : func (t * testing.T , r * CommandResult ) {
269
- slog .Helper ()
270
- assert .Equal (t , "exit code is as expected" , code , r .ExitCode )
271
- },
272
- name : fmt .Sprintf ("exitcode" ),
248
+ return func (t * testing.T , r * CommandResult ) {
249
+ slog .Helper ()
250
+ assert .Equal (t , "exit code is as expected" , code , r .ExitCode )
273
251
}
274
252
}
275
253
276
254
// StdoutEmpty asserts that the command did not write any data to Stdout
277
255
func StdoutEmpty () Assertion {
278
- return simpleFuncAssert {
279
- valid : func (t * testing.T , r * CommandResult ) {
280
- slog .Helper ()
281
- empty (t , "stdout" , r .Stdout )
282
- },
283
- name : fmt .Sprintf ("stdout-empty" ),
256
+ return func (t * testing.T , r * CommandResult ) {
257
+ slog .Helper ()
258
+ empty (t , "stdout" , r .Stdout )
284
259
}
285
260
}
286
261
287
262
// GetResult offers an escape hatch from tcli
288
263
// The pointer passed as "result" will be assigned to the command's *CommandResult
289
264
func GetResult (result * * CommandResult ) Assertion {
290
- return simpleFuncAssert {
291
- valid : func (t * testing.T , r * CommandResult ) {
292
- slog .Helper ()
293
- empty (t , "stdout" , r .Stdout )
294
- * result = r
295
- },
296
- name : "get-result" ,
265
+ return func (t * testing.T , r * CommandResult ) {
266
+ slog .Helper ()
267
+ empty (t , "stdout" , r .Stdout )
268
+ * result = r
297
269
}
298
270
}
299
271
300
272
// StderrEmpty asserts that the command did not write any data to Stderr
301
273
func StderrEmpty () Assertion {
302
- return simpleFuncAssert {
303
- valid : func (t * testing.T , r * CommandResult ) {
304
- slog .Helper ()
305
- empty (t , "stderr" , r .Stderr )
306
- },
307
- name : fmt .Sprintf ("stderr-empty" ),
274
+ return func (t * testing.T , r * CommandResult ) {
275
+ slog .Helper ()
276
+ empty (t , "stderr" , r .Stderr )
308
277
}
309
278
}
310
279
311
280
// StdoutMatches asserts that Stdout contains a substring which matches the given regexp
312
281
func StdoutMatches (pattern string ) Assertion {
313
- return simpleFuncAssert {
314
- valid : func (t * testing.T , r * CommandResult ) {
315
- slog .Helper ()
316
- matches (t , "stdout" , pattern , r .Stdout )
317
- },
318
- name : fmt .Sprintf ("stdout-matches" ),
282
+ return func (t * testing.T , r * CommandResult ) {
283
+ slog .Helper ()
284
+ matches (t , "stdout" , pattern , r .Stdout )
319
285
}
320
286
}
321
287
322
288
// StderrMatches asserts that Stderr contains a substring which matches the given regexp
323
289
func StderrMatches (pattern string ) Assertion {
324
- return simpleFuncAssert {
325
- valid : func (t * testing.T , r * CommandResult ) {
326
- slog .Helper ()
327
- matches (t , "stderr" , pattern , r .Stderr )
328
- },
329
- name : fmt .Sprintf ("stderr-matches" ),
290
+ return func (t * testing.T , r * CommandResult ) {
291
+ slog .Helper ()
292
+ matches (t , "stderr" , pattern , r .Stderr )
330
293
}
331
294
}
332
295
333
296
// CombinedMatches asserts that either Stdout or Stderr a substring which matches the given regexp
334
297
func CombinedMatches (pattern string ) Assertion {
335
- return simpleFuncAssert {
336
- valid : func (t * testing.T , r * CommandResult ) {
337
- slog .Helper ()
338
- StdoutMatches (pattern ).Valid (t , r )
339
- StderrMatches (pattern ).Valid (t , r )
340
- },
341
- name : fmt .Sprintf ("combined-matches" ),
298
+ return func (t * testing.T , r * CommandResult ) {
299
+ slog .Helper ()
300
+ StdoutMatches (pattern )(t , r )
301
+ StderrMatches (pattern )(t , r )
342
302
}
343
303
}
344
304
@@ -371,32 +331,26 @@ func empty(t *testing.T, name string, a []byte) {
371
331
372
332
// DurationLessThan asserts that the command completed in less than the given duration
373
333
func DurationLessThan (dur time.Duration ) Assertion {
374
- return simpleFuncAssert {
375
- valid : func (t * testing.T , r * CommandResult ) {
376
- slog .Helper ()
377
- if r .Duration > dur {
378
- slogtest .Fatal (t , "duration longer than expected" ,
379
- slog .F ("expected_less_than" , dur .String ),
380
- slog .F ("actual" , r .Duration .String ()),
381
- )
382
- }
383
- },
384
- name : fmt .Sprintf ("duration-lessthan" ),
334
+ return func (t * testing.T , r * CommandResult ) {
335
+ slog .Helper ()
336
+ if r .Duration > dur {
337
+ slogtest .Fatal (t , "duration longer than expected" ,
338
+ slog .F ("expected_less_than" , dur .String ),
339
+ slog .F ("actual" , r .Duration .String ()),
340
+ )
341
+ }
385
342
}
386
343
}
387
344
388
345
// DurationGreaterThan asserts that the command completed in greater than the given duration
389
346
func DurationGreaterThan (dur time.Duration ) Assertion {
390
- return simpleFuncAssert {
391
- valid : func (t * testing.T , r * CommandResult ) {
392
- slog .Helper ()
393
- if r .Duration < dur {
394
- slogtest .Fatal (t , "duration shorter than expected" ,
395
- slog .F ("expected_greater_than" , dur .String ),
396
- slog .F ("actual" , r .Duration .String ()),
397
- )
398
- }
399
- },
400
- name : fmt .Sprintf ("duration-greaterthan" ),
347
+ return func (t * testing.T , r * CommandResult ) {
348
+ slog .Helper ()
349
+ if r .Duration < dur {
350
+ slogtest .Fatal (t , "duration shorter than expected" ,
351
+ slog .F ("expected_greater_than" , dur .String ),
352
+ slog .F ("actual" , r .Duration .String ()),
353
+ )
354
+ }
401
355
}
402
356
}
0 commit comments