mirror of https://github.com/stretchr/testify.git
assert: shorten cases
parent
f7fedd9f85
commit
f6ed021e60
|
@ -1663,75 +1663,33 @@ func TestLen(t *testing.T) {
|
||||||
cases := []struct {
|
cases := []struct {
|
||||||
v interface{}
|
v interface{}
|
||||||
l int
|
l int
|
||||||
}{
|
format string
|
||||||
{[]int{1, 2, 3}, 3},
|
|
||||||
{[...]int{1, 2, 3}, 3},
|
|
||||||
{"ABC", 3},
|
|
||||||
{map[int]int{1: 2, 2: 4, 3: 6}, 3},
|
|
||||||
{ch, 3},
|
|
||||||
|
|
||||||
{[]int{}, 0},
|
|
||||||
{map[int]int{}, 0},
|
|
||||||
{make(chan int), 0},
|
|
||||||
|
|
||||||
{[]int(nil), 0},
|
|
||||||
{map[int]int(nil), 0},
|
|
||||||
{(chan int)(nil), 0},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, c := range cases {
|
|
||||||
True(t, Len(mockT, c.v, c.l), "%#v have %d items", c.v, c.l)
|
|
||||||
}
|
|
||||||
|
|
||||||
cases = []struct {
|
|
||||||
v interface{}
|
|
||||||
l int
|
|
||||||
}{
|
|
||||||
{[]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)
|
|
||||||
}
|
|
||||||
|
|
||||||
formatCases := []struct {
|
|
||||||
in interface{}
|
|
||||||
wantLen int
|
|
||||||
want string
|
|
||||||
}{
|
}{
|
||||||
{[]int{1, 2, 3}, 3, "[1 2 3]"},
|
{[]int{1, 2, 3}, 3, "[1 2 3]"},
|
||||||
{[...]int{1, 2, 3}, 3, "[1 2 3]"},
|
{[...]int{1, 2, 3}, 3, "[1 2 3]"},
|
||||||
{"ABC", 3, "ABC"},
|
{"ABC", 3, "ABC"},
|
||||||
{map[int]int{1: 2, 2: 4, 3: 6}, 3, "map[1:2 2:4 3:6]"},
|
{map[int]int{1: 2, 2: 4, 3: 6}, 3, "map[1:2 2:4 3:6]"},
|
||||||
|
{ch, 3, ""},
|
||||||
|
|
||||||
{[]int{}, 0, "[]"},
|
{[]int{}, 0, "[]"},
|
||||||
{map[int]int{}, 0, "map[]"},
|
{map[int]int{}, 0, "map[]"},
|
||||||
|
{make(chan int), 0, ""},
|
||||||
|
|
||||||
{[]int(nil), 0, "[]"},
|
{[]int(nil), 0, "[]"},
|
||||||
{map[int]int(nil), 0, "map[]"},
|
{map[int]int(nil), 0, "map[]"},
|
||||||
{(chan int)(nil), 0, "<nil>"},
|
{(chan int)(nil), 0, "<nil>"},
|
||||||
}
|
}
|
||||||
|
|
||||||
t.Run("Len() error message formatting", func(t *testing.T) {
|
for _, c := range cases {
|
||||||
for _, tt := range formatCases {
|
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.format != "" {
|
||||||
msgMock := new(mockTestingT)
|
msgMock := new(mockTestingT)
|
||||||
Len(msgMock, tt.in, 1234567)
|
Len(msgMock, c.v, 1234567)
|
||||||
want := fmt.Sprintf(`"%s" should have 1234567 item(s), but has %d`, tt.want, tt.wantLen)
|
want := fmt.Sprintf(`"%s" should have 1234567 item(s), but has %d`, c.format, c.l)
|
||||||
Contains(t, msgMock.errorString(), want)
|
Contains(t, msgMock.errorString(), want)
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestWithinDuration(t *testing.T) {
|
func TestWithinDuration(t *testing.T) {
|
||||||
|
|
Loading…
Reference in New Issue