-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathduration_test.go
203 lines (169 loc) · 3.55 KB
/
duration_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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package duration
import (
"errors"
"testing"
"time"
"github.com/google/go-cmp/cmp"
)
func mustParse(t *testing.T, s string) Duration {
d, err := ParseRFC3339(s)
if err != nil {
t.Fatal(err)
}
return d
}
func TestParseRFC3339(t *testing.T) {
tests := []struct {
s string
expected Duration
expectedErr error
}{
{
s: "P3Y6M4DT12H30M5S",
expected: Duration{
Years: 3,
Months: 6,
Days: 4,
Hours: 12,
Minutes: 30,
Seconds: 5,
},
},
{
s: "P5W",
expected: Duration{
Weeks: 5,
},
},
{
s: "P3Y5W",
expectedErr: errors.New("must be RFC3999 formatted duration"),
},
{
s: "123",
expectedErr: errors.New("must be RFC3999 formatted duration"),
},
}
for _, tt := range tests {
t.Run(tt.s, func(t *testing.T) {
got, gotErr := ParseRFC3339(tt.s)
switch {
case gotErr == nil && tt.expectedErr != nil:
t.Errorf("ParseRFC3339(%q) returned no error, expected error %q", tt.s, tt.expectedErr)
return
case gotErr != nil && tt.expectedErr == nil:
t.Errorf("ParseRFC3339(%q) returned error %q, expected no error", tt.s, gotErr)
return
case gotErr != nil && tt.expectedErr != nil:
if tt.expectedErr.Error() != gotErr.Error() {
t.Errorf("ParseRFC3339(%q) returned error %q, expected error %q", tt.s, gotErr, tt.expectedErr)
}
}
if diff := cmp.Diff(tt.expected, got); diff != "" {
t.Errorf("expected ParseRFC3339(%q) mismatch (-want +got):\n%s", tt.s, diff)
}
})
}
}
func TestDuration_FormatRFC3339(t *testing.T) {
tests := []struct {
d Duration
expected string
}{
{
d: Duration{
Years: 3,
Months: 6,
Days: 4,
Hours: 12,
Minutes: 30,
Seconds: 5,
},
expected: "P3Y6M4DT12H30M5S",
},
{
d: Duration{
Weeks: 5,
},
expected: "P5W",
},
}
for _, tt := range tests {
t.Run(tt.expected, func(t *testing.T) {
got := tt.d.FormatRFC3339()
if got != tt.expected {
t.Errorf("expected %+v.FormatRFC3339() returned %s, expected %s", tt.d, got, tt.expected)
}
})
}
}
func TestDuration_AddToTime(t *testing.T) {
tests := []struct {
t string
d string
expected string
}{
{
d: "P3Y6M4DT12H30M5S",
t: "2006-01-02T15:04:05Z",
expected: "2009-07-07T03:34:10Z",
},
{
d: "P5W",
t: "2006-01-02T15:04:05Z",
expected: "2006-02-06T15:04:05Z",
},
}
for _, tt := range tests {
t.Run(tt.expected, func(t *testing.T) {
dur := mustParse(t, tt.d)
tim, err := time.Parse(time.RFC3339, tt.t)
if err != nil {
t.Fatal(err)
}
expected, err := time.Parse(time.RFC3339, tt.expected)
if err != nil {
t.Fatal(err)
}
got := dur.AddToTime(tim)
if !got.Equal(expected) {
t.Errorf("expected %s.AddToTime(%s) returned %s, expected %s", dur, tim, got, expected)
}
})
}
}
func TestDuration_SubtractFromTime(t *testing.T) {
tests := []struct {
t string
d string
expected string
}{
{
d: "P3Y6M4DT12H30M5S",
t: "2006-01-02T15:04:05Z",
expected: "2002-06-28T02:34:00Z",
},
{
d: "P5W",
t: "2006-01-02T15:04:05Z",
expected: "2005-11-28T15:04:05Z",
},
}
for _, tt := range tests {
t.Run(tt.expected, func(t *testing.T) {
dur := mustParse(t, tt.d)
tim, err := time.Parse(time.RFC3339, tt.t)
if err != nil {
t.Fatal(err)
}
expected, err := time.Parse(time.RFC3339, tt.expected)
if err != nil {
t.Fatal(err)
}
got := dur.SubtractFromTime(tim)
if !got.Equal(expected) {
t.Errorf("expected %s.SubtractFromTime(%s) returned %s, expected %s", dur, tim, got, expected)
}
})
}
}