-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimeString_test.go
103 lines (100 loc) · 1.95 KB
/
timeString_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
package tt
import (
"reflect"
"testing"
"time"
)
func TestParseDate(t *testing.T) {
now := time.Now()
tests := []struct {
name string
arg string
want time.Time
wantErr bool
}{
{
"only time",
"18:04",
time.Date(now.Year(), now.Month(), now.Day(), 18, 04, 0, 0, now.Location()),
false,
},
{
"date and time using slash and space",
"2021/08/10 18:04:01",
time.Date(2021, time.Month(8), 10, 18, 4, 1, 0, now.Location()),
false,
},
{
"single digits in time",
"2021/08/10 0:4",
time.Date(2021, time.Month(8), 10, 0, 4, 0, 0, now.Location()),
false,
},
{
"date and time using dashes and T",
"2021-08-10T0:4",
time.Date(2021, time.Month(8), 10, 0, 4, 0, 0, now.Location()),
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got, err := ParseTime(tt.arg); (err != nil) != tt.wantErr {
t.Errorf("ParseTime() error = %v, wantErr %v", err, tt.wantErr)
} else if !reflect.DeepEqual(got, tt.want) {
t.Errorf("ParseTime() = %v, want %v", got, tt.want)
}
})
}
}
func TestFormatDurationCustom(t *testing.T) {
type args struct {
d time.Duration
precision time.Duration
}
tests := []struct {
name string
args args
want string
}{
{
"precision seconds positive",
args{
time.Minute*2 + time.Second*12,
time.Second,
},
"00h02m12s",
},
{
"precision minutes positive",
args{
time.Minute*7 + time.Second*12,
time.Minute,
},
"00h07m",
},
{
"precision hours positive",
args{
time.Minute*2 + time.Second*12,
time.Hour,
},
"00h",
},
{
"precision minute negative",
args{
-(time.Minute*2 + time.Second*12),
time.Second,
},
"-00h02m12s",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := FormatDurationCustom(tt.args.d, tt.args.precision); got != tt.want {
t.Errorf("FormatDurationCustom() = %v, want %v", got, tt.want)
}
})
}
}