From 59b85536dd60f7dcec556c7b23d41428d9d451fc Mon Sep 17 00:00:00 2001 From: Frederic BIDON Date: Fri, 17 Apr 2026 18:41:45 +0200 Subject: [PATCH] fix(collection): fixed error message in Subset. This fix bug reported at . Our fork suffered from the same issue. Signed-off-by: Frederic BIDON --- internal/assertions/collection.go | 6 ++-- internal/assertions/collection_test.go | 39 +++++++++++++++++++++++--- 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/internal/assertions/collection.go b/internal/assertions/collection.go index 5ace4e8b4..084b92ac2 100644 --- a/internal/assertions/collection.go +++ b/internal/assertions/collection.go @@ -528,7 +528,7 @@ func SliceNotSubsetT[Slice ~[]E, E comparable](t T, list, subset Slice, msgAndAr } } - return Fail(t, fmt.Sprintf("%s is a subset of %s", truncatingFormat("%q", subset), truncatingFormat("%q", list)), msgAndArgs...) + return Fail(t, fmt.Sprintf("%s is a subset of %s", truncatingFormat("%#v", subset), truncatingFormat("%#v", list)), msgAndArgs...) } // ElementsMatch asserts that the specified listA(array, slice...) is equal to specified @@ -805,7 +805,7 @@ func isNotSubsetMap(t T, list, subset any, subsetMap, actualMap reflect.Value, m } } - return Fail(t, fmt.Sprintf("%s is a subset of %s", truncatingFormat("%q", subset), truncatingFormat("%q", list)), msgAndArgs...) + return Fail(t, fmt.Sprintf("%s is a subset of %s", truncatingFormat("%#v", subset), truncatingFormat("%#v", list)), msgAndArgs...) } func isSubsetList(t T, list any, subsetList reflect.Value, msgAndArgs ...any) bool { @@ -829,7 +829,7 @@ func isNotSubsetList(t T, list, subset any, subsetList reflect.Value, msgAndArgs } } - return Fail(t, fmt.Sprintf("%s is a subset of %s", truncatingFormat("%q", subset), truncatingFormat("%q", list)), msgAndArgs...) + return Fail(t, fmt.Sprintf("%s is a subset of %s", truncatingFormat("%#v", subset), truncatingFormat("%#v", list)), msgAndArgs...) } // containsElement tries to loop over the list check if the list includes the element. diff --git a/internal/assertions/collection_test.go b/internal/assertions/collection_test.go index 37a0b5179..47a18512b 100644 --- a/internal/assertions/collection_test.go +++ b/internal/assertions/collection_test.go @@ -1272,8 +1272,8 @@ func collectionErrorMessageCases() iter.Seq[failCase] { name: "truncation/NotSubset(longSlice)", assertion: func(t T) bool { return NotSubset(t, longSlice, longSlice) }, wantContains: []string{ - `['\x00' '\x00' '\x00'`, - `<... truncated> is a subset of ['\x00' '\x00' '\x00'`, + `[]int{0, 0, 0,`, + `<... truncated> is a subset of []int{0, 0, 0,`, }, }, { @@ -1282,9 +1282,40 @@ func collectionErrorMessageCases() iter.Seq[failCase] { return NotSubset(t, map[int][]int{1: longSlice}, map[int][]int{1: longSlice}) }, wantContains: []string{ - `map['\x01':['\x00' '\x00' '\x00'`, - `<... truncated> is a subset of map['\x01':['\x00' '\x00' '\x00'`, + `map[int][]int{1:[]int{0, 0, 0,`, + `<... truncated> is a subset of map[int][]int{1:[]int{0, 0, 0,`, + }, + }, + + // NotSubset/SliceNotSubsetT fail messages — regression coverage for + // upstream stretchr/testify#1800 / #1848: previously %q produced + // broken output like "%!q(bool=true)" or "'\x01'" for non-string types. + { + name: "NotSubset(bools)", + assertion: func(t T) bool { return NotSubset(t, []bool{true}, []bool{true}) }, + wantContains: []string{`[]bool{true} is a subset of []bool{true}`}, + }, + { + name: "NotSubset(ints)", + assertion: func(t T) bool { return NotSubset(t, []int{1, 2, 3}, []int{1, 2}) }, + wantContains: []string{`[]int{1, 2} is a subset of []int{1, 2, 3}`}, + }, + { + name: "NotSubset(map-int)", + assertion: func(t T) bool { + return NotSubset(t, map[int]string{1: "one"}, map[int]string{1: "one"}) }, + wantContains: []string{`map[int]string{1:"one"} is a subset of map[int]string{1:"one"}`}, + }, + { + name: "SliceNotSubsetT(bools)", + assertion: func(t T) bool { return SliceNotSubsetT(t, []bool{true}, []bool{true}) }, + wantContains: []string{`[]bool{true} is a subset of []bool{true}`}, + }, + { + name: "SliceNotSubsetT(ints)", + assertion: func(t T) bool { return SliceNotSubsetT(t, []int{1, 2, 3}, []int{1, 2}) }, + wantContains: []string{`[]int{1, 2} is a subset of []int{1, 2, 3}`}, }, }) }