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 Object.defineProperty(obj, "notConf", {writable: true, enumerable: true, configurable: false, value: 1});
236 test_own_data_prop_desc(obj, "notConf", true, true, false);
238 expect_exception(function() {
239 Object.defineProperty(obj, "notConf",
240 {writable: true, enumerable: true, configurable: true, value: 1});
241 }, JS_E_NONCONFIGURABLE_REDEFINED);
243 expect_exception(function() {
244 Object.defineProperty(obj, "notConf",
245 {writable: true, enumerable: false, configurable: false, value: 1});
246 }, JS_E_NONCONFIGURABLE_REDEFINED);
248 Object.defineProperty(obj, "notConf",
249 {writable: true, enumerable: true, configurable: false, value: 2});
250 test_own_data_prop_desc(obj, "notConf", true, true, false);
252 Object.defineProperty(obj, "notConf", {writable: true, value: 2});
253 test_own_data_prop_desc(obj, "notConf", true, true, false);
255 Object.defineProperty(obj, "notConf2",
256 {writable: false, enumerable: false, configurable: false, value: 1});
257 test_own_data_prop_desc(obj, "notConf2", false, false, false);
259 ok(obj.notConf2 === 1, "obj.notConf2 = " + obj.notConf2)
261 expect_exception(function() {
262 Object.defineProperty(obj, "notConf2",
263 {writable: false, enumerable: false, configurable: true, value: 1});
264 }, JS_E_NONCONFIGURABLE_REDEFINED);
266 expect_exception(function() {
267 Object.defineProperty(obj, "notConf2",
268 {writable: false, enumerable: true, configurable: false, value: 1});
269 }, JS_E_NONCONFIGURABLE_REDEFINED);
271 expect_exception(function() {
272 Object.defineProperty(obj, "notConf2", {writable: true, value: 1});
273 }, JS_E_NONWRITABLE_MODIFIED);
275 expect_exception(function() {
276 Object.defineProperty(obj, "notConf2", {value: 2});
277 }, JS_E_NONWRITABLE_MODIFIED);
279 Object.defineProperty(obj, "notConf2",
280 {writable: false, enumerable: false, configurable: false, value: 1});
281 test_own_data_prop_desc(obj, "notConf2", false, false, false);
283 Object.defineProperty(obj, "notConf2", {writable: false, value: 1});
284 test_own_data_prop_desc(obj, "notConf2", false, false, false);
286 expect_exception(function() {
287 Object.defineProperty(obj, "invaliddesc", {get: undefined, value: 1});
288 }, JS_E_PROP_DESC_MISMATCH);
290 expect_exception(function() {
291 Object.defineProperty(obj, "invaliddesc", {set: undefined, writable: true});
292 }, JS_E_INVALID_WRITABLE_PROP_DESC);
297 function test_global_properties() {
300 /* Make sure that global properties are not writable. */
303 ok(isNaN(NaN), "NaN = " + NaN);
306 ok(undefined === o, "NaN = " + NaN);
309 ok(Infinity === o, "Infinity = " + NaN);
319 test_identifier_keywords,
320 test_getOwnPropertyDescriptor,
322 test_global_properties