Skip to content

Commit

Permalink
assert: shorten cases
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinburkesegment committed Oct 16, 2023
1 parent 0df26bb commit 14c1b0e
Showing 1 changed file with 12 additions and 54 deletions.
66 changes: 12 additions & 54 deletions assert/assertions_test.go
Expand Up @@ -1644,77 +1644,35 @@ func TestLen(t *testing.T) {
ch <- 3

cases := []struct {
v interface{}
l int
}{
{[]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
v interface{}
l int
format string
}{
{[]int{1, 2, 3}, 3, "[1 2 3]"},
{[...]int{1, 2, 3}, 3, "[1 2 3]"},
{"ABC", 3, "ABC"},
{map[int]int{1: 2, 2: 4, 3: 6}, 3, "map[1:2 2:4 3:6]"},
{ch, 3, ""},

{[]int{}, 0, "[]"},
{map[int]int{}, 0, "map[]"},
{make(chan int), 0, ""},

{[]int(nil), 0, "[]"},
{map[int]int(nil), 0, "map[]"},
{(chan int)(nil), 0, "<nil>"},
}

t.Run("Len() error message formatting", func(t *testing.T) {
for _, tt := range formatCases {
for _, c := range cases {
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)
Len(msgMock, tt.in, 1234567)
want := fmt.Sprintf(`"%s" should have 1234567 item(s), but has %d`, tt.want, tt.wantLen)
Len(msgMock, c.v, 1234567)
want := fmt.Sprintf(`"%s" should have 1234567 item(s), but has %d`, c.format, c.l)
Contains(t, msgMock.errorString(), want)
}
})
}
}

func TestWithinDuration(t *testing.T) {
Expand Down

0 comments on commit 14c1b0e

Please sign in to comment.