From d934d117da74875477b0400460c8103aefe90548 Mon Sep 17 00:00:00 2001 From: reesporte Date: Fri, 22 Oct 2021 14:52:57 -0500 Subject: [PATCH] add test case names --- util_test.go | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/util_test.go b/util_test.go index ea27f1c5b..3416a95a8 100644 --- a/util_test.go +++ b/util_test.go @@ -22,37 +22,44 @@ import ( ) func TestGetLoopProgress(t *testing.T) { + // TODO: try to find more sneaky cases cases := []struct { + name string start time.Time now time.Time i uint total uint }{ { + "one minute, half done", time.Date(1969, time.June, 9, 4, 20, 0, 0, time.UTC), time.Date(1969, time.June, 9, 4, 21, 0, 0, time.UTC), 10, 20, }, { + "four seconds, half done", time.Date(1969, time.June, 9, 4, 20, 0, 0, time.UTC), time.Date(1969, time.June, 9, 4, 20, 4, 0, time.UTC), 10, 20, }, { + "one minute, one second, 5 μs, half done", time.Date(1969, time.June, 9, 4, 20, 0, 0, time.UTC), time.Date(1969, time.June, 9, 4, 21, 1, 5, time.UTC), 10, 20, }, { + "one minute, one done", time.Date(1969, time.June, 9, 4, 20, 0, 0, time.UTC), time.Date(1969, time.June, 9, 4, 21, 0, 0, time.UTC), 1, 20, }, { + "one minute, 1/5 done", time.Date(1969, time.June, 9, 4, 20, 0, 0, time.UTC), time.Date(1969, time.June, 9, 4, 21, 0, 0, time.UTC), 10, @@ -61,17 +68,19 @@ func TestGetLoopProgress(t *testing.T) { } for _, c := range cases { - // we expect that it will be the avg time per message times - // the number of remaining messages - expected := time.Duration((float64(c.now.Sub(c.start)) / float64(c.i+1)) * float64(c.total-(c.i+1))) - expectedPct := 100 * (float64(c.i+1) / float64(c.total)) + t.Run(c.name, func(t *testing.T) { + // we expect that it will be the avg time per message times + // the number of remaining messages + expected := time.Duration((float64(c.now.Sub(c.start)) / float64(c.i+1)) * float64(c.total-(c.i+1))) + expectedPct := 100 * (float64(c.i+1) / float64(c.total)) - timeLeft, pctDone := GetLoopProgress(c.start, c.now, c.i, c.total) - if timeLeft != expected { - t.Errorf("Time left was incorrect, expected: %d, but got: %d", expected, timeLeft) - } - if pctDone != expectedPct { - t.Errorf("Percentage done was incorrect, expected: %f, but got: %f", expectedPct, pctDone) - } + timeLeft, pctDone := GetLoopProgress(c.start, c.now, c.i, c.total) + if timeLeft != expected { + t.Errorf("Time left was incorrect, expected: %d, but got: %d", expected, timeLeft) + } + if pctDone != expectedPct { + t.Errorf("Percentage done was incorrect, expected: %f, but got: %f", expectedPct, pctDone) + } + }) } }