Skip to content

Commit ce0aede

Browse files
committed
assert: rename and refactor TestContainsFailMessage
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.
1 parent c5fc9d6 commit ce0aede

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

assert/assertions_test.go

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"io"
1010
"math"
1111
"os"
12+
"path/filepath"
1213
"reflect"
1314
"regexp"
1415
"runtime"
@@ -688,15 +689,31 @@ func TestContainsNotContains(t *testing.T) {
688689
}
689690
}
690691

691-
func TestContainsFailMessage(t *testing.T) {
692-
692+
func TestContainsNotContainsFailMessage(t *testing.T) {
693693
mockT := new(mockTestingT)
694694

695-
Contains(mockT, "Hello World", errors.New("Hello"))
696-
expectedFail := "\"Hello World\" does not contain &errors.errorString{s:\"Hello\"}"
697-
actualFail := mockT.errorString()
698-
if !strings.Contains(actualFail, expectedFail) {
699-
t.Errorf("Contains failure should include %q but was %q", expectedFail, actualFail)
695+
cases := []struct {
696+
assertion func(t TestingT, s, contains interface{}, msgAndArgs ...interface{}) bool
697+
container interface{}
698+
instance interface{}
699+
expected string
700+
}{
701+
{
702+
assertion: Contains,
703+
container: "Hello World",
704+
instance: errors.New("Hello"),
705+
expected: "\"Hello World\" does not contain &errors.errorString{s:\"Hello\"}",
706+
},
707+
}
708+
for _, c := range cases {
709+
name := filepath.Base(runtime.FuncForPC(reflect.ValueOf(c.assertion).Pointer()).Name())
710+
t.Run(fmt.Sprintf("%v(%T, %T)", name, c.container, c.instance), func(t *testing.T) {
711+
c.assertion(mockT, "Hello World", errors.New("Hello"))
712+
actualFail := mockT.errorString()
713+
if !strings.Contains(actualFail, c.expected) {
714+
t.Errorf("Contains failure should include %q but was %q", c.expected, actualFail)
715+
}
716+
})
700717
}
701718
}
702719

0 commit comments

Comments
 (0)