2 * Copyright 2018 Jacek Caban for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 var JS_E_PROP_DESC_MISMATCH = 0x800a01bd;
20 var JS_E_INVALID_WRITABLE_PROP_DESC = 0x800a13ac;
21 var JS_E_NONCONFIGURABLE_REDEFINED = 0x800a13d6;
22 var JS_E_NONWRITABLE_MODIFIED = 0x800a13d7;
24 function test_date_now() {
26 var time = (new Date()).getTime();
28 ok(time >= now && time-now < 50, "unexpected Date.now() result " + now + " expected " + time);
35 function test_toISOString() {
36 function expect(date, expected) {
37 var s = date.toISOString();
38 ok(s === expected, "toISOString returned " + s + " expected " + expected);
41 function expect_exception(func) {
47 ok(false, "expected exception");
50 expect(new Date(0), "1970-01-01T00:00:00.000Z");
51 expect(new Date(0xdeadbeef), "1970-02-13T05:45:28.559Z");
52 expect(new Date(10928309128301), "2316-04-22T01:25:28.301Z");
53 expect(new Date(-1), "1969-12-31T23:59:59.999Z");
54 expect(new Date(-62167219200000), "0000-01-01T00:00:00.000Z");
55 expect(new Date(-62167219200001), "-000001-12-31T23:59:59.999Z");
56 expect(new Date(-6216721920000100), "-195031-12-03T23:59:59.900Z");
57 expect(new Date(1092830912830100), "+036600-06-07T22:27:10.100Z");
59 expect_exception(function() { new Date(NaN).toISOString(); });
60 expect_exception(function() { new Date(31494784780800001).toISOString(); });
65 function test_indexOf() {
66 function expect(array, args, exr) {
67 var r = Array.prototype.indexOf.apply(array, args);
68 ok(r == exr, "indexOf returned " + r + " expected " + exr);
71 ok(Array.prototype.indexOf.length == 1, "indexOf.length = " + Array.prototype.indexOf.length);
73 expect([1,2,3], [2], 1);
74 expect([1,undefined,3], [undefined], 1);
75 expect([1,undefined,3], [], 1);
76 expect([1,,3], [undefined], -1);
77 expect([1,2,3,4,5,6], [2, 2], -1);
78 expect([1,2,3,4,5,6], [5, -1], -1);
79 expect([1,2,3,4,5,6], [5, -2], 4);
80 expect([1,2,3,4,5,6], [5, -20], 4);
81 expect([1,2,3,4,5,6], [5, 20], -1);
82 expect("abc", ["b"], 1);
83 expect(true, [true], -1);
84 expect({"4": 4, length: 5}, [4], 4);
85 expect({"4": 4, length: 5}, [undefined], -1);
86 expect({"4": 4, length: 3}, [4], -1);
87 expect({"test": true}, [true], -1);
88 expect([1,2,3], [2, 1.9], 1);
93 function test_isArray() {
94 function expect_array(a, exr) {
95 var r = Array.isArray(a);
96 ok(r === exr, "isArray returned " + r + " expected " + exr);
99 expect_array([1], true);
100 expect_array(Array, false);
101 expect_array(new Array(), true);
102 expect_array({"1": 1, "2": 2, length: 2}, false);
105 C.prototype = Array.prototype;
106 expect_array(new C(), false);
111 function test_identifier_keywords() {
142 function ro() { return o; };
144 ok(o.if === 1, "o.if = " + o.if);
145 ok(ro().default === 2, "ro().default = " + ro().default);
146 ok(o.false === true, "o.false = " + o.false);
151 function test_own_data_prop_desc(obj, prop, expected_writable, expected_enumerable,
152 expected_configurable) {
153 var desc = Object.getOwnPropertyDescriptor(obj, prop);
154 ok("value" in desc, "value is not in desc");
155 ok(desc.value === obj[prop], "desc.value = " + desc.value + " expected " + obj[prop]);
156 ok(desc.writable === expected_writable, "desc(" + prop + ").writable = " + desc.writable
157 + " expected " + expected_writable);
158 ok(desc.enumerable === expected_enumerable, "desc.enumerable = " + desc.enumerable
159 + " expected " + expected_enumerable);
160 ok(desc.configurable === expected_configurable, "desc.configurable = " + desc.configurable
161 + " expected " + expected_configurable);
164 function test_getOwnPropertyDescriptor() {
168 test_own_data_prop_desc(obj, "test", true, true, true);
170 test_own_data_prop_desc(Object, "getOwnPropertyDescriptor", true, false, true);
171 test_own_data_prop_desc(Math, "PI", false, false, false);
173 var obj = new String();
174 test_own_data_prop_desc(obj, "length", false, false, false);
175 ok(Object.getOwnPropertyDescriptor(obj, "slice") === undefined,
176 "getOwnPropertyDescriptor(slice) did not return undefined");
177 test_own_data_prop_desc(String.prototype, "slice", true, false, true);
180 test_own_data_prop_desc(obj, "length", true, false, false);
182 obj = new Function();
183 test_own_data_prop_desc(obj, "length", false, false, false);
184 test_own_data_prop_desc(obj, "arguments", false, false, false);
187 test_own_data_prop_desc(obj, "global", false, false, false);
188 test_own_data_prop_desc(obj, "ignoreCase", false, false, false);
189 test_own_data_prop_desc(obj, "lastIndex", true, false, false);
190 test_own_data_prop_desc(obj, "multiline", false, false, false);
191 test_own_data_prop_desc(obj, "source", false, false, false);
194 test_own_data_prop_desc(arguments, "length", true, false, true);
195 test_own_data_prop_desc(arguments, "callee", true, false, true);
198 test_own_data_prop_desc(String, "prototype", false, false, false);
199 test_own_data_prop_desc(function(){}, "prototype", true, false, false);
200 test_own_data_prop_desc(Function, "prototype", false, false, false);
201 test_own_data_prop_desc(String.prototype, "constructor", true, false, true);
206 function test_defineProperty() {
207 function expect_exception(func, expected_number) {
211 var n = e.number + 0x100000000; /* make it unsigned like HRESULT */
212 todo_wine_if(expected_number == JS_E_PROP_DESC_MISMATCH).
213 ok(n === expected_number, "got exception " + n.toString(16)
214 + " expected " + expected_number.toString(16));
215 ok(e.name === "TypeError", "e.name = " + e.name);
218 ok(false, "expected exception");
221 var obj = new Object();
222 Object.defineProperty(obj, "test", {});
223 ok("test" in obj, "test is not in obj");
224 test_own_data_prop_desc(obj, "test", false, false, false);
225 ok(obj.test === undefined, "obj.test = " + obj.test);
227 Object.defineProperty(obj, "all", {writable: true, enumerable: true, configurable: true, value: 1});
228 test_own_data_prop_desc(obj, "all", true, true, true);
229 ok(obj.all === 1, "obj.test = " + obj.test);
231 Object.defineProperty(obj, "all", {writable: false});
232 test_own_data_prop_desc(obj, "all", false, true, true);
233 ok(obj.all === 1, "obj.test = " + obj.test);
235 var getsetprop_value = 1;
238 return getsetprop_value;
241 getsetprop_value = v;
244 Object.defineProperty(obj, "getsetprop", desc);
246 Object.defineProperty(obj, "notConf", {writable: true, enumerable: true, configurable: false, value: 1});
247 test_own_data_prop_desc(obj, "notConf", true, true, false);
249 expect_exception(function() {
250 Object.defineProperty(obj, "notConf",
251 {writable: true, enumerable: true, configurable: true, value: 1});
252 }, JS_E_NONCONFIGURABLE_REDEFINED);
254 expect_exception(function() {
255 Object.defineProperty(obj, "notConf",
256 {writable: true, enumerable: false, configurable: false, value: 1});
257 }, JS_E_NONCONFIGURABLE_REDEFINED);
259 Object.defineProperty(obj, "notConf",
260 {writable: true, enumerable: true, configurable: false, value: 2});
261 test_own_data_prop_desc(obj, "notConf", true, true, false);
263 Object.defineProperty(obj, "notConf", {writable: true, value: 2});
264 test_own_data_prop_desc(obj, "notConf", true, true, false);
266 Object.defineProperty(obj, "notConf2",
267 {writable: false, enumerable: false, configurable: false, value: 1});
268 test_own_data_prop_desc(obj, "notConf2", false, false, false);
270 ok(obj.notConf2 === 1, "obj.notConf2 = " + obj.notConf2)
272 expect_exception(function() {
273 Object.defineProperty(obj, "notConf2",
274 {writable: false, enumerable: false, configurable: true, value: 1});
275 }, JS_E_NONCONFIGURABLE_REDEFINED);
277 expect_exception(function() {
278 Object.defineProperty(obj, "notConf2",
279 {writable: false, enumerable: true, configurable: false, value: 1});
280 }, JS_E_NONCONFIGURABLE_REDEFINED);
282 expect_exception(function() {
283 Object.defineProperty(obj, "notConf2", {writable: true, value: 1});
284 }, JS_E_NONWRITABLE_MODIFIED);
286 expect_exception(function() {
287 Object.defineProperty(obj, "notConf2", {value: 2});
288 }, JS_E_NONWRITABLE_MODIFIED);
290 Object.defineProperty(obj, "notConf2",
291 {writable: false, enumerable: false, configurable: false, value: 1});
292 test_own_data_prop_desc(obj, "notConf2", false, false, false);
294 Object.defineProperty(obj, "notConf2", {writable: false, value: 1});
295 test_own_data_prop_desc(obj, "notConf2", false, false, false);
299 return getsetprop_value;
302 getsetprop_value = v;
306 Object.defineProperty(obj, "notConfAcc", desc);
308 expect_exception(function() {
309 Object.defineProperty(obj, "notConfAcc", {value: 1});
310 }, JS_E_NONCONFIGURABLE_REDEFINED);
312 expect_exception(function() {
313 Object.defineProperty(obj, "notConfAcc", {get: desc.get, set: function () {}});
314 }, JS_E_NONCONFIGURABLE_REDEFINED);
316 expect_exception(function() {
317 Object.defineProperty(obj, "notConfAcc", {get: undefined, set: desc.set});
318 }, JS_E_NONCONFIGURABLE_REDEFINED);
320 expect_exception(function() {
321 Object.defineProperty(obj, "notConfAcc", {writable: true});
322 }, JS_E_NONCONFIGURABLE_REDEFINED);
324 Object.defineProperty(obj, "notConfAcc", {get: desc.get});
326 Object.defineProperty(obj, "notConfAcc", {set: desc.set});
328 Object.defineProperty(obj, "notConfAcc", {configurable: false});
332 return getsetprop_value;
335 getsetprop_value = v;
339 Object.defineProperty(obj, "confAcc", desc);
341 Object.defineProperty(obj, "confAcc", {writable: 1});
342 test_own_data_prop_desc(obj, "confAcc", true, false, true);
344 Object.defineProperty(obj, "confAcc", desc);
346 desc.get = function() {};
347 desc.set = undefined;
348 Object.defineProperty(obj, "confAcc", desc);
350 expect_exception(function() {
351 Object.defineProperty(obj, "invaliddesc", {get: undefined, value: 1});
352 }, JS_E_PROP_DESC_MISMATCH);
354 expect_exception(function() {
355 Object.defineProperty(obj, "invaliddesc", {set: undefined, writable: true});
356 }, JS_E_INVALID_WRITABLE_PROP_DESC);
361 return getsetprop_value;
364 getsetprop_value = v;
368 Object.defineProperty(child.prototype, "parent_accessor", desc);
372 ok(Object.getOwnPropertyDescriptor(obj, "parent_accessor") === undefined,
373 "getOwnPropertyDescriptor(parent_accessor) did not return undefined");
378 function test_global_properties() {
381 /* Make sure that global properties are not writable. */
384 ok(isNaN(NaN), "NaN = " + NaN);
387 ok(undefined === o, "NaN = " + NaN);
390 ok(Infinity === o, "Infinity = " + NaN);
400 test_identifier_keywords,
401 test_getOwnPropertyDescriptor,
403 test_global_properties