Skip to content

Commit

Permalink
assertions: make Error helper output more consistent
Browse files Browse the repository at this point in the history
  • Loading branch information
brandondyck authored and shoenig committed Sep 10, 2024
1 parent a129663 commit 56f7a18
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
16 changes: 8 additions & 8 deletions internal/assertions/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,14 @@ func Unreachable() (s string) {

func Error(err error) (s string) {
if err == nil {
s = "expected non-nil error; is nil\n"
s = "expected non-nil error; got nil\n"
}
return
}

func EqError(err error, msg string) (s string) {
if err == nil {
s = "expected error; got nil\n"
s = "expected non-nil error; got nil\n"
return
}
e := err.Error()
Expand All @@ -151,30 +151,30 @@ func EqError(err error, msg string) (s string) {

func ErrorIs(err error, target error) (s string) {
if err == nil {
s = "expected error; got nil\n"
s = "expected non-nil error; got nil\n"
return
}
if !errors.Is(err, target) {
s = "expected errors.Is match\n"
s += bullet(" error: %v\n", err)
s += bullet("target: %v\n", target)
s += bullet(" got: %v\n", err)
}
return
}

func ErrorAs[E error, Target *E](err error, target Target) (s string) {
if err == nil {
s = "expected error; got nil\n"
s = "expected non-nil error; got nil\n"
return
}
if target == nil {
s = "expected target not to be nil"
s = "expected non-nil target; got nil\n"
return
}
if !errors.As(err, target) {
s = "expected errors.As match\n"
s += bullet(" error: %v\n", err)
s += bullet("target: %v\n", target)
s += bullet(" got: %v\n", err)
}
return
}
Expand All @@ -189,7 +189,7 @@ func NoError(err error) (s string) {

func ErrorContains(err error, sub string) (s string) {
if err == nil {
s = "expected non-nil error\n"
s = "expected non-nil error; got nil\n"
return
}
actual := err.Error()
Expand Down
10 changes: 5 additions & 5 deletions must/must_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions test_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func TestUnreachable_PS(t *testing.T) {
}

func TestError(t *testing.T) {
tc := newCase(t, `expected non-nil error; is nil`)
tc := newCase(t, `expected non-nil error; got nil`)
t.Cleanup(tc.assert)

Error(tc, nil)
Expand Down Expand Up @@ -131,7 +131,7 @@ func TestEqError_PS(t *testing.T) {
}

func TestEqError_nil(t *testing.T) {
tc := newCase(t, `expected error; got nil`)
tc := newCase(t, `expected non-nil error; got nil`)
t.Cleanup(tc.assert)

EqError(tc, nil, "blah")
Expand All @@ -156,7 +156,7 @@ func TestErrorIs_PS(t *testing.T) {
}

func TestErrorIs_nil(t *testing.T) {
tc := newCase(t, `expected error; got nil`)
tc := newCase(t, `expected non-nil error; got nil`)
t.Cleanup(tc.assert)

err := errors.New("oops")
Expand All @@ -179,15 +179,15 @@ func TestErrorAs(t *testing.T) {
}

func TestErrorAs_nilErr(t *testing.T) {
tc := newCase(t, `expected error; got nil`)
tc := newCase(t, `expected non-nil error; got nil`)
t.Cleanup(tc.assert)

var target FakeError
ErrorAs(tc, nil, &target)
}

func TestErrorAs_nilTarget(t *testing.T) {
tc := newCase(t, `expected target not to be nil`)
tc := newCase(t, `expected non-nil target; got nil`)
t.Cleanup(tc.assert)

var target *FakeError
Expand Down

0 comments on commit 56f7a18

Please sign in to comment.