-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflag_reflect_time_test.go
66 lines (56 loc) · 1.59 KB
/
flag_reflect_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
package clif
import (
"context"
"reflect"
"testing"
"time"
)
func TestNewValueFromTime_success(t *testing.T) {
t.Parallel()
type testCase struct {
input FlagValue
expected time.Time
}
testCases := map[string]testCase{
"6 July 2020": {input: FlagValue{Set: true, Raw: "6 July 2020"}, expected: time.Date(2020, time.July, 6, 0, 0, 0, 0, time.UTC)},
"07/06/2020": {input: FlagValue{Set: true, Raw: "07/06/2020"}, expected: time.Date(2020, time.July, 6, 0, 0, 0, 0, time.UTC)},
"07/06/20": {input: FlagValue{Set: true, Raw: "07/06/20"}, expected: time.Date(2020, time.July, 6, 0, 0, 0, 0, time.UTC)},
}
for name, test := range testCases {
t.Run(name, func(t *testing.T) {
t.Parallel()
ctx := context.Background()
var target time.Time
res, err := newValueFromTime(ctx, test.input, reflect.ValueOf(target))
if err != nil {
t.Fatalf("Unexpected error %s", err)
}
got, ok := res.Interface().(time.Time)
if !ok {
t.Fatalf("Expected result to be time.Time, was %T", res.Interface())
}
if !got.Equal(test.expected) {
t.Fatalf("Expected %v, got %v", test.expected, got)
}
})
}
}
func TestNewValueFromTime_error(t *testing.T) {
t.Parallel()
testCases := map[string]string{
"empty": "",
"mixedCase": "trUE",
"invalidValue": "yes",
}
for name, input := range testCases {
t.Run(name, func(t *testing.T) {
t.Parallel()
ctx := context.Background()
var target bool
_, err := newValueFromTime(ctx, FlagValue{Set: true, Raw: input}, reflect.ValueOf(target))
if err == nil {
t.Fatal("Expected error, got none")
}
})
}
}