From c2a6944e3dc337003e4e4914b520f4243477fc5f Mon Sep 17 00:00:00 2001 From: Tom Fleet Date: Sat, 27 Jul 2024 19:19:46 +0100 Subject: [PATCH] Remove context arg from Ok and Err (#29) --- test.go | 25 ++++++------------------- test_test.go | 4 ---- 2 files changed, 6 insertions(+), 23 deletions(-) diff --git a/test.go b/test.go index 77614d0..441d03d 100644 --- a/test.go +++ b/test.go @@ -4,7 +4,6 @@ package test import ( "bytes" - "fmt" "io" "math" "os" @@ -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) } } diff --git a/test_test.go b/test_test.go index 01a24ee..5acd5ee 100644 --- a/test_test.go +++ b/test_test.go @@ -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) }, @@ -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") },