Skip to content

Commit

Permalink
assert: rename and refactor TestContainsFailMessage
Browse files Browse the repository at this point in the history
To use this to validate the failure messages from both Contains and
NotContains, I've renamed it to TestContainsNotContains and added a
test case structure.

There are no functional changes here, strictly refactoring. A new test
case will be added in a subsequent change.
  • Loading branch information
wade-arista committed Mar 15, 2023
1 parent c5fc9d6 commit ce0aede
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions assert/assertions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"io"
"math"
"os"
"path/filepath"
"reflect"
"regexp"
"runtime"
Expand Down Expand Up @@ -688,15 +689,31 @@ func TestContainsNotContains(t *testing.T) {
}
}

func TestContainsFailMessage(t *testing.T) {

func TestContainsNotContainsFailMessage(t *testing.T) {
mockT := new(mockTestingT)

Contains(mockT, "Hello World", errors.New("Hello"))
expectedFail := "\"Hello World\" does not contain &errors.errorString{s:\"Hello\"}"
actualFail := mockT.errorString()
if !strings.Contains(actualFail, expectedFail) {
t.Errorf("Contains failure should include %q but was %q", expectedFail, actualFail)
cases := []struct {
assertion func(t TestingT, s, contains interface{}, msgAndArgs ...interface{}) bool
container interface{}
instance interface{}
expected string
}{
{
assertion: Contains,
container: "Hello World",
instance: errors.New("Hello"),
expected: "\"Hello World\" does not contain &errors.errorString{s:\"Hello\"}",
},
}
for _, c := range cases {
name := filepath.Base(runtime.FuncForPC(reflect.ValueOf(c.assertion).Pointer()).Name())
t.Run(fmt.Sprintf("%v(%T, %T)", name, c.container, c.instance), func(t *testing.T) {
c.assertion(mockT, "Hello World", errors.New("Hello"))
actualFail := mockT.errorString()
if !strings.Contains(actualFail, c.expected) {
t.Errorf("Contains failure should include %q but was %q", c.expected, actualFail)
}
})
}
}

Expand Down

0 comments on commit ce0aede

Please sign in to comment.