Skip to content

Commit

Permalink
Split be.Error* output into multiple lines
Browse files Browse the repository at this point in the history
  • Loading branch information
rliebz committed Feb 5, 2024
1 parent 1a71918 commit 1ca46c5
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 27 deletions.
72 changes: 57 additions & 15 deletions be/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,49 +190,91 @@ func ErrorContaining(err error, msg string) ghost.Result {
switch {
case err == nil && argMsg == fmt.Sprintf("%q", msg):
return ghost.Result{
Ok: false,
Message: fmt.Sprintf(`%v is nil; missing error message: %v`, argErr, msg),
Ok: false,
Message: fmt.Sprintf(`error %v is nil, does not contain message
got: <nil>
want: %v`,
argErr,
msg,
),
}
case err == nil:
return ghost.Result{
Ok: false,
Message: fmt.Sprintf(`%v is nil; missing error message %v: %v`, argErr, argMsg, msg),
Ok: false,
Message: fmt.Sprintf(`error %v is nil, does not contain %v
got: <nil>
want: %v`,
argErr,
argMsg,
msg,
),
}
case strings.Contains(err.Error(), msg):
return ghost.Result{
Ok: true,
Message: fmt.Sprintf("%v contains error message %q: %v", argErr, msg, err),
Ok: true,
Message: fmt.Sprintf(`error %v contains message %v
got: %v
want: %v`,
argErr,
argMsg,
err,
msg,
),
}
default:
return ghost.Result{
Ok: false,
Message: fmt.Sprintf("%v does not contain error message %q: %v", argErr, msg, err),
Ok: false,
Message: fmt.Sprintf(`error %v does not contain message %v
got: %v
want: %v`,
argErr,
argMsg,
err,
msg,
),
}
}
}

// ErrorEqual asserts that an error string equals a particular message.
func ErrorEqual(err error, msg string) ghost.Result {
args := ghostlib.ArgsFromAST(err, msg)
argErr := args[0]
argErr, argMsg := args[0], args[1]

if err == nil {
return ghost.Result{
Ok: false,
Message: fmt.Sprintf(`%v is nil; want message: %v`, argErr, msg),
Ok: false,
Message: fmt.Sprintf(`error %v is nil
got: <nil>
want: %v`,
argErr,
msg,
),
}
}

if err.Error() == msg {
return ghost.Result{
Ok: true,
Message: fmt.Sprintf("%v equals error message %q: %v", argErr, msg, err),
Ok: true,
Message: fmt.Sprintf(`error %v has message %v
value: %v`,
argErr,
argMsg,
err,
),
}
}

return ghost.Result{
Ok: false,
Message: fmt.Sprintf("%v does not equal error message %q: %v", argErr, msg, err),
Ok: false,
Message: fmt.Sprintf(`error %v does not have message %v
got: %v
want: %v`,
argErr,
argMsg,
err,
msg,
),
}
}

Expand Down
46 changes: 34 additions & 12 deletions be/assertions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,14 +353,18 @@ func TestErrorContaining(t *testing.T) {
g.Should(be.True(result.Ok))
g.Should(be.Equal(
result.Message,
`err contains error message "oob": foobar`,
`error err contains message msg
got: foobar
want: oob`,
))

result = be.ErrorContaining(errors.New("foobar"), "oob")
g.Should(be.True(result.Ok))
g.Should(be.Equal(
result.Message,
`errors.New("foobar") contains error message "oob": foobar`,
`error errors.New("foobar") contains message "oob"
got: foobar
want: oob`,
))
})

Expand All @@ -374,14 +378,18 @@ func TestErrorContaining(t *testing.T) {
g.Should(be.False(result.Ok))
g.Should(be.Equal(
result.Message,
`err does not contain error message "boo": foobar`,
`error err does not contain message msg
got: foobar
want: boo`,
))

result = be.ErrorContaining(errors.New("foobar"), "boo")
g.Should(be.False(result.Ok))
g.Should(be.Equal(
result.Message,
`errors.New("foobar") does not contain error message "boo": foobar`,
`error errors.New("foobar") does not contain message "boo"
got: foobar
want: boo`,
))
})

Expand All @@ -393,11 +401,15 @@ func TestErrorContaining(t *testing.T) {

result := be.ErrorContaining(err, msg)
g.Should(be.False(result.Ok))
g.Should(be.Equal(result.Message, `err is nil; missing error message msg: boo`))
g.Should(be.Equal(result.Message, `error err is nil, does not contain msg
got: <nil>
want: boo`))

result = be.ErrorContaining(nil, "boo")
g.Should(be.False(result.Ok))
g.Should(be.Equal(result.Message, `nil is nil; missing error message: boo`))
g.Should(be.Equal(result.Message, `error nil is nil, does not contain message
got: <nil>
want: boo`))
})
}

Expand All @@ -412,14 +424,16 @@ func TestErrorEqual(t *testing.T) {
g.Should(be.True(result.Ok))
g.Should(be.Equal(
result.Message,
`err equals error message "foobar": foobar`,
`error err has message msg
value: foobar`,
))

result = be.ErrorEqual(errors.New("foobar"), "foobar")
g.Should(be.True(result.Ok))
g.Should(be.Equal(
result.Message,
`errors.New("foobar") equals error message "foobar": foobar`,
`error errors.New("foobar") has message "foobar"
value: foobar`,
))
})

Expand All @@ -433,14 +447,18 @@ func TestErrorEqual(t *testing.T) {
g.Should(be.False(result.Ok))
g.Should(be.Equal(
result.Message,
`err does not equal error message "boo": foobar`,
`error err does not have message msg
got: foobar
want: boo`,
))

result = be.ErrorEqual(errors.New("foobar"), "boo")
g.Should(be.False(result.Ok))
g.Should(be.Equal(
result.Message,
`errors.New("foobar") does not equal error message "boo": foobar`,
`error errors.New("foobar") does not have message "boo"
got: foobar
want: boo`,
))
})

Expand All @@ -452,11 +470,15 @@ func TestErrorEqual(t *testing.T) {

result := be.ErrorEqual(err, msg)
g.Should(be.False(result.Ok))
g.Should(be.Equal(result.Message, `err is nil; want message: boo`))
g.Should(be.Equal(result.Message, `error err is nil
got: <nil>
want: boo`))

result = be.ErrorEqual(nil, "boo")
g.Should(be.False(result.Ok))
g.Should(be.Equal(result.Message, `nil is nil; want message: boo`))
g.Should(be.Equal(result.Message, `error nil is nil
got: <nil>
want: boo`))
})
}

Expand Down

0 comments on commit 1ca46c5

Please sign in to comment.