Skip to content

Commit a48909f

Browse files
authored
Test helpers (#32)
1 parent 33f9d45 commit a48909f

File tree

1 file changed

+37
-44
lines changed

1 file changed

+37
-44
lines changed

acmd_test.go

Lines changed: 37 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"fmt"
88
"io"
99
"os"
10+
"reflect"
1011
"sort"
1112
"strings"
1213
"testing"
@@ -62,12 +63,8 @@ func TestRunner(t *testing.T) {
6263
Output: buf,
6364
})
6465

65-
if err := r.Run(); err != nil {
66-
t.Fatal(err)
67-
}
68-
if got := buf.String(); got != "for" {
69-
t.Fatalf("want %q got %q", "for", got)
70-
}
66+
failIfErr(t, r.Run())
67+
mustEqual(t, buf.String(), "for")
7168
}
7269

7370
func TestRunnerMustSetDefaults(t *testing.T) {
@@ -81,16 +78,13 @@ func TestRunnerMustSetDefaults(t *testing.T) {
8178
})
8279

8380
err := r.Run()
84-
if err == nil {
85-
t.Fatal()
86-
}
81+
failIfOk(t, err)
82+
8783
if errStr := err.Error(); !strings.Contains(errStr, `no such command "runner"`) {
8884
t.Fatal(err)
8985
}
9086

91-
if r.cfg.AppName != app {
92-
t.Fatalf("want %q got %q", app, r.cfg.AppName)
93-
}
87+
mustEqual(t, r.cfg.AppName, app)
9488
if r.ctx == nil {
9589
t.Fatal("context must be set")
9690
}
@@ -119,13 +113,8 @@ func TestRunnerWithoutArgs(t *testing.T) {
119113
})
120114

121115
err := r.Run()
122-
if err == nil {
123-
t.Fatal("must be error")
124-
}
125-
want := "no args provided"
126-
if got := err.Error(); got != want {
127-
t.Fatalf("got %q want %q", got, want)
128-
}
116+
failIfOk(t, err)
117+
mustEqual(t, err.Error(), "no args provided")
129118
}
130119

131120
func TestRunnerMustSortCommands(t *testing.T) {
@@ -145,9 +134,7 @@ func TestRunnerMustSortCommands(t *testing.T) {
145134
Args: []string{"./someapp", "foo"},
146135
})
147136

148-
if err := r.Run(); err != nil {
149-
t.Fatal(err)
150-
}
137+
failIfErr(t, r.Run())
151138

152139
sort.SliceIsSorted(r.cmds, func(i, j int) bool {
153140
return r.cmds[i].Name < r.cmds[j].Name
@@ -183,15 +170,12 @@ func TestRunnerJustExit(t *testing.T) {
183170
})
184171
r.Exit(nil)
185172

186-
if !exitDone {
187-
t.Fatal("must be done")
188-
}
173+
mustEqual(t, exitDone, true)
189174
exitDone = false
190175

191176
r.Exit(errors.New("oops"))
192-
if !exitDone {
193-
t.Fatal("must be done")
194-
}
177+
mustEqual(t, exitDone, true)
178+
195179
got := buf.String()
196180
if !strings.Contains(got, "exit-test: oops") {
197181
t.Fatal(got)
@@ -331,9 +315,7 @@ func TestRunner_suggestCommand(t *testing.T) {
331315
t.Fatal(err)
332316
}
333317

334-
if got := buf.String(); got != tc.want {
335-
t.Fatalf("want %q got %q", tc.want, got)
336-
}
318+
mustEqual(t, buf.String(), tc.want)
337319
}
338320
}
339321

@@ -348,9 +330,7 @@ func TestHasHelpFlag(t *testing.T) {
348330
{[]string{"--help", "-h", "baz"}, true},
349331
}
350332
for _, tc := range testCases {
351-
if got := HasHelpFlag(tc.args); got != tc.hasHelp {
352-
t.Fatalf("got %v, want %v", got, tc.hasHelp)
353-
}
333+
mustEqual(t, HasHelpFlag(tc.args), tc.hasHelp)
354334
}
355335
}
356336

@@ -366,9 +346,7 @@ func TestCommand_IsHidden(t *testing.T) {
366346
AppName: "myapp",
367347
Output: buf,
368348
})
369-
if err := r.Run(); err != nil {
370-
t.Fatal(err)
371-
}
349+
failIfErr(t, r.Run())
372350

373351
if strings.Contains(buf.String(), "foo") {
374352
t.Fatal("should not show foo")
@@ -396,9 +374,7 @@ func TestExit(t *testing.T) {
396374
})
397375

398376
err := r.Run()
399-
if err == nil {
400-
t.Fatal("must not be nil")
401-
}
377+
failIfOk(t, err)
402378

403379
var gotStatus int
404380
doExitOld := func(code int) {
@@ -410,10 +386,27 @@ func TestExit(t *testing.T) {
410386

411387
r.Exit(err)
412388

413-
if gotStatus != wantStatus {
414-
t.Fatalf("got %d want %d", gotStatus, wantStatus)
389+
mustEqual(t, gotStatus, wantStatus)
390+
mustEqual(t, buf.String(), wantOutput)
391+
}
392+
393+
func failIfOk(t testing.TB, err error) {
394+
t.Helper()
395+
if err == nil {
396+
t.Fail()
415397
}
416-
if got := buf.String(); got != wantOutput {
417-
t.Fatalf("got %q want %q", got, wantOutput)
398+
}
399+
400+
func failIfErr(t testing.TB, err error) {
401+
t.Helper()
402+
if err != nil {
403+
t.Fatal(err)
404+
}
405+
}
406+
407+
func mustEqual(t testing.TB, got, want interface{}) {
408+
t.Helper()
409+
if !reflect.DeepEqual(got, want) {
410+
t.Fatalf("\nhave %+v\nwant %+v", got, want)
418411
}
419412
}

0 commit comments

Comments
 (0)