Skip to content

Commit 59b8553

Browse files
committed
fix(collection): fixed error message in Subset.
This fix bug reported at <stretchr#1848>. Our fork suffered from the same issue. Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
1 parent 16f6c61 commit 59b8553

2 files changed

Lines changed: 38 additions & 7 deletions

File tree

internal/assertions/collection.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ func SliceNotSubsetT[Slice ~[]E, E comparable](t T, list, subset Slice, msgAndAr
528528
}
529529
}
530530

531-
return Fail(t, fmt.Sprintf("%s is a subset of %s", truncatingFormat("%q", subset), truncatingFormat("%q", list)), msgAndArgs...)
531+
return Fail(t, fmt.Sprintf("%s is a subset of %s", truncatingFormat("%#v", subset), truncatingFormat("%#v", list)), msgAndArgs...)
532532
}
533533

534534
// 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
805805
}
806806
}
807807

808-
return Fail(t, fmt.Sprintf("%s is a subset of %s", truncatingFormat("%q", subset), truncatingFormat("%q", list)), msgAndArgs...)
808+
return Fail(t, fmt.Sprintf("%s is a subset of %s", truncatingFormat("%#v", subset), truncatingFormat("%#v", list)), msgAndArgs...)
809809
}
810810

811811
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
829829
}
830830
}
831831

832-
return Fail(t, fmt.Sprintf("%s is a subset of %s", truncatingFormat("%q", subset), truncatingFormat("%q", list)), msgAndArgs...)
832+
return Fail(t, fmt.Sprintf("%s is a subset of %s", truncatingFormat("%#v", subset), truncatingFormat("%#v", list)), msgAndArgs...)
833833
}
834834

835835
// containsElement tries to loop over the list check if the list includes the element.

internal/assertions/collection_test.go

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1272,8 +1272,8 @@ func collectionErrorMessageCases() iter.Seq[failCase] {
12721272
name: "truncation/NotSubset(longSlice)",
12731273
assertion: func(t T) bool { return NotSubset(t, longSlice, longSlice) },
12741274
wantContains: []string{
1275-
`['\x00' '\x00' '\x00'`,
1276-
`<... truncated> is a subset of ['\x00' '\x00' '\x00'`,
1275+
`[]int{0, 0, 0,`,
1276+
`<... truncated> is a subset of []int{0, 0, 0,`,
12771277
},
12781278
},
12791279
{
@@ -1282,9 +1282,40 @@ func collectionErrorMessageCases() iter.Seq[failCase] {
12821282
return NotSubset(t, map[int][]int{1: longSlice}, map[int][]int{1: longSlice})
12831283
},
12841284
wantContains: []string{
1285-
`map['\x01':['\x00' '\x00' '\x00'`,
1286-
`<... truncated> is a subset of map['\x01':['\x00' '\x00' '\x00'`,
1285+
`map[int][]int{1:[]int{0, 0, 0,`,
1286+
`<... truncated> is a subset of map[int][]int{1:[]int{0, 0, 0,`,
1287+
},
1288+
},
1289+
1290+
// NotSubset/SliceNotSubsetT fail messages — regression coverage for
1291+
// upstream stretchr/testify#1800 / #1848: previously %q produced
1292+
// broken output like "%!q(bool=true)" or "'\x01'" for non-string types.
1293+
{
1294+
name: "NotSubset(bools)",
1295+
assertion: func(t T) bool { return NotSubset(t, []bool{true}, []bool{true}) },
1296+
wantContains: []string{`[]bool{true} is a subset of []bool{true}`},
1297+
},
1298+
{
1299+
name: "NotSubset(ints)",
1300+
assertion: func(t T) bool { return NotSubset(t, []int{1, 2, 3}, []int{1, 2}) },
1301+
wantContains: []string{`[]int{1, 2} is a subset of []int{1, 2, 3}`},
1302+
},
1303+
{
1304+
name: "NotSubset(map-int)",
1305+
assertion: func(t T) bool {
1306+
return NotSubset(t, map[int]string{1: "one"}, map[int]string{1: "one"})
12871307
},
1308+
wantContains: []string{`map[int]string{1:"one"} is a subset of map[int]string{1:"one"}`},
1309+
},
1310+
{
1311+
name: "SliceNotSubsetT(bools)",
1312+
assertion: func(t T) bool { return SliceNotSubsetT(t, []bool{true}, []bool{true}) },
1313+
wantContains: []string{`[]bool{true} is a subset of []bool{true}`},
1314+
},
1315+
{
1316+
name: "SliceNotSubsetT(ints)",
1317+
assertion: func(t T) bool { return SliceNotSubsetT(t, []int{1, 2, 3}, []int{1, 2}) },
1318+
wantContains: []string{`[]int{1, 2} is a subset of []int{1, 2, 3}`},
12881319
},
12891320
})
12901321
}

0 commit comments

Comments
 (0)