Merge pull request #1485 from kevinburkesegment/len-format

assert: better formatting for Len() error
This commit is contained in:
Bracken 2024-02-16 15:58:11 +00:00 committed by GitHub
commit 0e75f9941b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 22 additions and 38 deletions

View File

@ -772,11 +772,11 @@ func Len(t TestingT, object interface{}, length int, msgAndArgs ...interface{})
} }
l, ok := getLen(object) l, ok := getLen(object)
if !ok { if !ok {
return Fail(t, fmt.Sprintf("\"%s\" could not be applied builtin len()", object), msgAndArgs...) return Fail(t, fmt.Sprintf("\"%v\" could not be applied builtin len()", object), msgAndArgs...)
} }
if l != length { if l != length {
return Fail(t, fmt.Sprintf("\"%s\" should have %d item(s), but has %d", object, length, l), msgAndArgs...) return Fail(t, fmt.Sprintf("\"%v\" should have %d item(s), but has %d", object, length, l), msgAndArgs...)
} }
return true return true
} }

View File

@ -1661,49 +1661,33 @@ func TestLen(t *testing.T) {
ch <- 3 ch <- 3
cases := []struct { cases := []struct {
v interface{} v interface{}
l int l int
expected1234567 string // message when expecting 1234567 items
}{ }{
{[]int{1, 2, 3}, 3}, {[]int{1, 2, 3}, 3, `"[1 2 3]" should have 1234567 item(s), but has 3`},
{[...]int{1, 2, 3}, 3}, {[...]int{1, 2, 3}, 3, `"[1 2 3]" should have 1234567 item(s), but has 3`},
{"ABC", 3}, {"ABC", 3, `"ABC" should have 1234567 item(s), but has 3`},
{map[int]int{1: 2, 2: 4, 3: 6}, 3}, {map[int]int{1: 2, 2: 4, 3: 6}, 3, `"map[1:2 2:4 3:6]" should have 1234567 item(s), but has 3`},
{ch, 3}, {ch, 3, ""},
{[]int{}, 0}, {[]int{}, 0, `"[]" should have 1234567 item(s), but has 0`},
{map[int]int{}, 0}, {map[int]int{}, 0, `"map[]" should have 1234567 item(s), but has 0`},
{make(chan int), 0}, {make(chan int), 0, ""},
{[]int(nil), 0}, {[]int(nil), 0, `"[]" should have 1234567 item(s), but has 0`},
{map[int]int(nil), 0}, {map[int]int(nil), 0, `"map[]" should have 1234567 item(s), but has 0`},
{(chan int)(nil), 0}, {(chan int)(nil), 0, `"<nil>" should have 1234567 item(s), but has 0`},
} }
for _, c := range cases { for _, c := range cases {
True(t, Len(mockT, c.v, c.l), "%#v have %d items", c.v, c.l) True(t, Len(mockT, c.v, c.l), "%#v have %d items", c.v, c.l)
} False(t, Len(mockT, c.v, c.l+1), "%#v have %d items", c.v, c.l)
if c.expected1234567 != "" {
cases = []struct { msgMock := new(mockTestingT)
v interface{} Len(msgMock, c.v, 1234567)
l int Contains(t, msgMock.errorString(), c.expected1234567)
}{ }
{[]int{1, 2, 3}, 4},
{[...]int{1, 2, 3}, 2},
{"ABC", 2},
{map[int]int{1: 2, 2: 4, 3: 6}, 4},
{ch, 2},
{[]int{}, 1},
{map[int]int{}, 1},
{make(chan int), 1},
{[]int(nil), 1},
{map[int]int(nil), 1},
{(chan int)(nil), 1},
}
for _, c := range cases {
False(t, Len(mockT, c.v, c.l), "%#v have %d items", c.v, c.l)
} }
} }