Skip to content

Commit

Permalink
Move error assertions to own file
Browse files Browse the repository at this point in the history
  • Loading branch information
rliebz committed Apr 27, 2024
1 parent 5e08a90 commit 87709f9
Show file tree
Hide file tree
Showing 4 changed files with 302 additions and 284 deletions.
114 changes: 0 additions & 114 deletions be/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,120 +164,6 @@ func quoteString(s string) string {
return fmt.Sprintf("%q", s)
}

// Error asserts that an error is non-nil.
func Error(err error) ghost.Result {
args := ghostlib.ArgsFromAST(err)
argErr := args[0]

if err == nil {
return ghost.Result{
Ok: false,
Message: fmt.Sprintf("%s is nil", argErr),
}
}

return ghost.Result{
Ok: true,
Message: fmt.Sprintf("%s has error value: %s", argErr, err),
}
}

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

switch {
case err == nil && argMsg == fmt.Sprintf("%q", msg):
return ghost.Result{
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(`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(`error %v contains message %v
got: %v
want: %v`,
argErr,
argMsg,
err,
msg,
),
}
default:
return ghost.Result{
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, argMsg := args[0], args[1]

if err == nil {
return ghost.Result{
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(`error %v has message %v
value: %v`,
argErr,
argMsg,
err,
),
}
}

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

// False asserts that a value is false.
func False(b bool) ghost.Result {
args := ghostlib.ArgsFromAST(b)
Expand Down
170 changes: 0 additions & 170 deletions be/assertions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,176 +335,6 @@ want: "bar"
})
}

func TestError(t *testing.T) {
t.Run("non-nil", func(t *testing.T) {
g := ghost.New(t)

err := errors.New("oopsie")

result := be.Error(err)
g.Should(be.True(result.Ok))
g.Should(be.Equal(result.Message, `err has error value: oopsie`))

result = be.Error(errors.New("oopsie"))
g.Should(be.True(result.Ok))
g.Should(be.Equal(result.Message, `errors.New("oopsie") has error value: oopsie`))
})

t.Run("nil", func(t *testing.T) {
g := ghost.New(t)

var err error

result := be.Error(err)
g.Should(be.False(result.Ok))
g.Should(be.Equal(result.Message, `err is nil`))

result = be.Error(nil)
g.Should(be.False(result.Ok))
g.Should(be.Equal(result.Message, `nil is nil`))
})
}

func TestErrorContaining(t *testing.T) {
t.Run("contains", func(t *testing.T) {
g := ghost.New(t)

err := errors.New("foobar")
msg := "oob"

result := be.ErrorContaining(err, msg)
g.Should(be.True(result.Ok))
g.Should(be.Equal(
result.Message,
`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,
`error errors.New("foobar") contains message "oob"
got: foobar
want: oob`,
))
})

t.Run("does not contain", func(t *testing.T) {
g := ghost.New(t)

err := errors.New("foobar")
msg := "boo"

result := be.ErrorContaining(err, msg)
g.Should(be.False(result.Ok))
g.Should(be.Equal(
result.Message,
`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,
`error errors.New("foobar") does not contain message "boo"
got: foobar
want: boo`,
))
})

t.Run("nil", func(t *testing.T) {
g := ghost.New(t)

var err error
msg := "boo"

result := be.ErrorContaining(err, msg)
g.Should(be.False(result.Ok))
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, `error nil is nil, does not contain message
got: <nil>
want: boo`))
})
}

func TestErrorEqual(t *testing.T) {
t.Run("equal", func(t *testing.T) {
g := ghost.New(t)

err := errors.New("foobar")
msg := "foobar"

result := be.ErrorEqual(err, msg)
g.Should(be.True(result.Ok))
g.Should(be.Equal(
result.Message,
`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,
`error errors.New("foobar") has message "foobar"
value: foobar`,
))
})

t.Run("not equal", func(t *testing.T) {
g := ghost.New(t)

err := errors.New("foobar")
msg := "boo"

result := be.ErrorEqual(err, msg)
g.Should(be.False(result.Ok))
g.Should(be.Equal(
result.Message,
`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,
`error errors.New("foobar") does not have message "boo"
got: foobar
want: boo`,
))
})

t.Run("nil", func(t *testing.T) {
g := ghost.New(t)

var err error
msg := "boo"

result := be.ErrorEqual(err, msg)
g.Should(be.False(result.Ok))
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, `error nil is nil
got: <nil>
want: boo`))
})
}

func TestFalse(t *testing.T) {
t.Run("true", func(t *testing.T) {
g := ghost.New(t)
Expand Down
Loading

0 comments on commit 87709f9

Please sign in to comment.