Skip to content

Add be.OK #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ jobs:
run: go mod download

- name: Test
run: go test -v ./...
run: go test -race -v ./...
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ be.NilErr(t, err) // bad
// t.Fatal("got: (O_o)")
be.Nonzero(t, err) // good

// Can type infer results with be.OK
val1 := be.OK(someFailingFunc())(t) // bad
val2 := be.OK(someSucceedingFunc())(t) // good

be.In(t, "world", "hello, world") // good
be.In(t, "World", "hello, world") // bad
// t.Fatal("World" not in "hello, world")
Expand Down
14 changes: 14 additions & 0 deletions be.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,17 @@ func False(t testing.TB, value bool) {
t.Fatalf("got: true")
}
}

// OK is a convenience wrapper for NilErr checks.
// It works with Go's generic type inference system
// to allow inline function calling.
func OK[T any](v T, err error) func(testing.TB) T {
return func(t testing.TB) T {
t.Helper()
if err != nil {
t.Fatalf("got: %v", err)
return *new(T)
}
return v
}
}
26 changes: 26 additions & 0 deletions be_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package be_test

import (
"errors"
"fmt"

"github.com/carlmjohnson/be"
)
Expand Down Expand Up @@ -43,3 +44,28 @@ func Example() {
// "World" not in "hello, world"
// "\x00" in "\a\b\x00\r\t"
}

func ExampleOK() {
// mock *testing.T for example purposes
t := be.Relaxed(&mockingT{})

// a function that might fail
flakey := func(n int) (int, error) {
if n == 1 {
return 0, errors.New("1 is invalid")
}
return n, nil
}

// Fails
n := be.OK(flakey(1))(t)
fmt.Println("n =", n)

// Succeeds
n = be.OK(flakey(2))(t)
fmt.Println("n =", n)
// Output:
// got: 1 is invalid
// n = 0
// n = 2
}
2 changes: 2 additions & 0 deletions be_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func Test(t *testing.T) {
beOkay(func(tb testing.TB) { be.NilErr(tb, nil) })
beOkay(func(tb testing.TB) { be.True(tb, true) })
beOkay(func(tb testing.TB) { be.False(tb, false) })
beOkay(func(tb testing.TB) { be.OK(1, nil)(tb) })
beBad := func(callback func(tb testing.TB)) {
t.Helper()
var buf strings.Builder
Expand All @@ -62,4 +63,5 @@ func Test(t *testing.T) {
beBad(func(tb testing.TB) { be.NilErr(tb, errors.New("")) })
beBad(func(tb testing.TB) { be.True(tb, false) })
beBad(func(tb testing.TB) { be.False(tb, true) })
beBad(func(tb testing.TB) { be.OK(1, errors.New(""))(tb) })
}