|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | +) |
| 10 | + |
| 11 | +func TestParseScheduleAtFlag(t *testing.T) { |
| 12 | + tests := map[string]struct { |
| 13 | + flag string |
| 14 | + expectedHour int |
| 15 | + expectedLoc string |
| 16 | + expectedError string |
| 17 | + }{ |
| 18 | + "single hour uses local timezone": { |
| 19 | + flag: "3", |
| 20 | + expectedHour: 3, |
| 21 | + expectedLoc: time.Local.String(), |
| 22 | + }, |
| 23 | + "hour and timezone": { |
| 24 | + flag: "3 Europe/Paris", |
| 25 | + expectedHour: 3, |
| 26 | + expectedLoc: "Europe/Paris", |
| 27 | + }, |
| 28 | + "hour with minutes and timezone (1)": { |
| 29 | + flag: "4:00 Europe/Paris", |
| 30 | + expectedHour: 4, |
| 31 | + expectedLoc: "Europe/Paris", |
| 32 | + }, |
| 33 | + "hour with minutes and timezone (2)": { |
| 34 | + flag: "4h00 Europe/Paris", |
| 35 | + expectedHour: 4, |
| 36 | + expectedLoc: "Europe/Paris", |
| 37 | + }, |
| 38 | + "hour with minutes and timezone (3)": { |
| 39 | + flag: "4H00 Europe/Paris", |
| 40 | + expectedHour: 4, |
| 41 | + expectedLoc: "Europe/Paris", |
| 42 | + }, |
| 43 | + "invalid flag (1)": { |
| 44 | + flag: "invalid", |
| 45 | + expectedError: "schedule-at=only contains two space-separated fields", |
| 46 | + }, |
| 47 | + "invalid flag (2)": { |
| 48 | + flag: "invalid invalid2", |
| 49 | + expectedError: "schedule-at=first field must be a digit representing the hour", |
| 50 | + }, |
| 51 | + "invalid flag (3)": { |
| 52 | + flag: "invalid invalid2:invalid3", |
| 53 | + expectedError: "schedule-at=first field must be a digit representing the hour", |
| 54 | + }, |
| 55 | + "invalid hour": { |
| 56 | + flag: "aa Europe/Paris", |
| 57 | + expectedError: "schedule-at=first field must be a digit representing the hour", |
| 58 | + }, |
| 59 | + "unknown timezone": { |
| 60 | + flag: "3 Europe/Unknown", |
| 61 | + expectedError: "schedule-at=unknown timezoneEurope/Unknown", |
| 62 | + }, |
| 63 | + } |
| 64 | + |
| 65 | + for name, test := range tests { |
| 66 | + t.Run(name, func(t *testing.T) { |
| 67 | + hour, loc, err := parseScheduleAtFlag(test.flag) |
| 68 | + if test.expectedError != "" { |
| 69 | + require.ErrorContains(t, err, test.expectedError) |
| 70 | + assert.Equal(t, -1, hour) |
| 71 | + assert.Nil(t, loc) |
| 72 | + return |
| 73 | + } |
| 74 | + |
| 75 | + require.NoError(t, err) |
| 76 | + require.NotNil(t, loc) |
| 77 | + assert.Equal(t, test.expectedHour, hour) |
| 78 | + assert.Equal(t, test.expectedLoc, loc.String()) |
| 79 | + }) |
| 80 | + } |
| 81 | +} |
0 commit comments