Skip to content

Commit

Permalink
Add test.ErrIsWanted for table driven tests (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
FollowTheProcess authored Aug 15, 2023
1 parent 8e9bb45 commit e28e819
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 2 deletions.
71 changes: 71 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,77 @@ Will give you:
}
```

### Table Driven Tests

Table driven tests are great! But when you test errors too it can get a bit awkward, you have to do the `if (err != nil) != tt.wantErr` thing and I personally
*always* have to do the boolean logic in my head to make sure I got that right. Enter `test.ErrIsWanted`:

```go
func TestTableThings(t *testing.T) {
tests := []struct {
name string
want int
wantErr bool
}{
{
name: "no error",
want: 4,
wantErr: false,
},
{
name: "yes error",
want: 4,
wantErr: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := SomeFunction()

test.ErrIsWanted(t, err, tt.wantErr)
test.Equal(t, got, tt.want)
})
}
}
```

Which is basically semantically equivalent to:

```go
func TestTableThings(t *testing.T) {
tests := []struct {
name string
want int
wantErr bool
}{
{
name: "no error",
want: 4,
wantErr: false,
},
{
name: "yes error",
want: 4,
wantErr: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := SomeFunction()

if tt.wantErr {
test.Err(t, err)
} else {
test.Ok(t, err)
}
test.Equal(t, got, tt.want)
})
}
}
```

### Credits

This package was created with [copier] and the [FollowTheProcess/go_copier] project template.
Expand Down
16 changes: 14 additions & 2 deletions test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,31 @@ func Err(t testing.TB, err error) {
}
}

// ErrIsWanted fails if you got an error and didn't want it, or if you
// didn't get an error but wanted one.
//
// It simplifies checking for errors in table driven tests where on any
// iteration err could either be nil or not.
func ErrIsWanted(t testing.TB, err error, want bool) {
t.Helper()
if (err != nil) != want {
t.Fatalf("\nGot error:\t%v\nWanted error: %v\n", err, want)
}
}

// True fails if v is false.
func True(t testing.TB, v bool) {
t.Helper()
if !v {
t.Fatalf("\nGot:\t%+v\nWanted:\t%+v", v, true)
t.Fatalf("\nGot:\t%v\nWanted:\t%v", v, true)
}
}

// False fails if v is true.
func False(t testing.TB, v bool) {
t.Helper()
if v {
t.Fatalf("\nGot:\t%+v\nWanted:\t%+v", v, false)
t.Fatalf("\nGot:\t%v\nWanted:\t%v", v, false)
}
}

Expand Down
4 changes: 4 additions & 0 deletions test_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ func TestPass(t *testing.T) {
test.Diff(tb, struct{ Name string }{Name: "dave"}, struct{ Name string }{Name: "dave"})
},
func(tb testing.TB) { test.DeepEqual(tb, []string{"hello"}, []string{"hello"}) },
func(tb testing.TB) { test.ErrIsWanted(tb, errors.New("uh oh"), true) },
func(tb testing.TB) { test.ErrIsWanted(tb, nilErr(), false) },
}

for _, fn := range passFns {
Expand Down Expand Up @@ -155,6 +157,8 @@ func TestFail(t *testing.T) {
test.Diff(tb, struct{ Name string }{Name: "dave"}, struct{ Name string }{Name: "john"})
},
func(tb testing.TB) { test.DeepEqual(tb, []string{"hello"}, []string{"world"}) },
func(tb testing.TB) { test.ErrIsWanted(tb, errors.New("uh oh"), false) },
func(tb testing.TB) { test.ErrIsWanted(tb, nilErr(), true) },
}

for _, fn := range failFns {
Expand Down

0 comments on commit e28e819

Please sign in to comment.