mirror of
https://github.com/stretchr/testify.git
synced 2025-05-31 11:42:44 +00:00
Fixes #311 - Detect empty interface error gotcha
This commit is contained in:
parent
8d64eb7173
commit
d77da356e5
@ -832,11 +832,11 @@ func InEpsilonSlice(t TestingT, expected, actual interface{}, epsilon float64, m
|
||||
//
|
||||
// Returns whether the assertion was successful (true) or not (false).
|
||||
func NoError(t TestingT, err error, msgAndArgs ...interface{}) bool {
|
||||
if isNil(err) {
|
||||
return true
|
||||
if err != nil {
|
||||
return Fail(t, fmt.Sprintf("Received unexpected error %q", err), msgAndArgs...)
|
||||
}
|
||||
|
||||
return Fail(t, fmt.Sprintf("Received unexpected error %q", err), msgAndArgs...)
|
||||
return true
|
||||
}
|
||||
|
||||
// Error asserts that a function returned an error (i.e. not `nil`).
|
||||
@ -850,8 +850,11 @@ func NoError(t TestingT, err error, msgAndArgs ...interface{}) bool {
|
||||
func Error(t TestingT, err error, msgAndArgs ...interface{}) bool {
|
||||
|
||||
message := messageFromMsgAndArgs(msgAndArgs...)
|
||||
return NotNil(t, err, "An error is expected but got nil. %s", message)
|
||||
if err == nil {
|
||||
return Fail(t, "An error is expected but got nil. %s", message)
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// EqualError asserts that a function returned an error (i.e. not `nil`)
|
||||
|
@ -523,8 +523,26 @@ func TestNoError(t *testing.T) {
|
||||
|
||||
False(t, NoError(mockT, err), "NoError with error should return False")
|
||||
|
||||
// returning an empty error interface
|
||||
err = func() error {
|
||||
var err *customError
|
||||
if err != nil {
|
||||
t.Fatal("err should be nil here")
|
||||
}
|
||||
return err
|
||||
}()
|
||||
|
||||
if err == nil { // err is not nil here!
|
||||
t.Errorf("Error should be nil due to empty interface", err)
|
||||
}
|
||||
|
||||
False(t, NoError(mockT, err), "NoError should fail with empty error interface")
|
||||
}
|
||||
|
||||
type customError struct{}
|
||||
|
||||
func (*customError) Error() string { return "fail" }
|
||||
|
||||
func TestError(t *testing.T) {
|
||||
|
||||
mockT := new(testing.T)
|
||||
@ -539,6 +557,20 @@ func TestError(t *testing.T) {
|
||||
|
||||
True(t, Error(mockT, err), "Error with error should return True")
|
||||
|
||||
// returning an empty error interface
|
||||
err = func() error {
|
||||
var err *customError
|
||||
if err != nil {
|
||||
t.Fatal("err should be nil here")
|
||||
}
|
||||
return err
|
||||
}()
|
||||
|
||||
if err == nil { // err is not nil here!
|
||||
t.Errorf("Error should be nil due to empty interface", err)
|
||||
}
|
||||
|
||||
True(t, Error(mockT, err), "Error should pass with empty error interface")
|
||||
}
|
||||
|
||||
func TestEqualError(t *testing.T) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user