A lightweight test helper package
0

Configure Feed

Select the types of activity you want to include in your feed.

Rename ErrIsWanted -> WantErr (#10)

authored by

Tom Fleet and committed by
GitHub
(Oct 28, 2023, 8:44 PM +0100) 709d4133 0064e316

+15 -12
+5 -2
README.md
··· 38 38 test.Ok(t, err) // Fails if err != nil 39 39 test.Err(t, err) // Fails if err == nil 40 40 41 + // Can even add context 42 + test.Ok(t, err, "doSomething went wrong") 43 + 41 44 test.True(t, true) // Passes 42 45 test.False(t, true) // Fails 43 46 ··· 104 107 ### Table Driven Tests 105 108 106 109 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 107 - *always* have to do the boolean logic in my head to make sure I got that right. Enter `test.ErrIsWanted`: 110 + *always* have to do the boolean logic in my head to make sure I got that right. Enter `test.WantErr`: 108 111 109 112 ```go 110 113 func TestTableThings(t *testing.T) { ··· 129 132 t.Run(tt.name, func(t *testing.T) { 130 133 got, err := SomeFunction() 131 134 132 - test.ErrIsWanted(t, err, tt.wantErr) 135 + test.WantErr(t, err, tt.wantErr) 133 136 test.Equal(t, got, tt.want) 134 137 }) 135 138 }
+6 -6
test.go
··· 88 88 } 89 89 } 90 90 91 - // ErrIsWanted fails if you got an error and didn't want it, or if you 91 + // WantErr fails if you got an error and didn't want it, or if you 92 92 // didn't get an error but wanted one. 93 93 // 94 94 // It simplifies checking for errors in table driven tests where on any 95 95 // iteration err may or may not be nil. 96 96 // 97 - // test.ErrIsWanted(t, errors.New("uh oh"), true) // Passes, got error when we wanted one 98 - // test.ErrIsWanted(t, errors.New("uh oh"), false) // Fails, got error but didn't want one 99 - // test.ErrIsWanted(t, nil, true) // Fails, wanted an error but didn't get one 100 - // test.ErrIsWanted(t, nil, false) // Passes, didn't want an error and didn't get one 101 - func ErrIsWanted(t testing.TB, err error, want bool) { 97 + // test.WantErr(t, errors.New("uh oh"), true) // Passes, got error when we wanted one 98 + // test.WantErr(t, errors.New("uh oh"), false) // Fails, got error but didn't want one 99 + // test.WantErr(t, nil, true) // Fails, wanted an error but didn't get one 100 + // test.WantErr(t, nil, false) // Passes, didn't want an error and didn't get one 101 + func WantErr(t testing.TB, err error, want bool) { 102 102 t.Helper() 103 103 if (err != nil) != want { 104 104 t.Fatalf("\nGot error:\t%v\nWanted error:\t%v\n", err, want)
+4 -4
test_test.go
··· 88 88 test.Diff(tb, struct{ Name string }{Name: "dave"}, struct{ Name string }{Name: "dave"}) 89 89 }, 90 90 func(tb testing.TB) { test.DeepEqual(tb, []string{"hello"}, []string{"hello"}) }, 91 - func(tb testing.TB) { test.ErrIsWanted(tb, errors.New("uh oh"), true) }, 92 - func(tb testing.TB) { test.ErrIsWanted(tb, nilErr(), false) }, 91 + func(tb testing.TB) { test.WantErr(tb, errors.New("uh oh"), true) }, 92 + func(tb testing.TB) { test.WantErr(tb, nilErr(), false) }, 93 93 } 94 94 95 95 for _, fn := range passFns { ··· 161 161 test.Diff(tb, struct{ Name string }{Name: "dave"}, struct{ Name string }{Name: "john"}) 162 162 }, 163 163 func(tb testing.TB) { test.DeepEqual(tb, []string{"hello"}, []string{"world"}) }, 164 - func(tb testing.TB) { test.ErrIsWanted(tb, errors.New("uh oh"), false) }, 165 - func(tb testing.TB) { test.ErrIsWanted(tb, nilErr(), true) }, 164 + func(tb testing.TB) { test.WantErr(tb, errors.New("uh oh"), false) }, 165 + func(tb testing.TB) { test.WantErr(tb, nilErr(), true) }, 166 166 } 167 167 168 168 for _, fn := range failFns {