| 14 | } |
| 15 | |
| 16 | func TestText(t *testing.T) { |
| 17 | testCases := []struct { |
| 18 | input string |
| 19 | success bool |
| 20 | expected time.Time |
| 21 | }{ |
| 22 | {"2003-01-02T15:04:05Z", true, time.Date(2003, 1, 2, 15, 04, 05, 0, time.UTC)}, |
| 23 | {"2003-01-02 15:05:01", false, time.Time{}}, //negative case, invalid layout |
| 24 | {"2024-11-22T03:01:02Z", true, time.Date(2024, 11, 22, 3, 1, 02, 0, time.UTC)}, |
| 25 | {"2006-01-02T15:04:05+07:00", true, time.Date(2006, 1, 2, 15, 4, 5, 0, time.FixedZone("UTC+7", 7*60*60))}, |
| 26 | } |
| 27 | |
| 28 | devnull, _ := os.Open(os.DevNull) |
| 29 | os.Stderr = devnull |
| 30 | for i := range testCases { |
| 31 | var ts time.Time |
| 32 | f := setUpTime(&ts) |
| 33 | tc := &testCases[i] |
| 34 | arg := fmt.Sprintf("--time=%s", tc.input) |
| 35 | err := f.Parse([]string{arg}) |
| 36 | if err != nil { |
| 37 | if tc.success { |
| 38 | t.Errorf("expected parsing to succeed, but got %q", err) |
| 39 | } |
| 40 | continue |
| 41 | } |
| 42 | if !tc.success { |
| 43 | t.Errorf("expected parsing failure, but parsing succeeded") |
| 44 | continue |
| 45 | } |
| 46 | parsedT := new(time.Time) |
| 47 | err = f.GetText("time", parsedT) |
| 48 | if err != nil { |
| 49 | t.Errorf("Got error trying to fetch the time flag: %v", err) |
| 50 | } |
| 51 | if !parsedT.Equal(tc.expected) { |
| 52 | t.Errorf("expected %q, got %q", tc.expected, parsedT) |
| 53 | } |
| 54 | |
| 55 | } |
| 56 | } |