Skip to content

Commit

Permalink
Remove context arg from Ok and Err (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
FollowTheProcess authored Jul 27, 2024
1 parent 3fdac1e commit c2a6944
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 23 deletions.
25 changes: 6 additions & 19 deletions test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package test

import (
"bytes"
"fmt"
"io"
"math"
"os"
Expand Down Expand Up @@ -76,37 +75,25 @@ func NotEqualFunc[T any](t testing.TB, got, want T, equal func(a, b T) bool) {
}
}

// Ok fails if err != nil, optionally adding context to the output.
// Ok fails if err != nil.
//
// err := doSomething()
// test.Ok(t, err, "Doing something")
func Ok(t testing.TB, err error, context ...string) {
func Ok(t testing.TB, err error) {
t.Helper()
var msg string
if len(context) == 0 {
msg = fmt.Sprintf("\nGot error:\t%v\nWanted:\tnil\n", err)
} else {
msg = fmt.Sprintf("\nGot error:\t%v\nWanted:\tnil\nContext:\t%s\n", err, context[0])
}
if err != nil {
t.Fatalf(msg, err)
t.Fatalf("\nGot error:\t%v\nWanted:\tnil\n", err)
}
}

// Err fails if err == nil.
//
// err := shouldReturnErr()
// test.Err(t, err, "shouldReturnErr")
func Err(t testing.TB, err error, context ...string) {
// test.Err(t, err)
func Err(t testing.TB, err error) {
t.Helper()
var msg string
if len(context) == 0 {
msg = fmt.Sprintf("Error was not nil:\t%v\n", err)
} else {
msg = fmt.Sprintf("Error was not nil:\t%v\nContext:\t%s", err, context[0])
}
if err == nil {
t.Fatalf(msg, err)
t.Fatalf("Error was not nil:\t%v\n", err)
}
}

Expand Down
4 changes: 0 additions & 4 deletions test_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,7 @@ func TestPass(t *testing.T) {
"NotEqual bool": func(tb testing.TB) { test.NotEqual(tb, true, false) },
"NotEqual float": func(tb testing.TB) { test.NotEqual(tb, 3.14, 8.67) },
"Ok nil": func(tb testing.TB) { test.Ok(tb, nil) },
"Ok with context": func(tb testing.TB) { test.Ok(tb, nil, "Something") },
"Err": func(tb testing.TB) { test.Err(tb, errors.New("uh oh")) },
"Err with context": func(tb testing.TB) { test.Err(tb, errors.New("uh oh"), "Something") },
"True": func(tb testing.TB) { test.True(tb, true) },
"False": func(tb testing.TB) { test.False(tb, false) },
"Diff int": func(tb testing.TB) { test.Diff(tb, 42, 42) },
Expand Down Expand Up @@ -148,9 +146,7 @@ func TestFail(t *testing.T) {
"NotEqual bool": func(tb testing.TB) { test.NotEqual(tb, true, true) },
"NotEqual float": func(tb testing.TB) { test.NotEqual(tb, 3.14, 3.14) },
"Ok": func(tb testing.TB) { test.Ok(tb, errors.New("uh oh")) },
"Ok with context": func(tb testing.TB) { test.Ok(tb, errors.New("uh oh"), "Something") },
"Err": func(tb testing.TB) { test.Err(tb, nilErr()) },
"Err with context": func(tb testing.TB) { test.Err(tb, nilErr(), "Something") },
"True": func(tb testing.TB) { test.True(tb, false) },
"False": func(tb testing.TB) { test.False(tb, true) },
"Diff string": func(tb testing.TB) { test.Diff(tb, "hello", "there") },
Expand Down

0 comments on commit c2a6944

Please sign in to comment.