Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Commit 3f11516

Browse files
committed
Simplify assertion type
1 parent 969252d commit 3f11516

File tree

1 file changed

+46
-92
lines changed

1 file changed

+46
-92
lines changed

ci/tcli/tcli.go

Lines changed: 46 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -207,16 +207,14 @@ func (a Assertable) Assert(t *testing.T, option ...Assertion) {
207207
slog.F("duration", cmdResult.Duration),
208208
)
209209

210-
for _, o := range option {
211-
o.Valid(t, &cmdResult)
210+
for _, assertion := range option {
211+
assertion(t, &cmdResult)
212212
}
213213
}
214214

215215
// 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)
220218

221219
// Named is an optional extension of Assertion that provides a helpful label
222220
// to *testing.T
@@ -231,20 +229,6 @@ type CommandResult struct {
231229
Duration time.Duration
232230
}
233231

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-
248232
// Success asserts that the command exited with an exit code of 0
249233
func Success() Assertion {
250234
slog.Helper()
@@ -253,92 +237,68 @@ func Success() Assertion {
253237

254238
// Error asserts that the command exited with a nonzero exit code
255239
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)
262243
}
263244
}
264245

265246
// ExitCodeIs asserts that the command exited with the given code
266247
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)
273251
}
274252
}
275253

276254
// StdoutEmpty asserts that the command did not write any data to Stdout
277255
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)
284259
}
285260
}
286261

287262
// GetResult offers an escape hatch from tcli
288263
// The pointer passed as "result" will be assigned to the command's *CommandResult
289264
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
297269
}
298270
}
299271

300272
// StderrEmpty asserts that the command did not write any data to Stderr
301273
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)
308277
}
309278
}
310279

311280
// StdoutMatches asserts that Stdout contains a substring which matches the given regexp
312281
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)
319285
}
320286
}
321287

322288
// StderrMatches asserts that Stderr contains a substring which matches the given regexp
323289
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)
330293
}
331294
}
332295

333296
// CombinedMatches asserts that either Stdout or Stderr a substring which matches the given regexp
334297
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)
342302
}
343303
}
344304

@@ -371,32 +331,26 @@ func empty(t *testing.T, name string, a []byte) {
371331

372332
// DurationLessThan asserts that the command completed in less than the given duration
373333
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+
}
385342
}
386343
}
387344

388345
// DurationGreaterThan asserts that the command completed in greater than the given duration
389346
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+
}
401355
}
402356
}

0 commit comments

Comments
 (0)