Skip to content

Commit

Permalink
Improve the output from test functions (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
FollowTheProcess authored Aug 8, 2024
1 parent ff3b7f6 commit 1566154
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 34 deletions.
45 changes: 28 additions & 17 deletions test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand All @@ -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,
)
}
}

Expand All @@ -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,
)
}
}

Expand All @@ -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)
}
}

Expand All @@ -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)
}
}

Expand All @@ -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")
}
}

Expand All @@ -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)
}
}

Expand All @@ -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)
}
}

Expand All @@ -136,24 +143,28 @@ 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)
}
}

// DeepEqual fails if reflect.DeepEqual(got, want) == false.
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,
)
}
}

Expand Down
39 changes: 22 additions & 17 deletions test_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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<nil>\nWanted error:\ttrue\n",
wantOut: "\nWantErr\n-------\nGot error:\t<nil>\nWanted error:\ttrue\n",
},
{
name: "file pass",
Expand All @@ -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
},
Expand All @@ -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
},
Expand All @@ -346,16 +346,21 @@ 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,
)
}

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,
)
}
})
}
Expand Down

0 comments on commit 1566154

Please sign in to comment.