forked from dop251/goja
-
Notifications
You must be signed in to change notification settings - Fork 0
/
date_test.go
322 lines (249 loc) · 9.24 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
package goja
import (
"testing"
"time"
)
const TESTLIB = `
function $ERROR(message) {
throw new Error(message);
}
function assert(mustBeTrue, message) {
if (mustBeTrue === true) {
return;
}
if (message === undefined) {
message = 'Expected true but got ' + String(mustBeTrue);
}
$ERROR(message);
}
assert._isSameValue = function (a, b) {
if (a === b) {
// Handle +/-0 vs. -/+0
return a !== 0 || 1 / a === 1 / b;
}
// Handle NaN vs. NaN
return a !== a && b !== b;
};
assert.sameValue = function (actual, expected, message) {
if (assert._isSameValue(actual, expected)) {
return;
}
if (message === undefined) {
message = '';
} else {
message += ' ';
}
message += 'Expected SameValue(«' + String(actual) + '», «' + String(expected) + '») to be true';
$ERROR(message);
};
`
func TestDateUTC(t *testing.T) {
const SCRIPT = `
assert.sameValue(Date.UTC(1970, 0), 0, '1970, 0');
assert.sameValue(Date.UTC(2016, 0), 1451606400000, '2016, 0');
assert.sameValue(Date.UTC(2016, 6), 1467331200000, '2016, 6');
assert.sameValue(Date.UTC(2016, 6, 1), 1467331200000, '2016, 6, 1');
assert.sameValue(Date.UTC(2016, 6, 5), 1467676800000, '2016, 6, 5');
assert.sameValue(Date.UTC(2016, 6, 5, 0), 1467676800000, '2016, 6, 5, 0');
assert.sameValue(Date.UTC(2016, 6, 5, 15), 1467730800000, '2016, 6, 5, 15');
assert.sameValue(
Date.UTC(2016, 6, 5, 15, 0), 1467730800000, '2016, 6, 5, 15, 0'
);
assert.sameValue(
Date.UTC(2016, 6, 5, 15, 34), 1467732840000, '2016, 6, 5, 15, 34'
);
assert.sameValue(
Date.UTC(2016, 6, 5, 15, 34, 0), 1467732840000, '2016, 6, 5, 15, 34, 0'
);
assert.sameValue(
Date.UTC(2016, 6, 5, 15, 34, 45), 1467732885000, '2016, 6, 5, 15, 34, 45'
);
`
testScript1(TESTLIB+SCRIPT, _undefined, t)
}
func TestNewDate(t *testing.T) {
const SCRIPT = `
var d1 = new Date("2016-09-01T12:34:56Z");
d1.getUTCHours() === 12;
`
testScript1(SCRIPT, valueTrue, t)
}
func TestNewDate0(t *testing.T) {
const SCRIPT = `
(new Date(0)).toUTCString();
`
testScript1(SCRIPT, asciiString("Thu Jan 01 1970 00:00:00 GMT+0000 (UTC)"), t)
}
func TestSetHour(t *testing.T) {
l := time.Local
defer func() {
time.Local = l
}()
var err error
time.Local, err = time.LoadLocation("America/New_York")
if err != nil {
t.Fatal(err)
}
const SCRIPT = `
var d = new Date(2016, 8, 1, 12, 23, 45)
assert.sameValue(d.getHours(), 12);
assert.sameValue(d.getUTCHours(), 16);
d.setHours(13);
assert.sameValue(d.getHours(), 13);
assert.sameValue(d.getMinutes(), 23);
assert.sameValue(d.getSeconds(), 45);
d.setUTCHours(13);
assert.sameValue(d.getHours(), 9);
assert.sameValue(d.getMinutes(), 23);
assert.sameValue(d.getSeconds(), 45);
`
testScript1(TESTLIB+SCRIPT, _undefined, t)
}
func TestSetMinute(t *testing.T) {
l := time.Local
defer func() {
time.Local = l
}()
time.Local = time.FixedZone("Asia/Delhi", 5*60*60+30*60)
const SCRIPT = `
var d = new Date(2016, 8, 1, 12, 23, 45)
assert.sameValue(d.getHours(), 12);
assert.sameValue(d.getUTCHours(), 6);
assert.sameValue(d.getMinutes(), 23);
assert.sameValue(d.getUTCMinutes(), 53);
d.setMinutes(55);
assert.sameValue(d.getMinutes(), 55);
assert.sameValue(d.getSeconds(), 45);
d.setUTCMinutes(52);
assert.sameValue(d.getMinutes(), 22);
assert.sameValue(d.getHours(), 13);
`
testScript1(TESTLIB+SCRIPT, _undefined, t)
}
func TestTimezoneOffset(t *testing.T) {
const SCRIPT = `
var d = new Date(0);
d.getTimezoneOffset();
`
l := time.Local
defer func() {
time.Local = l
}()
var err error
time.Local, err = time.LoadLocation("Europe/London")
if err != nil {
t.Fatal(err)
}
testScript1(SCRIPT, intToValue(-60), t)
}
func TestDateValueOf(t *testing.T) {
const SCRIPT = `
var d9 = new Date(1.23e15);
d9.valueOf();
`
testScript1(SCRIPT, intToValue(1.23e15), t)
}
func TestDateSetters(t *testing.T) {
const SCRIPT = `
assert.sameValue((new Date(0)).setMilliseconds(2345), 2345, "setMilliseconds(2345)");
assert.sameValue(new Date(1000).setMilliseconds(23450000000000), 23450000001000, "setMilliseconds(23450000000000)");
assert.sameValue((new Date(0)).setUTCMilliseconds(2345), 2345, "setUTCMilliseconds()");
assert.sameValue((new Date(0)).setSeconds(12), 12000, "setSeconds()");
assert.sameValue((new Date(0)).setUTCSeconds(12), 12000, "setUTCSeconds()");
assert.sameValue((new Date(0)).setMinutes(12), 12 * 60 * 1000, "setMinutes()");
assert.sameValue((new Date(0)).setUTCMinutes(12), 12 * 60 * 1000, "setUTCMinutes()");
assert.sameValue((new Date("2016-06-01")).setHours(1), 1464739200000, "setHours()");
assert.sameValue((new Date("2016-06-01")).setUTCHours(1), 1464742800000, "setUTCHours()");
assert.sameValue((new Date(0)).setDate(2), 86400000, "setDate()");
assert.sameValue((new Date(0)).setUTCDate(2), 86400000, "setUTCDate()");
assert.sameValue((new Date(0)).setMonth(2), 5097600000, "setMonth()");
assert.sameValue((new Date(0)).setUTCMonth(2), 5097600000, "setUTCMonth()");
assert.sameValue((new Date(0)).setFullYear(1971), 31536000000, "setFullYear()");
assert.sameValue((new Date(0)).setFullYear(1971, 2, 3), 36806400000, "setFullYear(Y,M,D)");
assert.sameValue((new Date(0)).setUTCFullYear(1971), 31536000000, "setUTCFullYear()");
assert.sameValue((new Date(0)).setUTCFullYear(1971, 2, 3), 36806400000, "setUTCFullYear(Y,M,D)");
var d = new Date();
d.setTime(1151877845000);
assert.sameValue(d.getHours(), 23, "d.getHours()");
`
l := time.Local
defer func() {
time.Local = l
}()
var err error
time.Local, err = time.LoadLocation("Europe/London")
if err != nil {
t.Fatal(err)
}
testScript1(TESTLIB+SCRIPT, _undefined, t)
}
func TestDateParse(t *testing.T) {
const SCRIPT = `
var zero = new Date(0);
assert.sameValue(zero.valueOf(), Date.parse(zero.toString()),
"Date.parse(zeroDate.toString())");
assert.sameValue(zero.valueOf(), Date.parse(zero.toUTCString()),
"Date.parse(zeroDate.toUTCString())");
assert.sameValue(zero.valueOf(), Date.parse(zero.toISOString()),
"Date.parse(zeroDate.toISOString())");
assert.sameValue(Date.parse("Mon, 02 Jan 2006 15:04:05 MST"), 1136239445000,
"Date.parse(\"Mon, 02 Jan 2006 15:04:05 MST\")");
assert.sameValue(Date.parse("Mon, 02 Jan 2006 15:04:05 GMT-07:00 (MST)"), 1136239445000,
"Date.parse(\"Mon, 02 Jan 2006 15:04:05 GMT-07:00 (MST)\")");
assert.sameValue(Date.parse("Mon, 02 Jan 2006 15:04:05 -07:00 (MST)"), 1136239445000,
"Date.parse(\"Mon, 02 Jan 2006 15:04:05 -07:00 (MST)\")");
assert.sameValue(Date.parse("Monday, 02 Jan 2006 15:04:05 -0700 (MST)"), 1136239445000,
"Date.parse(\"Monday, 02 Jan 2006 15:04:05 -0700 (MST)\")");
assert.sameValue(Date.parse("Mon Jan 02 2006 15:04:05 GMT-0700 (GMT Standard Time)"), 1136239445000,
"Date.parse(\"Mon Jan 02 2006 15:04:05 GMT-0700 (GMT Standard Time)\")");
assert.sameValue(Date.parse("2006-01-02T15:04:05.000Z"), 1136214245000,
"Date.parse(\"2006-01-02T15:04:05.000Z\")");
assert.sameValue(Date.parse("2006-06-02T15:04:05.000"), 1149260645000,
"Date.parse(\"2006-01-02T15:04:05.000\")");
assert.sameValue(Date.parse("2006-01-02T15:04:05"), 1136214245000,
"Date.parse(\"2006-01-02T15:04:05\")");
assert.sameValue(Date.parse("2006-01-02"), 1136160000000,
"Date.parse(\"2006-01-02\")");
assert.sameValue(Date.parse("2006T15:04-0700"), 1136153040000,
"Date.parse(\"2006T15:04-0700\")");
assert.sameValue(Date.parse("2006T15:04Z"), 1136127840000,
"Date.parse(\"2006T15:04Z\")");
assert.sameValue(Date.parse("Mon Jan 2 15:04:05 MST 2006"), 1136239445000,
"Date.parse(\"Mon Jan 2 15:04:05 MST 2006\")");
assert.sameValue(Date.parse("Mon Jan 02 15:04:05 MST 2006"), 1136239445000,
"Date.parse(\"Mon Jan 02 15:04:05 MST 2006\")");
assert.sameValue(Date.parse("Mon Jan 02 15:04:05 -0700 2006"), 1136239445000,
"Date.parse(\"Mon Jan 02 15:04:05 -0700 2006\")");
var d = new Date("Mon, 02 Jan 2006 15:04:05 MST");
assert.sameValue(d.getUTCHours(), 22,
"new Date(\"Mon, 02 Jan 2006 15:04:05 MST\").getUTCHours()");
assert.sameValue(d.getHours(), 17,
"new Date(\"Mon, 02 Jan 2006 15:04:05 MST\").getHours()");
assert.sameValue(Date.parse("Mon, 02 Jan 2006 15:04:05 zzz"), NaN,
"Date.parse(\"Mon, 02 Jan 2006 15:04:05 zzz\")");
assert.sameValue(Date.parse("Mon, 02 Jan 2006 15:04:05 ZZZ"), NaN,
"Date.parse(\"Mon, 02 Jan 2006 15:04:05 ZZZ\")");
var minDateStr = "-271821-04-20T00:00:00.000Z";
var minDate = new Date(-8640000000000000);
assert.sameValue(minDate.toISOString(), minDateStr, "minDateStr");
assert.sameValue(Date.parse(minDateStr), minDate.valueOf(), "parse minDateStr");
var maxDateStr = "+275760-09-13T00:00:00.000Z";
var maxDate = new Date(8640000000000000);
assert.sameValue(maxDate.toISOString(), maxDateStr, "maxDateStr");
assert.sameValue(Date.parse(maxDateStr), maxDate.valueOf(), "parse maxDateStr");
var belowRange = "-271821-04-19T23:59:59.999Z";
var aboveRange = "+275760-09-13T00:00:00.001Z";
assert.sameValue(Date.parse(belowRange), NaN, "parse below minimum time value");
assert.sameValue(Date.parse(aboveRange), NaN, "parse above maximum time value");
`
l := time.Local
defer func() {
time.Local = l
}()
var err error
time.Local, err = time.LoadLocation("America/New_York")
if err != nil {
t.Fatal(err)
}
testScript1(TESTLIB+SCRIPT, _undefined, t)
}