Skip to content

Commit 9382caf

Browse files
authored
feat: Add function Nil, NotNil (#3)
1 parent e79d9c5 commit 9382caf

File tree

4 files changed

+25
-15
lines changed

4 files changed

+25
-15
lines changed

README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Forked from [earthboundkid/be](https://github.com/earthboundkid/be), Inspired by
77
## Features
88

99
- Simple and readable test assertions using generics
10-
- Built-in helpers for common cases like `test.NilErr` and `test.In`
10+
- Built-in helpers for common cases like `test.Nil` and `test.Contains`
1111
- Fail fast by default but easily switch to relaxed with `test.Relaxed(t)`
1212
- Helpers for testing against golden files with the testfile subpackage
1313
- No dependencies: just uses standard library
@@ -45,13 +45,13 @@ Handle errors:
4545

4646
```go
4747
var err error
48-
test.NilErr(t, err) // good
49-
test.NotZero(t, err) // bad
48+
test.Nil(t, err) // good
49+
test.NotNil(t, err) // bad
5050
// t.Fatal("got: <nil>")
5151
err = errors.New("(O_o)")
52-
test.NilErr(t, err) // bad
52+
test.Nil(t, err) // bad
5353
// t.Fatal("got: (O_o)")
54-
test.NotZero(t, err) // good
54+
test.NotNil(t, err) // good
5555
```
5656

5757
Check substring containment:

test.go

+12-4
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,19 @@ func isZero[T any](v T) bool {
6666
}
6767
}
6868

69-
// NilErr calls t.Fatalf if err is not nil.
70-
func NilErr(t testing.TB, err error) {
69+
// Nil calls t.Fatalf if v is not nil.
70+
func Nil(t testing.TB, v any) {
7171
t.Helper()
72-
if err != nil {
73-
t.Fatalf("got: %v", err)
72+
if v != nil {
73+
t.Fatalf("got: %v", v)
74+
}
75+
}
76+
77+
// NotNil calls t.Fatalf if v is nil.
78+
func NotNil(t testing.TB, v any) {
79+
t.Helper()
80+
if v == nil {
81+
t.Fatalf("got: %v", v)
7482
}
7583
}
7684

test_example_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ func Example() {
2121
test.AllEqual(t, []int{3, 2, 1}, s) // bad
2222

2323
var err error
24-
test.NilErr(t, err) // good
25-
test.NotZero(t, err) // bad
24+
test.Nil(t, err) // good
25+
test.NotNil(t, err) // bad
2626
err = errors.New("(O_o)")
27-
test.NilErr(t, err) // bad
28-
test.NotZero(t, err) // good
27+
test.Nil(t, err) // bad
28+
test.NotNil(t, err) // good
2929

3030
type mytype string
3131
var mystring mytype = "hello, world"

test_test.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ func Test(t *testing.T) {
4040
beOkay(func(tb testing.TB) { test.Zero(tb, time.Time{}.Local()) })
4141
beOkay(func(tb testing.TB) { test.Zero(tb, []string(nil)) })
4242
beOkay(func(tb testing.TB) { test.NotZero(tb, []string{""}) })
43-
beOkay(func(tb testing.TB) { test.NilErr(tb, nil) })
43+
beOkay(func(tb testing.TB) { test.Nil(tb, nil) })
44+
beOkay(func(tb testing.TB) { test.NotNil(tb, errors.New("")) })
4445
beOkay(func(tb testing.TB) { test.True(tb, true) })
4546
beOkay(func(tb testing.TB) { test.False(tb, false) })
4647
beBad := func(callback func(tb testing.TB)) {
@@ -59,7 +60,8 @@ func Test(t *testing.T) {
5960
beBad(func(tb testing.TB) { test.NotZero(tb, time.Time{}.Local()) })
6061
beBad(func(tb testing.TB) { test.Zero(tb, []string{""}) })
6162
beBad(func(tb testing.TB) { test.NotZero(tb, []string(nil)) })
62-
beBad(func(tb testing.TB) { test.NilErr(tb, errors.New("")) })
63+
beBad(func(tb testing.TB) { test.Nil(tb, errors.New("")) })
64+
beBad(func(tb testing.TB) { test.NotNil(tb, nil) })
6365
beBad(func(tb testing.TB) { test.True(tb, false) })
6466
beBad(func(tb testing.TB) { test.False(tb, true) })
6567
}

0 commit comments

Comments
 (0)