forked from dop251/goja
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdate_test.go
219 lines (175 loc) · 5.39 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
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()");
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)");
`
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)
}