forked from cornfeedhobo/pflag
-
Notifications
You must be signed in to change notification settings - Fork 1
/
time_test.go
113 lines (102 loc) · 3.04 KB
/
time_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package zflag_test
import (
"io/ioutil"
"testing"
"time"
"github.com/zulucmd/zflag/v2"
)
func parseTime(t *testing.T, value string) time.Time {
t.Helper()
res, err := time.Parse(time.RFC3339, value)
if err != nil {
t.Fatalf("failed to parse time %s", value)
}
return res
}
func TestTime(t *testing.T) {
tests := []struct {
name string
flagDefault time.Time
input []string
formats []string
expectedErr string
expectedValue time.Time
}{
{
name: "no value passed",
input: []string{},
flagDefault: time.Time{},
expectedErr: "",
expectedValue: time.Time{},
},
{
name: "empty value passed",
input: []string{""},
flagDefault: time.Time{},
expectedErr: `invalid argument "" for "--st" flag: invalid time format '' must be one of: '2006-01-02T15:04:05.999999999Z07:00'`,
},
{
name: "invalid datetime",
input: []string{"blabla"},
flagDefault: time.Time{},
expectedErr: `invalid argument "blabla" for "--st" flag: invalid time format 'blabla' must be one of: '2006-01-02T15:04:05.999999999Z07:00'`,
},
{
name: "no csv",
input: []string{"2022-01-01T01:01:01Z,2021-01-01T01:01:01Z"},
flagDefault: time.Time{},
expectedErr: `invalid argument "2022-01-01T01:01:01Z,2021-01-01T01:01:01Z" for "--st" flag: invalid time format '2022-01-01T01:01:01Z,2021-01-01T01:01:01Z' must be one of: '2006-01-02T15:04:05.999999999Z07:00'`,
},
{
name: "empty defaults",
input: []string{"2022-01-01T01:01:01Z"},
flagDefault: time.Time{},
expectedValue: parseTime(t, "2022-01-01T01:01:01Z"),
},
{
name: "with default values",
input: []string{"2022-01-01T01:01:01Z"},
flagDefault: parseTime(t, "2021-01-01T01:01:01Z"),
expectedValue: parseTime(t, "2022-01-01T01:01:01Z"),
},
{
name: "trims input",
input: []string{" 2022-01-01T01:01:01Z "},
flagDefault: time.Time{},
expectedValue: parseTime(t, "2022-01-01T01:01:01Z"),
},
}
t.Parallel()
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
var st time.Time
f := zflag.NewFlagSet("test", zflag.ContinueOnError)
f.SetOutput(ioutil.Discard)
formats := test.formats
if len(formats) == 0 {
formats = []string{time.RFC3339Nano}
}
f.TimeVar(&st, "st", test.flagDefault, formats, "usage")
err := f.Parse(repeatFlag("--st", test.input...))
if test.expectedErr != "" {
assertErrMsg(t, test.expectedErr, err)
return
}
assertNoErr(t, err)
assertEqual(t, test.expectedValue, st)
getTime, err := f.GetTime("st")
assertNoErr(t, err)
assertEqual(t, test.expectedValue, getTime)
getTimeGet, err := f.Get("st")
assertNoErr(t, err)
assertEqual(t, test.expectedValue, getTimeGet)
defer assertNoPanic(t)()
mustTime := f.MustGetTime("st")
assertEqual(t, test.expectedValue, mustTime)
})
}
}