forked from robertkrimen/otto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
date_test.go
481 lines (353 loc) · 12.3 KB
/
date_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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
package otto
import (
"math"
"testing"
"time"
)
func mockTimeLocal(location *time.Location) func() {
local := time.Local
time.Local = location
return func() {
time.Local = local
}
}
// Passing or failing should not be dependent on what time zone we're in
func mockUTC() func() {
return mockTimeLocal(time.UTC)
}
func TestDate(t *testing.T) {
tt(t, func() {
test, _ := test()
defer mockUTC()()
time0 := time.Unix(1348616313, 47*1000*1000).Local()
test(`Date`, "function Date() { [native code] }")
test(`new Date(0).toUTCString()`, "Thu, 01 Jan 1970 00:00:00 UTC")
test(`new Date(0).toGMTString()`, "Thu, 01 Jan 1970 00:00:00 GMT")
if false {
// TODO toLocale{Date,Time}String
test(`new Date(0).toLocaleString()`, "")
test(`new Date(0).toLocaleDateString()`, "")
test(`new Date(0).toLocaleTimeString()`, "")
}
test(`new Date(1348616313).getTime()`, 1348616313)
test(`new Date(1348616313).toUTCString()`, "Fri, 16 Jan 1970 14:36:56 UTC")
test(`abc = new Date(1348616313047); abc.toUTCString()`, "Tue, 25 Sep 2012 23:38:33 UTC")
test(`abc.getYear()`, time0.Year()-1900)
test(`abc.getFullYear()`, time0.Year())
test(`abc.getUTCFullYear()`, 2012)
test(`abc.getMonth()`, int(time0.Month())-1) // Remember, the JavaScript month is 0-based
test(`abc.getUTCMonth()`, 8)
test(`abc.getDate()`, time0.Day())
test(`abc.getUTCDate()`, 25)
test(`abc.getDay()`, int(time0.Weekday()))
test(`abc.getUTCDay()`, 2)
test(`abc.getHours()`, time0.Hour())
test(`abc.getUTCHours()`, 23)
test(`abc.getMinutes()`, time0.Minute())
test(`abc.getUTCMinutes()`, 38)
test(`abc.getSeconds()`, time0.Second())
test(`abc.getUTCSeconds()`, 33)
test(`abc.getMilliseconds()`, time0.Nanosecond()/(1000*1000)) // In honor of the 47%
test(`abc.getUTCMilliseconds()`, 47)
_, offset := time0.Zone()
test(`abc.getTimezoneOffset()`, offset/-60)
test(`new Date("Xyzzy").getTime()`, math.NaN())
test(`abc.setFullYear(2011); abc.toUTCString()`, "Sun, 25 Sep 2011 23:38:33 UTC")
test(`new Date(12564504e5).toUTCString()`, "Sun, 25 Oct 2009 06:00:00 UTC")
test(`new Date(2009, 9, 25).toUTCString()`, "Sun, 25 Oct 2009 00:00:00 UTC")
test(`+(new Date(2009, 9, 25))`, 1256428800000)
format := "Mon, 2 Jan 2006 15:04:05 MST"
time1 := time.Unix(1256450400, 0)
time0 = time.Date(time1.Year(), time1.Month(), time1.Day(), time1.Hour(), time1.Minute(), time1.Second(), time1.Nanosecond(), time1.Location()).UTC()
time0 = time.Date(time1.Year(), time1.Month(), time1.Day(), time1.Hour(), time1.Minute(), time1.Second(), 2001*1000*1000, time1.Location()).UTC()
test(`abc = new Date(12564504e5); abc.setMilliseconds(2001); abc.toUTCString()`, time0.Format(format))
time0 = time.Date(time1.Year(), time1.Month(), time1.Day(), time1.Hour(), time1.Minute(), 61, time1.Nanosecond(), time1.Location()).UTC()
test(`abc = new Date(12564504e5); abc.setSeconds("61"); abc.toUTCString()`, time0.Format(format))
time0 = time.Date(time1.Year(), time1.Month(), time1.Day(), time1.Hour(), 61, time1.Second(), time1.Nanosecond(), time1.Location()).UTC()
test(`abc = new Date(12564504e5); abc.setMinutes("61"); abc.toUTCString()`, time0.Format(format))
time0 = time.Date(time1.Year(), time1.Month(), time1.Day(), 5, time1.Minute(), time1.Second(), time1.Nanosecond(), time1.Location()).UTC()
test(`abc = new Date(12564504e5); abc.setHours("5"); abc.toUTCString()`, time0.Format(format))
time0 = time.Date(time1.Year(), time1.Month(), 26, time1.Hour(), time1.Minute(), time1.Second(), time1.Nanosecond(), time1.Location()).UTC()
test(`abc = new Date(12564504e5); abc.setDate("26"); abc.toUTCString()`, time0.Format(format))
time0 = time.Date(time1.Year(), 10, time1.Day(), time1.Hour(), time1.Minute(), time1.Second(), time1.Nanosecond(), time1.Location()).UTC()
test(`abc = new Date(12564504e5); abc.setMonth(9); abc.toUTCString()`, time0.Format(format))
test(`abc = new Date(12564504e5); abc.setMonth("09"); abc.toUTCString()`, time0.Format(format))
time0 = time.Date(time1.Year(), 11, time1.Day(), time1.Hour(), time1.Minute(), time1.Second(), time1.Nanosecond(), time1.Location()).UTC()
test(`abc = new Date(12564504e5); abc.setMonth("10"); abc.toUTCString()`, time0.Format(format))
time0 = time.Date(2010, time1.Month(), time1.Day(), time1.Hour(), time1.Minute(), time1.Second(), time1.Nanosecond(), time1.Location()).UTC()
test(`abc = new Date(12564504e5); abc.setFullYear(2010); abc.toUTCString()`, time0.Format(format))
test(`new Date("2001-01-01T10:01:02.000").getTime()`, 978343262000)
// Date()
test(`typeof Date()`, "string")
test(`typeof Date(2006, 1, 2)`, "string")
test(`
abc = Object.getOwnPropertyDescriptor(Date, "parse");
[ abc.value === Date.parse, abc.writable, abc.enumerable, abc.configurable ];
`, "true,true,false,true")
test(`
abc = Object.getOwnPropertyDescriptor(Date.prototype, "toTimeString");
[ abc.value === Date.prototype.toTimeString, abc.writable, abc.enumerable, abc.configurable ];
`, "true,true,false,true")
test(`
var abc = Object.getOwnPropertyDescriptor(Date, "prototype");
[ [ typeof Date.prototype ],
[ abc.writable, abc.enumerable, abc.configurable ] ];
`, "object,false,false,false")
})
}
func TestDate_parse(t *testing.T) {
tt(t, func() {
test, _ := test()
defer mockUTC()()
test(`Date.parse("2001-01-01T10:01:02.000")`, 978343262000)
test(`Date.parse("2006-01-02T15:04:05.000")`, 1136214245000)
test(`Date.parse("2006")`, 1136073600000)
test(`Date.parse("1970-01-16T14:36:56+00:00")`, 1348616000)
test(`Date.parse("1970-01-16T14:36:56.313+00:00")`, 1348616313)
test(`Date.parse("1970-01-16T14:36:56.000")`, 1348616000)
test(`Date.parse.length`, 1)
})
}
func TestDate_UTC(t *testing.T) {
tt(t, func() {
test, _ := test()
defer mockUTC()()
test(`Date.UTC(2009, 9, 25)`, 1256428800000)
test(`Date.UTC.length`, 7)
})
}
func TestDate_now(t *testing.T) {
tt(t, func() {
test, _ := test()
defer mockUTC()()
// FIXME I think this too risky
test(`+(""+Date.now()).substr(0, 10)`, float64(epochToInteger(timeToEpoch(time.Now()))/1000))
test(`Date.now() - Date.now(1,2,3) < 24 * 60 * 60`, true)
})
}
func TestDate_toISOString(t *testing.T) {
tt(t, func() {
test, _ := test()
defer mockUTC()()
test(`new Date(0).toISOString()`, "1970-01-01T00:00:00.000Z")
})
}
func TestDate_toJSON(t *testing.T) {
tt(t, func() {
test, _ := test()
defer mockUTC()()
test(`new Date(0).toJSON()`, "1970-01-01T00:00:00.000Z")
})
}
func TestDate_setYear(t *testing.T) {
tt(t, func() {
test, _ := test()
defer mockUTC()()
test(`new Date(12564504e5).setYear(96)`, 846223200000)
test(`new Date(12564504e5).setYear(1996)`, 846223200000)
test(`new Date(12564504e5).setYear(2000)`, 972453600000)
})
}
func TestDateDefaultValue(t *testing.T) {
tt(t, func() {
test, _ := test()
defer mockUTC()()
test(`
var date = new Date();
date + 0 === date.toString() + "0";
`, true)
})
}
func TestDate_April1978(t *testing.T) {
tt(t, func() {
test, _ := test()
defer mockUTC()()
test(`
var abc = new Date(1978,3);
[ abc.getYear(), abc.getMonth(), abc.valueOf() ];
`, "78,3,260236800000")
})
}
func TestDate_setMilliseconds(t *testing.T) {
tt(t, func() {
test, _ := test()
defer mockUTC()()
test(`
abc = new Date();
def = abc.setMilliseconds();
[ abc, def ];
`, "Invalid Date,NaN")
})
}
func TestDate_new(t *testing.T) {
// FIXME?
// This is probably incorrect, due to differences in Go date/time handling
// versus ECMA date/time handling, but we'll leave this here for
// future reference
if true {
return
}
tt(t, func() {
test, _ := test()
defer mockUTC()()
test(`
[
new Date(1899, 11).valueOf(),
new Date(1899, 12).valueOf(),
new Date(1900, 0).valueOf()
]
`, "-2211638400000,-2208960000000,-2208960000000")
})
}
func TestDateComparison(t *testing.T) {
tt(t, func() {
test, _ := test()
defer mockUTC()()
test(`
var now0 = Date.now();
var now1 = (new Date()).toString();
[ now0 === now1, Math.abs(now0 - Date.parse(now1)) <= 1000 ];
`, "false,true")
})
}
func TestDate_setSeconds(t *testing.T) {
tt(t, func() {
test, _ := test()
defer mockUTC()()
test(`
abc = new Date(1980, 10);
def = new Date(abc);
abc.setSeconds(10, 12);
def.setSeconds(10);
def.setMilliseconds(12);
abc.valueOf() === def.valueOf();
`, true)
test(`
abc = new Date(1980, 10);
def = new Date(abc);
abc.setUTCSeconds(10, 12);
def.setUTCSeconds(10);
def.setUTCMilliseconds(12);
abc.valueOf() === def.valueOf();
`, true)
test(`Date.prototype.setSeconds.length`, 2)
test(`Date.prototype.setUTCSeconds.length`, 2)
})
}
func TestDate_setMinutes(t *testing.T) {
tt(t, func() {
test, _ := test()
defer mockUTC()()
test(`
abc = new Date(1980, 10);
def = new Date(abc);
abc.setMinutes(8, 10, 12);
def.setMinutes(8);
def.setSeconds(10);
def.setMilliseconds(12);
abc.valueOf() === def.valueOf();
`, true)
test(`
abc = new Date(1980, 10);
def = new Date(abc);
abc.setUTCMinutes(8, 10, 12);
def.setUTCMinutes(8);
def.setUTCSeconds(10);
def.setUTCMilliseconds(12);
abc.valueOf() === def.valueOf();
`, true)
test(`Date.prototype.setMinutes.length`, 3)
test(`Date.prototype.setUTCMinutes.length`, 3)
})
}
func TestDate_setHours(t *testing.T) {
tt(t, func() {
test, _ := test()
defer mockUTC()()
test(`
abc = new Date(1980, 10);
def = new Date(abc);
abc.setHours(6, 8, 10, 12);
def.setHours(6);
def.setMinutes(8);
def.setSeconds(10);
def.setMilliseconds(12);
abc.valueOf() === def.valueOf();
`, true)
test(`
abc = new Date(1980, 10);
def = new Date(abc);
abc.setUTCHours(6, 8, 10, 12);
def.setUTCHours(6);
def.setUTCMinutes(8);
def.setUTCSeconds(10);
def.setUTCMilliseconds(12);
abc.valueOf() === def.valueOf();
`, true)
test(`Date.prototype.setHours.length`, 4)
test(`Date.prototype.setUTCHours.length`, 4)
})
}
func TestDate_setMonth(t *testing.T) {
tt(t, func() {
test, _ := test()
defer mockUTC()()
test(`
abc = new Date(1980, 10);
def = new Date(abc);
abc.setMonth(6, 8);
def.setMonth(6);
def.setDate(8);
abc.valueOf() === def.valueOf();
`, true)
test(`
abc = new Date(1980, 10);
def = new Date(abc);
abc.setUTCMonth(6, 8);
def.setUTCMonth(6);
def.setUTCDate(8);
abc.valueOf() === def.valueOf();
`, true)
test(`Date.prototype.setMonth.length`, 2)
test(`Date.prototype.setUTCMonth.length`, 2)
})
}
func TestDate_setFullYear(t *testing.T) {
tt(t, func() {
test, _ := test()
defer mockUTC()()
test(`
abc = new Date(1980, 10);
def = new Date(abc);
abc.setFullYear(1981, 6, 8);
def.setFullYear(1981);
def.setMonth(6);
def.setDate(8);
abc.valueOf() === def.valueOf();
`, true)
test(`
abc = new Date(1980, 10);
def = new Date(abc);
abc.setUTCFullYear(1981, 6, 8);
def.setUTCFullYear(1981);
def.setUTCMonth(6);
def.setUTCDate(8);
abc.valueOf() === def.valueOf();
`, true)
test(`Date.prototype.setFullYear.length`, 3)
test(`Date.prototype.setUTCFullYear.length`, 3)
})
}
func TestDate_setTime(t *testing.T) {
tt(t, func() {
test, _ := test()
defer mockUTC()()
test(`
var abc = new Date(1999, 6, 1);
var def = new Date();
def.setTime(abc.getTime());
[ def, abc.valueOf() == def.valueOf() ];
`, "Thu, 01 Jul 1999 00:00:00 UTC,true")
test(`Date.prototype.setTime.length`, 1)
})
}