Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions assert/assertion_format.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,17 @@ func ErrorContainsf(t TestingT, theError error, contains string, msg string, arg
return ErrorContains(t, theError, contains, append([]interface{}{msg}, args...)...)
}

// ErrorNotContainsf asserts that a function returned a non-nil error (i.e. an
// error) and that the error does not contain the specified substring.
//
// assert.ErrorNotContainsf(t, err, expectedErrorSubString, "error message %s", "formatted")
func ErrorNotContainsf(t TestingT, theError error, contains string, msg string, args ...interface{}) bool {
if h, ok := t.(tHelper); ok {
h.Helper()
}
return ErrorNotContains(t, theError, contains, append([]interface{}{msg}, args...)...)
}

// ErrorIsf asserts that at least one of the errors in err's chain matches target.
// This is a wrapper for errors.Is.
func ErrorIsf(t TestingT, err error, target error, msg string, args ...interface{}) bool {
Expand Down
22 changes: 22 additions & 0 deletions assert/assertion_forward.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,28 @@ func (a *Assertions) ErrorContainsf(theError error, contains string, msg string,
return ErrorContainsf(a.t, theError, contains, msg, args...)
}

// ErrorNotContains asserts that a function returned a non-nil error (i.e. an
// error) and that the error does not contain the specified substring.
//
// a.ErrorNotContains(err, expectedErrorSubString)
func (a *Assertions) ErrorNotContains(theError error, contains string, msgAndArgs ...interface{}) bool {
if h, ok := a.t.(tHelper); ok {
h.Helper()
}
return ErrorNotContains(a.t, theError, contains, msgAndArgs...)
}

// ErrorNotContainsf asserts that a function returned a non-nil error (i.e. an
// error) and that the error does not contain the specified substring.
//
// a.ErrorNotContainsf(err, expectedErrorSubString, "error message %s", "formatted")
func (a *Assertions) ErrorNotContainsf(theError error, contains string, msg string, args ...interface{}) bool {
if h, ok := a.t.(tHelper); ok {
h.Helper()
}
return ErrorNotContainsf(a.t, theError, contains, msg, args...)
}

// ErrorIs asserts that at least one of the errors in err's chain matches target.
// This is a wrapper for errors.Is.
func (a *Assertions) ErrorIs(err error, target error, msgAndArgs ...interface{}) bool {
Expand Down
21 changes: 21 additions & 0 deletions assert/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -1707,6 +1707,27 @@ func ErrorContains(t TestingT, theError error, contains string, msgAndArgs ...in
return true
}

// ErrorNotContains asserts that a function returned a non-nil error (i.e. an
// error) and that the error does not contain the specified substring.
//
// actualObj, err := SomeFunction()
// assert.ErrorNotContains(t, err, expectedErrorSubString)
func ErrorNotContains(t TestingT, theError error, contains string, msgAndArgs ...interface{}) bool {
if h, ok := t.(tHelper); ok {
h.Helper()
}
if !Error(t, theError, msgAndArgs...) {
return false
}

actual := theError.Error()
if strings.Contains(actual, contains) {
return Fail(t, fmt.Sprintf("Error %s contains %#v", truncatingFormat("%#v", actual), contains), msgAndArgs...)
}

return true
}

// matchRegexp return true if a specified regexp matches a string.
func matchRegexp(rx interface{}, str interface{}) bool {
var r *regexp.Regexp
Expand Down
31 changes: 31 additions & 0 deletions assert/assertions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1760,6 +1760,26 @@ func TestErrorContains(t *testing.T) {
"ErrorContains should return true")
}

func TestErrorNotContains(t *testing.T) {
t.Parallel()

mockT := new(testing.T)

// start with a nil error
var err error
False(t, ErrorNotContains(mockT, err, ""),
"ErrorNotContains should return false for nil arg")

// now set an error
err = errors.New("some error: another error")
True(t, ErrorNotContains(mockT, err, "bad error"),
"ErrorNotContains should return true for different error string")
False(t, ErrorNotContains(mockT, err, "some error"),
"ErrorNotContains should return false")
False(t, ErrorNotContains(mockT, err, "another error"),
"ErrorNotContains should return false")
}

func Test_isEmpty(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -4150,6 +4170,17 @@ func TestErrorContainsWithErrorTooLongToPrint(t *testing.T) {
Contains(t, mockT.errorString(), `<... truncated> does not contain "EOF"`)
}

func TestErrorNotContainsWithErrorTooLongToPrint(t *testing.T) {
t.Parallel()
mockT := new(mockTestingT)
longSlice := make([]int, 1_000_000)
ErrorNotContains(mockT, fmt.Errorf("long: %v", longSlice), "long:")
Contains(t, mockT.errorString(), `
Error Trace:
Error: Error "long: [0 0 0`)
Contains(t, mockT.errorString(), `<... truncated> contains "long:"`)
}

func TestZeroWithSliceTooLongToPrint(t *testing.T) {
t.Parallel()
mockT := new(mockTestingT)
Expand Down
21 changes: 21 additions & 0 deletions assert/forward_assertions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,27 @@ func TestErrorContainsWrapper(t *testing.T) {
"ErrorContains should return true")
}

func TestErrorNotContainsWrapper(t *testing.T) {
t.Parallel()

assert := New(t)
mockAssert := New(new(testing.T))

// start with a nil error
var err error
assert.False(mockAssert.ErrorNotContains(err, ""),
"ErrorNotContains should return false for nil arg")

// now set an error
err = errors.New("some error: another error")
assert.True(mockAssert.ErrorNotContains(err, "bad error"),
"ErrorNotContains should return true for different error string")
assert.False(mockAssert.ErrorNotContains(err, "some error"),
"ErrorNotContains should return false")
assert.False(mockAssert.ErrorNotContains(err, "another error"),
"ErrorNotContains should return false")
}

func TestEqualErrorWrapper(t *testing.T) {
t.Parallel()

Expand Down
14 changes: 14 additions & 0 deletions require/forward_requirements_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,20 @@ func TestErrorContainsWrapper(t *testing.T) {
}
}

func TestErrorNotContainsWrapper(t *testing.T) {
t.Parallel()

require := New(t)
require.ErrorNotContains(errors.New("some error: another error"), "different error")

mockT := new(MockT)
mockRequire := New(mockT)
mockRequire.ErrorNotContains(errors.New("some error: another error"), "some error")
if !mockT.Failed {
t.Error("Check should fail")
}
}

func TestEqualErrorWrapper(t *testing.T) {
t.Parallel()

Expand Down
29 changes: 29 additions & 0 deletions require/require.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,35 @@ func ErrorContainsf(t TestingT, theError error, contains string, msg string, arg
t.FailNow()
}

// ErrorNotContains asserts that a function returned a non-nil error (i.e. an
// error) and that the error does not contain the specified substring.
//
// actualObj, err := SomeFunction()
// require.ErrorNotContains(t, err, expectedErrorSubString)
func ErrorNotContains(t TestingT, theError error, contains string, msgAndArgs ...interface{}) {
if h, ok := t.(tHelper); ok {
h.Helper()
}
if assert.ErrorNotContains(t, theError, contains, msgAndArgs...) {
return
}
t.FailNow()
}

// ErrorNotContainsf asserts that a function returned a non-nil error (i.e. an
// error) and that the error does not contain the specified substring.
//
// require.ErrorNotContainsf(t, err, expectedErrorSubString, "error message %s", "formatted")
func ErrorNotContainsf(t TestingT, theError error, contains string, msg string, args ...interface{}) {
if h, ok := t.(tHelper); ok {
h.Helper()
}
if assert.ErrorNotContainsf(t, theError, contains, msg, args...) {
return
}
t.FailNow()
}

// ErrorIs asserts that at least one of the errors in err's chain matches target.
// This is a wrapper for errors.Is.
func ErrorIs(t TestingT, err error, target error, msgAndArgs ...interface{}) {
Expand Down
24 changes: 24 additions & 0 deletions require/require_forward.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,30 @@ func (a *Assertions) ErrorContainsf(theError error, contains string, msg string,
ErrorContainsf(a.t, theError, contains, msg, args...)
}

// ErrorNotContains asserts that a function returned a non-nil error (i.e. an
// error) and that the error does not contain the specified substring.
//
// actualObj, err := SomeFunction()
// a.ErrorNotContains(err, expectedErrorSubString)
func (a *Assertions) ErrorNotContains(theError error, contains string, msgAndArgs ...interface{}) {
if h, ok := a.t.(tHelper); ok {
h.Helper()
}
ErrorNotContains(a.t, theError, contains, msgAndArgs...)
}

// ErrorNotContainsf asserts that a function returned a non-nil error (i.e. an
// error) and that the error does not contain the specified substring.
//
// actualObj, err := SomeFunction()
// a.ErrorNotContainsf(err, expectedErrorSubString, "error message %s", "formatted")
func (a *Assertions) ErrorNotContainsf(theError error, contains string, msg string, args ...interface{}) {
if h, ok := a.t.(tHelper); ok {
h.Helper()
}
ErrorNotContainsf(a.t, theError, contains, msg, args...)
}

// ErrorIs asserts that at least one of the errors in err's chain matches target.
// This is a wrapper for errors.Is.
func (a *Assertions) ErrorIs(err error, target error, msgAndArgs ...interface{}) {
Expand Down
12 changes: 12 additions & 0 deletions require/requirements_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,18 @@ func TestErrorContains(t *testing.T) {
}
}

func TestErrorNotContains(t *testing.T) {
t.Parallel()

ErrorNotContains(t, errors.New("some error: another error"), "different error")

mockT := new(MockT)
ErrorNotContains(mockT, errors.New("some error"), "some error")
if !mockT.Failed {
t.Error("Check should fail")
}
}

func TestEqualError(t *testing.T) {
t.Parallel()

Expand Down
Loading