Skip to content

Commit 690d49c

Browse files
committed
assert: fix error message formatting for NotContains
It was using "%s" to format the s and contains arguments, but these could be any type that supports iteration such as a map. In this case of NotContains failing against a map, it would print something like: "map[one:%!s(int=1)]" should not contain "one" Fix this using "%#v" as was already done for Contains and added test cases covering both the "len()" / iterable failure messages as well as the Contains/NotContains failure case. The new message for this example map would be something like: map[string]int{"one":1} should not contain "one"
1 parent ce0aede commit 690d49c

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

assert/assertions.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -877,10 +877,10 @@ func NotContains(t TestingT, s, contains interface{}, msgAndArgs ...interface{})
877877

878878
ok, found := containsElement(s, contains)
879879
if !ok {
880-
return Fail(t, fmt.Sprintf("\"%s\" could not be applied builtin len()", s), msgAndArgs...)
880+
return Fail(t, fmt.Sprintf("%#v could not be applied builtin len()", s), msgAndArgs...)
881881
}
882882
if found {
883-
return Fail(t, fmt.Sprintf("\"%s\" should not contain \"%s\"", s, contains), msgAndArgs...)
883+
return Fail(t, fmt.Sprintf("%#v should not contain %#v", s, contains), msgAndArgs...)
884884
}
885885

886886
return true

assert/assertions_test.go

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,10 @@ func TestContainsNotContains(t *testing.T) {
692692
func TestContainsNotContainsFailMessage(t *testing.T) {
693693
mockT := new(mockTestingT)
694694

695+
type nonContainer struct {
696+
Value string
697+
}
698+
695699
cases := []struct {
696700
assertion func(t TestingT, s, contains interface{}, msgAndArgs ...interface{}) bool
697701
container interface{}
@@ -704,11 +708,35 @@ func TestContainsNotContainsFailMessage(t *testing.T) {
704708
instance: errors.New("Hello"),
705709
expected: "\"Hello World\" does not contain &errors.errorString{s:\"Hello\"}",
706710
},
711+
{
712+
assertion: Contains,
713+
container: map[string]int{"one": 1},
714+
instance: "two",
715+
expected: "map[string]int{\"one\":1} does not contain \"two\"\n",
716+
},
717+
{
718+
assertion: NotContains,
719+
container: map[string]int{"one": 1},
720+
instance: "one",
721+
expected: "map[string]int{\"one\":1} should not contain \"one\"",
722+
},
723+
{
724+
assertion: Contains,
725+
container: nonContainer{Value: "Hello"},
726+
instance: "Hello",
727+
expected: "assert.nonContainer{Value:\"Hello\"} could not be applied builtin len()\n",
728+
},
729+
{
730+
assertion: NotContains,
731+
container: nonContainer{Value: "Hello"},
732+
instance: "Hello",
733+
expected: "assert.nonContainer{Value:\"Hello\"} could not be applied builtin len()\n",
734+
},
707735
}
708736
for _, c := range cases {
709737
name := filepath.Base(runtime.FuncForPC(reflect.ValueOf(c.assertion).Pointer()).Name())
710738
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"))
739+
c.assertion(mockT, c.container, c.instance)
712740
actualFail := mockT.errorString()
713741
if !strings.Contains(actualFail, c.expected) {
714742
t.Errorf("Contains failure should include %q but was %q", c.expected, actualFail)

0 commit comments

Comments
 (0)