-
Notifications
You must be signed in to change notification settings - Fork 6
/
datetime_test.go
283 lines (249 loc) · 8.54 KB
/
datetime_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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
// Copyright (c) The microformats project authors.
// SPDX-License-Identifier: MIT
package microformats
import (
"testing"
"time"
"github.com/google/go-cmp/cmp"
)
//nolint:dupl // Test_Datetime_SetDate is very similar to but different from Test_Datetime_SetTime
func Test_Datetime_SetDate(t *testing.T) {
var d datetime
// ref is the first time.Time value set. All subsequent values set
// should have no effect.
ref := time.Date(2000, 1, 2, 0, 0, 0, 0, time.UTC)
for _, tt := range []time.Time{
ref,
time.Date(2003, 4, 5, 0, 0, 0, 0, time.UTC),
time.Date(2003, 4, 5, 6, 7, 8, 0, time.UTC),
{},
} {
d.setDate(tt.Year(), tt.Month(), tt.Day())
// check that has* flags are set properly
if got, want := d.hasDate, true; got != want {
t.Errorf("datetime(%v) hasDate: %t, want %t", tt, got, want)
}
if got, want := d.hasTime, false; got != want {
t.Errorf("datetime(%v) hasTime: %t, want %t", tt, got, want)
}
if got, want := d.hasTZ, false; got != want {
t.Errorf("datetime(%v) hasTZ: %t, want %t", tt, got, want)
}
if got, want := d.t, ref; !got.Equal(want) {
t.Errorf("datetime(%v) has time: %v, want %v", tt, got, want)
}
}
}
//nolint:dupl // Test_Datetime_SetDate is very similar to but different from Test_Datetime_SetTime
func Test_Datetime_SetTime(t *testing.T) {
var d datetime
// ref is the first time.Time value set. All subsequent values set
// should have no effect. date set to 0001-01-01 to match zero value.
ref := time.Date(1, 1, 1, 1, 2, 3, 0, time.UTC)
for _, tt := range []time.Time{
ref,
time.Date(0, 0, 0, 4, 5, 6, 0, time.UTC),
time.Date(2000, 1, 3, 4, 5, 6, 0, time.UTC),
{},
} {
d.setTime(tt.Hour(), tt.Minute(), tt.Second())
// check that has* flags are set properly
if got, want := d.hasDate, false; got != want {
t.Errorf("datetime(%v) hasDate: %t, want %t", tt, got, want)
}
if got, want := d.hasTime, true; got != want {
t.Errorf("datetime(%v) hasTime: %t, want %t", tt, got, want)
}
if got, want := d.hasTZ, false; got != want {
t.Errorf("datetime(%v) hasTZ: %t, want %t", tt, got, want)
}
if got, want := d.t, ref; !got.Equal(want) {
t.Errorf("datetime(%v) has time: %v, want %v", tt, got, want)
}
}
}
func Test_Datetime_SetTZ(t *testing.T) {
var d datetime
// ref is the first time.Time value set. All subsequent values set
// should have no effect. date set to 0001-01-01 to match zero value.
ref := time.Date(1, 1, 1, 0, 0, 0, 0, time.FixedZone("", -8*60*60))
for _, tt := range []time.Time{
ref,
time.Date(2000, 1, 3, 4, 5, 6, 0, time.UTC),
{},
} {
d.setTZ(tt.Location())
// check that has* flags are set properly
if got, want := d.hasDate, false; got != want {
t.Errorf("datetime(%v) hasDate: %t, want %t", tt, got, want)
}
if got, want := d.hasTime, false; got != want {
t.Errorf("datetime(%v) hasTime: %t, want %t", tt, got, want)
}
if got, want := d.hasTZ, true; got != want {
t.Errorf("datetime(%v) hasTZ: %t, want %t", tt, got, want)
}
if got, want := d.t, ref; !got.Equal(want) {
t.Errorf("datetime(%v) has time: %v, want %v", tt, got, want)
}
}
}
func Test_Datetime_String(t *testing.T) {
ref := time.Date(2006, 1, 2, 15, 04, 05, 0, time.UTC)
tests := []struct {
datetime datetime
want string
}{
{datetime{t: ref}, ""},
{datetime{t: ref, hasDate: true}, "2006-01-02"},
{datetime{t: ref, hasDate: true, hasTime: true}, "2006-01-02 15:04"},
{
datetime{t: ref, hasDate: true, hasTime: true, hasSeconds: true},
"2006-01-02 15:04:05",
},
{
datetime{t: ref, hasDate: true, hasTime: true, hasTZ: true},
"2006-01-02 15:04Z",
},
{
datetime{t: ref, hasDate: true, hasTime: true, hasSeconds: true, hasTZ: true},
"2006-01-02 15:04:05Z",
},
{
datetime{
t: time.Date(2006, 1, 2, 15, 04, 05, 0, time.FixedZone("", -5*60*60)),
hasDate: true, hasTime: true, hasSeconds: true, hasTZ: true,
},
"2006-01-02 15:04:05-0500",
},
}
for _, tt := range tests {
if got, want := tt.datetime.String(), tt.want; got != want {
t.Errorf("datetime(%v).String returned %v, want %v", tt.datetime, got, want)
}
}
}
func Test_Datetime_Parse(t *testing.T) {
tests := []struct {
input string
want time.Time
}{
{"2000-01-02T03:04:05Z", time.Date(2000, 1, 2, 3, 4, 5, 0, time.UTC)},
{"2000-01-02t03:04Z", time.Date(2000, 1, 2, 3, 4, 0, 0, time.UTC)},
{"2000-01-02 03:04:05Z", time.Date(2000, 1, 2, 3, 4, 5, 0, time.UTC)},
{"2000-01-02", time.Date(2000, 1, 2, 0, 0, 0, 0, time.UTC)},
{"2000-145", time.Date(2000, 5, 24, 0, 0, 0, 0, time.UTC)},
{"03:04:05-05:00", time.Date(1, 1, 1, 3, 4, 5, 0, time.FixedZone("", -5*60*60))},
{"03:04:05+05:00", time.Date(1, 1, 1, 3, 4, 5, 0, time.FixedZone("", 5*60*60))},
{"03:04:05-0500", time.Date(1, 1, 1, 3, 4, 5, 0, time.FixedZone("", -5*60*60))},
{"03:04:05+0500", time.Date(1, 1, 1, 3, 4, 5, 0, time.FixedZone("", 5*60*60))},
{"03:04:05Z", time.Date(1, 1, 1, 3, 4, 5, 0, time.UTC)},
{"03:04:05", time.Date(1, 1, 1, 3, 4, 5, 0, time.UTC)},
{"03:04-05:00", time.Date(1, 1, 1, 3, 4, 0, 0, time.FixedZone("", -5*60*60))},
{"03:04+05:00", time.Date(1, 1, 1, 3, 4, 0, 0, time.FixedZone("", 5*60*60))},
{"03:04-0500", time.Date(1, 1, 1, 3, 4, 0, 0, time.FixedZone("", -5*60*60))},
{"03:04+0500", time.Date(1, 1, 1, 3, 4, 0, 0, time.FixedZone("", 5*60*60))},
{"03:04Z", time.Date(1, 1, 1, 3, 4, 0, 0, time.UTC)},
{"03:04", time.Date(1, 1, 1, 3, 4, 0, 0, time.UTC)},
{"03:04:05am", time.Date(1, 1, 1, 3, 4, 5, 0, time.UTC)},
{"03:04:05pm", time.Date(1, 1, 1, 15, 4, 5, 0, time.UTC)},
{"03:04AM", time.Date(1, 1, 1, 3, 4, 0, 0, time.UTC)},
{"03:04PM", time.Date(1, 1, 1, 15, 4, 0, 0, time.UTC)},
{"03a.m.", time.Date(1, 1, 1, 3, 0, 0, 0, time.UTC)},
{"03p.m.", time.Date(1, 1, 1, 15, 0, 0, 0, time.UTC)},
{"-05:00", time.Date(1, 1, 1, 0, 0, 0, 0, time.FixedZone("", -5*60*60))},
{"+05:00", time.Date(1, 1, 1, 0, 0, 0, 0, time.FixedZone("", 5*60*60))},
{"-0500", time.Date(1, 1, 1, 0, 0, 0, 0, time.FixedZone("", -5*60*60))},
{"+0500", time.Date(1, 1, 1, 0, 0, 0, 0, time.FixedZone("", 5*60*60))},
{"-05", time.Date(1, 1, 1, 0, 0, 0, 0, time.FixedZone("", -5*60*60))},
{"+05", time.Date(1, 1, 1, 0, 0, 0, 0, time.FixedZone("", 5*60*60))},
{"Z", time.Date(1, 1, 1, 0, 0, 0, 0, time.UTC)},
}
for _, tt := range tests {
var d datetime
d.Parse(tt.input)
if got, want := d.t, tt.want; !got.Equal(want) {
t.Errorf("datetime.Parse(%v) returned %v, want %v", tt.input, got, want)
}
}
}
func Test_GetDateTimeValue(t *testing.T) {
tests := []struct {
html string
value *string
}{
{``, nil},
{`<p><time class="value" datetime="2015-02-03T21:15:00-08:00"><time></p>`, ptr("2015-02-03 21:15:00-0800")},
{`<p>
<time class="value" datetime="2015-02-03"></time>
<time class="value" datetime="21:15:00"></time>
<time class="value" datetime="-08:00"></time>
</p>`, ptr("2015-02-03 21:15:00-0800")},
}
for _, tt := range tests {
n, err := parseNode(tt.html)
if err != nil {
t.Fatalf("Error parsing HTML: %v", err)
}
if got, want := getDateTimeValue(n), tt.value; !cmp.Equal(got, want) {
t.Errorf("getDateTimeValue(%q) returned %v, want %v", tt.html, *got, *want)
}
}
}
func Test_ImplyEndDate(t *testing.T) {
tests := []struct {
description string
start, end []string
want []any
}{
{
"invalid dates",
[]string{"foo"},
[]string{"bar"},
[]any{"bar"},
},
{
"single start and end date",
[]string{"2006-01-02 03:04:05"},
[]string{"01:02:03"},
[]any{"2006-01-02 01:02:03"},
},
{
"single start and end date, end has date",
[]string{"2006-01-02 03:04:05"},
[]string{"2007-01-02 01:02:03"},
[]any{"2007-01-02 01:02:03"},
},
{
"multiple start dates",
[]string{"2006-01-02 03:04:05", "2007-01-02"},
[]string{"01:02:03"},
[]any{"2006-01-02 01:02:03"},
},
{
"multiple start dates, first with no date",
[]string{"03:04:05", "2007-01-02"},
[]string{"01:02:03"},
[]any{"2007-01-02 01:02:03"},
},
{
"multiple start and end dates",
[]string{"03:04:05", "2007-01-02"},
[]string{"01:02:03", "2006-01-02 01:02:03"},
[]any{"2007-01-02 01:02:03", "2006-01-02 01:02:03"},
},
}
for _, tt := range tests {
item := &Microformat{Properties: map[string][]any{}}
for _, d := range tt.start {
item.Properties["start"] = append(item.Properties["start"], d)
}
for _, d := range tt.end {
item.Properties["end"] = append(item.Properties["end"], d)
}
implyEndDate(item)
if got, want := item.Properties["end"], tt.want; !cmp.Equal(got, want) {
t.Errorf("implyEndDate(%q, %q) returned %#v, want %#v", tt.start, tt.end, got, want)
}
}
}