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
12 changes: 12 additions & 0 deletions assert/assertion_format.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,18 @@ func ErrorIsf(t TestingT, err error, target error, msg string, args ...interface
return ErrorIs(t, err, target, 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.
//
// actualObj, err := SomeFunction()
// 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...)...)
}

// Eventuallyf asserts that given condition will be met in waitFor time,
// periodically checking target function each tick.
//
Expand Down
24 changes: 24 additions & 0 deletions assert/assertion_forward.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,30 @@ func (a *Assertions) ErrorIsf(err error, target error, msg string, args ...inter
return ErrorIsf(a.t, err, target, 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{}) 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.
//
// actualObj, err := SomeFunction()
// a.ErrorNotContainsf(err, expectedErrorSubString, "error message %s", "formatted")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// a.ErrorNotContainsf(err, expectedErrorSubString, "error message %s", "formatted")
// a.ErrorNotContainsf(err, expectedErrorSubString, "error message %s", "formatted")

Extra space here.

Reported here, but valid for everywhere.

func (a *Assertions) ErrorNotContainsf(theError error, contains string, msg string, args ...interface{}) bool {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use err not theError, to be consistent with other methods and Go specification

Suggested change
func (a *Assertions) ErrorNotContainsf(theError error, contains string, msg string, args ...interface{}) bool {
func (a *Assertions) ErrorNotContainsf(err error, contains string, msg string, args ...interface{}) bool {

Reported here, but valid for everything

if h, ok := a.t.(tHelper); ok {
h.Helper()
}
return ErrorNotContainsf(a.t, theError, contains, msg, args...)
}

// Errorf asserts that a function returned a non-nil error (ie. an error).
//
// actualObj, err := SomeFunction()
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
30 changes: 30 additions & 0 deletions require/require.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,36 @@ func ErrorIsf(t TestingT, err error, target error, msg string, args ...interface
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.
//
// actualObj, err := SomeFunction()
// 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()
}

// Errorf asserts that a function returned a non-nil error (ie. an error).
//
// actualObj, err := SomeFunction()
Expand Down
24 changes: 24 additions & 0 deletions require/require_forward.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,30 @@ func (a *Assertions) ErrorIsf(err error, target error, msg string, args ...inter
ErrorIsf(a.t, err, target, 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...)
}

// Errorf asserts that a function returned a non-nil error (ie. an error).
//
// actualObj, err := SomeFunction()
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