From 15661541168174c6446d17edcd9148803886c68f Mon Sep 17 00:00:00 2001 From: Tom Fleet Date: Thu, 8 Aug 2024 09:00:33 +0100 Subject: [PATCH] Improve the output from test functions (#33) --- test.go | 45 ++++++++++++++++++++++++++++----------------- test_test.go | 39 ++++++++++++++++++++++----------------- 2 files changed, 50 insertions(+), 34 deletions(-) diff --git a/test.go b/test.go index 5a03eb9..cafcca9 100644 --- a/test.go +++ b/test.go @@ -25,7 +25,7 @@ const floatEqualityThreshold = 1e-8 func Equal[T comparable](t testing.TB, got, want T) { t.Helper() if got != want { - t.Fatalf("\nGot:\t%+v\nWanted:\t%+v\n", got, want) + t.Fatalf("\nNot Equal\n---------\nGot:\t%+v\nWanted:\t%+v\n", got, want) } } @@ -37,10 +37,15 @@ func Equal[T comparable](t testing.TB, got, want T) { // test.NearlyEqual(t, 3.0000001, 3.0) // Fails, too different func NearlyEqual[T ~float32 | ~float64](t testing.TB, got, want T) { t.Helper() - // TODO: Message here could be better, we should say how far away it was and that - // it exceeds the float equality threshold - if math.Abs(float64(got-want)) >= floatEqualityThreshold { - t.Fatalf("\nGot:\t%v\nWanted:\t%v\n", got, want) + diff := math.Abs(float64(got - want)) + if diff >= floatEqualityThreshold { + t.Fatalf( + "\nNot NearlyEqual\n---------------\nGot:\t%v\nWanted:\t%v\n\nDifference %v exceeds maximum tolerance of %v\n", + got, + want, + diff, + floatEqualityThreshold, + ) } } @@ -50,9 +55,12 @@ func NearlyEqual[T ~float32 | ~float64](t testing.TB, got, want T) { // The comparator should return true if the two items should be considered equal. func EqualFunc[T any](t testing.TB, got, want T, equal func(a, b T) bool) { t.Helper() - // TODO: Better message saying equal returned false if !equal(got, want) { - t.Fatalf("\nGot:\t%+v\nWanted:\t%+v\n", got, want) + t.Fatalf( + "\nNot Equal\n---------\nGot:\t%+v\nWanted:\t%+v\n\nequal(got, want) returned false\n", + got, + want, + ) } } @@ -74,7 +82,7 @@ func NotEqual[T comparable](t testing.TB, got, want T) { func NotEqualFunc[T any](t testing.TB, got, want T, equal func(a, b T) bool) { t.Helper() if equal(got, want) { - t.Fatalf("\nValues were equal:\t%+v\n", got) + t.Fatalf("\nValues were equal:\t%+v\n\nequal(got, want) returned true\n", got) } } @@ -85,7 +93,7 @@ func NotEqualFunc[T any](t testing.TB, got, want T, equal func(a, b T) bool) { func Ok(t testing.TB, err error) { t.Helper() if err != nil { - t.Fatalf("\nGot error:\t%v\nWanted:\tnil\n", err) + t.Fatalf("\nNot Ok\n------\nGot error:\t%v\n", err) } } @@ -96,7 +104,7 @@ func Ok(t testing.TB, err error) { func Err(t testing.TB, err error) { t.Helper() if err == nil { - t.Fatalf("Error was nil\n") + t.Fatalf("\nNot Err\n-------\nError was nil\n") } } @@ -113,7 +121,7 @@ func Err(t testing.TB, err error) { func WantErr(t testing.TB, err error, want bool) { t.Helper() if (err != nil) != want { - t.Fatalf("\nGot error:\t%v\nWanted error:\t%v\n", err, want) + t.Fatalf("\nWantErr\n-------\nGot error:\t%v\nWanted error:\t%v\n", err, want) } } @@ -123,9 +131,8 @@ func WantErr(t testing.TB, err error, want bool) { // test.True(t, false) // Fails func True(t testing.TB, v bool) { t.Helper() - // TODO: Newline consistency if !v { - t.Fatalf("\nGot:\t%v\nWanted:\t%v", v, true) + t.Fatalf("\nNot True\n--------\nGot:\t%v\n", v) } } @@ -136,16 +143,16 @@ func True(t testing.TB, v bool) { func False(t testing.TB, v bool) { t.Helper() if v { - t.Fatalf("\nGot:\t%v\nWanted:\t%v", v, false) + t.Fatalf("\nNot False\n--------\nGot:\t%v\n", v) } } // Diff fails if got != want and provides a rich diff. func Diff(t testing.TB, got, want any) { - // TODO: Nicer output for diff, don't like the +got -want thing, also newline consistency + // TODO: Nicer output for diff, don't like the +got -want thing t.Helper() if diff := cmp.Diff(want, got); diff != "" { - t.Fatalf("Mismatch (-want, +got):\n%s", diff) + t.Fatalf("\nMismatch (-want, +got):\n%s\n", diff) } } @@ -153,7 +160,11 @@ func Diff(t testing.TB, got, want any) { func DeepEqual(t testing.TB, got, want any) { t.Helper() if !reflect.DeepEqual(got, want) { - t.Fatalf("\nGot:\t%+v\nWanted:\t%+v\n", got, want) + t.Fatalf( + "\nNot Equal\n---------\nGot:\t%+v\nWanted:\t%+v\n\nreflect.DeepEqual(got, want) returned false\n", + got, + want, + ) } } diff --git a/test_test.go b/test_test.go index e34248f..b131260 100644 --- a/test_test.go +++ b/test_test.go @@ -51,7 +51,7 @@ func TestPassFail(t *testing.T) { test.Equal(tb, "apples", "oranges") }, wantFail: true, - wantOut: "\nGot:\tapples\nWanted:\toranges\n", + wantOut: "\nNot Equal\n---------\nGot:\tapples\nWanted:\toranges\n", }, { name: "equal int pass", @@ -67,7 +67,7 @@ func TestPassFail(t *testing.T) { test.Equal(tb, 1, 42) }, wantFail: true, - wantOut: "\nGot:\t1\nWanted:\t42\n", + wantOut: "\nNot Equal\n---------\nGot:\t1\nWanted:\t42\n", }, { name: "nearly equal pass", @@ -83,7 +83,7 @@ func TestPassFail(t *testing.T) { test.NearlyEqual(tb, 3.0000001, 3.0) }, wantFail: true, - wantOut: "\nGot:\t3.0000001\nWanted:\t3\n", + wantOut: "\nNot NearlyEqual\n---------------\nGot:\t3.0000001\nWanted:\t3\n\nDifference 9.999999983634211e-08 exceeds maximum tolerance of 1e-08\n", }, { name: "not equal string pass", @@ -131,7 +131,7 @@ func TestPassFail(t *testing.T) { test.Ok(tb, errors.New("uh oh")) }, wantFail: true, - wantOut: "\nGot error:\tuh oh\nWanted:\tnil\n", + wantOut: "\nNot Ok\n------\nGot error:\tuh oh\n", }, { name: "err pass", @@ -147,7 +147,7 @@ func TestPassFail(t *testing.T) { test.Err(tb, nil) }, wantFail: true, - wantOut: "Error was nil\n", + wantOut: "\nNot Err\n-------\nError was nil\n", }, { name: "true pass", @@ -163,7 +163,7 @@ func TestPassFail(t *testing.T) { test.True(tb, false) }, wantFail: true, - wantOut: "\nGot:\tfalse\nWanted:\ttrue", + wantOut: "\nNot True\n--------\nGot:\tfalse\n", }, { name: "false pass", @@ -179,7 +179,7 @@ func TestPassFail(t *testing.T) { test.False(tb, true) }, wantFail: true, - wantOut: "\nGot:\ttrue\nWanted:\tfalse", + wantOut: "\nNot False\n--------\nGot:\ttrue\n", }, { name: "equal func pass", @@ -201,7 +201,7 @@ func TestPassFail(t *testing.T) { test.EqualFunc(tb, "word", "word", rubbishEqual) }, wantFail: true, - wantOut: "\nGot:\tword\nWanted:\tword\n", + wantOut: "\nNot Equal\n---------\nGot:\tword\nWanted:\tword\n\nequal(got, want) returned false\n", }, { name: "not equal func pass", @@ -223,7 +223,7 @@ func TestPassFail(t *testing.T) { test.NotEqualFunc(tb, "word", "different word", rubbishNotEqual) }, wantFail: true, - wantOut: "\nValues were equal:\tword\n", + wantOut: "\nValues were equal:\tword\n\nequal(got, want) returned true\n", }, { name: "deep equal pass", @@ -245,7 +245,7 @@ func TestPassFail(t *testing.T) { test.DeepEqual(tb, a, b) }, wantFail: true, - wantOut: "\nGot:\t[a b c]\nWanted:\t[d e f]\n", + wantOut: "\nNot Equal\n---------\nGot:\t[a b c]\nWanted:\t[d e f]\n\nreflect.DeepEqual(got, want) returned false\n", }, { name: "want err pass when got and wanted", @@ -261,7 +261,7 @@ func TestPassFail(t *testing.T) { test.WantErr(tb, errors.New("uh oh"), false) // Didn't want an error but got one }, wantFail: true, - wantOut: "\nGot error:\tuh oh\nWanted error:\tfalse\n", + wantOut: "\nWantErr\n-------\nGot error:\tuh oh\nWanted error:\tfalse\n", }, { name: "want err pass when not got and not wanted", @@ -277,7 +277,7 @@ func TestPassFail(t *testing.T) { test.WantErr(tb, nil, true) // Wanted an error but didn't get one }, wantFail: true, - wantOut: "\nGot error:\t\nWanted error:\ttrue\n", + wantOut: "\nWantErr\n-------\nGot error:\t\nWanted error:\ttrue\n", }, { name: "file pass", @@ -302,7 +302,7 @@ func TestPassFail(t *testing.T) { }, wantFail: true, wantOut: fmt.Sprintf( - "Mismatch (-want, +got):\n%s", + "\nMismatch (-want, +got):\n%s\n", cmp.Diff("hello there", "hello"), ), // Output equivalent to diff }, @@ -325,7 +325,7 @@ func TestPassFail(t *testing.T) { }, wantFail: true, wantOut: fmt.Sprintf( - "Mismatch (-want, +got):\n%s", + "\nMismatch (-want, +got):\n%s\n", cmp.Diff([]string{"not", "me"}, []string{"hello", "there"}), ), // Output equivalent to diff }, @@ -346,8 +346,8 @@ func TestPassFail(t *testing.T) { tt.testFunc(tb) if tb.failed != tt.wantFail { - t.Errorf( - "%s failure mismatch. failed: %v, wanted failure: %v", + t.Fatalf( + "\n%s failure mismatch\n--------------\nfailed:\t%v\nwanted failure:\t%v\n", tt.name, tb.failed, tt.wantFail, @@ -355,7 +355,12 @@ func TestPassFail(t *testing.T) { } if got := buf.String(); got != tt.wantOut { - t.Errorf("%s output mismatch. got: %s, wanted %s", tt.name, got, tt.wantOut) + t.Errorf( + "\n%s output mismatch\n---------------\nGot:\t%s\nWanted:\t%s\n", + tt.name, + got, + tt.wantOut, + ) } }) }