|
| 1 | +package duration_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "time" |
| 6 | + |
| 7 | + duration "github.com/coder/coder/v2/coderd/util/time" |
| 8 | +) |
| 9 | + |
| 10 | +func TestHumanize(t *testing.T) { |
| 11 | + t.Parallel() |
| 12 | + |
| 13 | + testCases := []struct { |
| 14 | + duration time.Duration |
| 15 | + expected string |
| 16 | + }{ |
| 17 | + { |
| 18 | + duration: time.Duration(0), |
| 19 | + expected: "0 seconds", |
| 20 | + }, |
| 21 | + { |
| 22 | + duration: time.Duration(1 * time.Second), |
| 23 | + expected: "1 second", |
| 24 | + }, |
| 25 | + { |
| 26 | + duration: time.Duration(45 * time.Second), |
| 27 | + expected: "45 seconds", |
| 28 | + }, |
| 29 | + { |
| 30 | + duration: time.Duration(30 * time.Minute), |
| 31 | + expected: "30 minutes", |
| 32 | + }, |
| 33 | + { |
| 34 | + duration: time.Duration(30*time.Minute + 10*time.Second), |
| 35 | + expected: "30 minutes and 10 seconds", |
| 36 | + }, |
| 37 | + { |
| 38 | + duration: time.Duration(2*time.Hour + 25*time.Minute + 10*time.Second), |
| 39 | + expected: "2 hours, 25 minutes and 10 seconds", |
| 40 | + }, |
| 41 | + { |
| 42 | + duration: time.Duration(2400 * time.Hour), |
| 43 | + expected: "100 days", |
| 44 | + }, |
| 45 | + } |
| 46 | + |
| 47 | + for _, tc := range testCases { |
| 48 | + t.Run(tc.expected, func(t *testing.T) { |
| 49 | + t.Parallel() |
| 50 | + |
| 51 | + actual := duration.Humanize(tc.duration) |
| 52 | + if actual != tc.expected { |
| 53 | + t.Errorf("expected: %s, got: %s", tc.expected, actual) |
| 54 | + } |
| 55 | + }) |
| 56 | + } |
| 57 | +} |
0 commit comments