Skip to content

Commit

Permalink
feat: use %+v for displaying errors
Browse files Browse the repository at this point in the history
Some error tracing libraries use this to write the full trace, which is
useful when debugging.
  • Loading branch information
alecthomas committed Mar 27, 2024
1 parent 4606ea7 commit 5491cb3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
6 changes: 3 additions & 3 deletions assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func IsError(t testing.TB, err, target error, msgAndArgs ...any) {
return
}
t.Helper()
t.Fatal(formatMsgAndArgs(fmt.Sprintf("Error tree %q should contain error %q", err, target), msgAndArgs...))
t.Fatal(formatMsgAndArgs(fmt.Sprintf("Error tree %+v should contain error %q", err, target), msgAndArgs...))
}

// NotIsError asserts than no error in "err"'s tree matches "target".
Expand All @@ -157,7 +157,7 @@ func NotIsError(t testing.TB, err, target error, msgAndArgs ...any) {
return
}
t.Helper()
t.Fatal(formatMsgAndArgs(fmt.Sprintf("Error tree %q should NOT contain error %q", err, target), msgAndArgs...))
t.Fatal(formatMsgAndArgs(fmt.Sprintf("Error tree %+v should NOT contain error %q", err, target), msgAndArgs...))
}

// Error asserts that an error is not nil.
Expand All @@ -176,7 +176,7 @@ func NoError(t testing.TB, err error, msgAndArgs ...any) {
}
t.Helper()
msg := formatMsgAndArgs("Did not expect an error but got:", msgAndArgs...)
t.Fatalf("%s\n%s", msg, err)
t.Fatalf("%s\n%+v", msg, err)
}

// True asserts that an expression is true.
Expand Down
18 changes: 18 additions & 0 deletions assert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,24 @@ func TestEqualError(t *testing.T) {
})
}

func TestError(t *testing.T) {
assertOk(t, "Error", func(t testing.TB) {
Error(t, fmt.Errorf("hello"))
})
assertFail(t, "Nil", func(t testing.TB) {
Error(t, nil)
})
}

func TestNoError(t *testing.T) {
assertOk(t, "Nil", func(t testing.TB) {
NoError(t, nil)
})
assertFail(t, "Error", func(t testing.TB) {
NoError(t, fmt.Errorf("hello"))
})
}

func TestZero(t *testing.T) {
assertOk(t, "Struct", func(t testing.TB) {
Zero(t, Data{})
Expand Down

0 comments on commit 5491cb3

Please sign in to comment.