Skip to content

Commit

Permalink
Rename ErrIsWanted -> WantErr (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
FollowTheProcess authored Oct 28, 2023
1 parent 0064e31 commit 709d413
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ func TestSomething(t *testing.T) {
test.Ok(t, err) // Fails if err != nil
test.Err(t, err) // Fails if err == nil

// Can even add context
test.Ok(t, err, "doSomething went wrong")

test.True(t, true) // Passes
test.False(t, true) // Fails

Expand Down Expand Up @@ -104,7 +107,7 @@ 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`:
*always* have to do the boolean logic in my head to make sure I got that right. Enter `test.WantErr`:

```go
func TestTableThings(t *testing.T) {
Expand All @@ -129,7 +132,7 @@ func TestTableThings(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
got, err := SomeFunction()

test.ErrIsWanted(t, err, tt.wantErr)
test.WantErr(t, err, tt.wantErr)
test.Equal(t, got, tt.want)
})
}
Expand Down
12 changes: 6 additions & 6 deletions test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,17 @@ func Err(t testing.TB, err error, context ...string) {
}
}

// ErrIsWanted fails if you got an error and didn't want it, or if you
// WantErr 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 may or may not be nil.
//
// test.ErrIsWanted(t, errors.New("uh oh"), true) // Passes, got error when we wanted one
// test.ErrIsWanted(t, errors.New("uh oh"), false) // Fails, got error but didn't want one
// test.ErrIsWanted(t, nil, true) // Fails, wanted an error but didn't get one
// test.ErrIsWanted(t, nil, false) // Passes, didn't want an error and didn't get one
func ErrIsWanted(t testing.TB, err error, want bool) {
// test.WantErr(t, errors.New("uh oh"), true) // Passes, got error when we wanted one
// test.WantErr(t, errors.New("uh oh"), false) // Fails, got error but didn't want one
// test.WantErr(t, nil, true) // Fails, wanted an error but didn't get one
// test.WantErr(t, nil, false) // Passes, didn't want an error and didn't get one
func WantErr(t testing.TB, err error, want bool) {
t.Helper()
if (err != nil) != want {
t.Fatalf("\nGot error:\t%v\nWanted error:\t%v\n", err, want)
Expand Down
8 changes: 4 additions & 4 deletions test_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,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) },
func(tb testing.TB) { test.WantErr(tb, errors.New("uh oh"), true) },
func(tb testing.TB) { test.WantErr(tb, nilErr(), false) },
}

for _, fn := range passFns {
Expand Down Expand Up @@ -161,8 +161,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) },
func(tb testing.TB) { test.WantErr(tb, errors.New("uh oh"), false) },
func(tb testing.TB) { test.WantErr(tb, nilErr(), true) },
}

for _, fn := range failFns {
Expand Down

0 comments on commit 709d413

Please sign in to comment.