6e35f3ba1d422694bcc9e5c9df2f37220866a3c2
[WebKit-https.git] / LayoutTests / js / resources / getOwnPropertyDescriptor.js
1 description("Test to ensure correct behaviour of Object.getOwnPropertyDescriptor");
2
3 function descriptorShouldBe(object, property, expected) {
4     var test = 'Object.getOwnPropertyDescriptor('+object+", "+property+')';
5     if ("writable" in expected) {
6       //  shouldBe(test+'.writable', '' + expected.writable);
7         shouldBe(test+'.value', '' + expected.value);
8         shouldBeFalse(test+".hasOwnProperty('get')");
9         shouldBeFalse(test+".hasOwnProperty('set')");
10     } else {
11         shouldBe(test+'.get', '' + expected.get);
12         shouldBe(test+'.set', '' + expected.set);
13         shouldBeFalse(test+".hasOwnProperty('value')");
14         shouldBeFalse(test+".hasOwnProperty('writable')");
15     }
16     shouldBe(test+'.enumerable', '' + expected.enumerable);
17     shouldBe(test+'.configurable', '' + expected.configurable);
18 }
19
20 shouldBeUndefined("Object.getOwnPropertyDescriptor({}, 'undefinedProperty')");
21 descriptorShouldBe("{definedProperty:'defined'}", "'definedProperty'", {writable: true, enumerable: true, configurable: true, value:'"defined"'});
22 descriptorShouldBe("Array.prototype", "'concat'", {writable: true, enumerable: false, configurable: true, value:"Array.prototype.concat"});
23 descriptorShouldBe("Date.prototype", "'toISOString'", {writable: true, enumerable: false, configurable: true, value: "Date.prototype.toISOString"});
24 descriptorShouldBe("String.prototype", "'concat'", {writable: true, enumerable: false, configurable: true, value:"String.prototype.concat"});
25 descriptorShouldBe("RegExp.prototype", "'exec'", {writable: true, enumerable: false, configurable: true, value:"RegExp.prototype.exec"});
26 descriptorShouldBe("document.__proto__.__proto__", "'createElement'", {writable: true, enumerable: true, configurable: true, value:"document.createElement"});
27 descriptorShouldBe("Number", "'NEGATIVE_INFINITY'", {writable: false, enumerable: false, configurable: false, value:"Number.NEGATIVE_INFINITY"});
28 descriptorShouldBe("RegExp", "'$_'", {writable: true, enumerable: false, configurable: true, value:"RegExp.$_"});
29 descriptorShouldBe("Node", "'DOCUMENT_POSITION_DISCONNECTED'", {writable: false, enumerable: true, configurable: false, value:"Node.DOCUMENT_POSITION_DISCONNECTED"});
30 descriptorShouldBe("Math", "'sin'", {writable: true, enumerable: false, configurable: true, value:"Math.sin"});
31 descriptorShouldBe("[1,2,3]", "0", {writable: true, enumerable: true, configurable: true, value:"1"});
32 descriptorShouldBe("[1,2,3]", "'length'", {writable: true, enumerable: false, configurable: false, value:"3"});
33 descriptorShouldBe("[1,2,3]", "'length'", {writable: true, enumerable: false, configurable: false, value:"3"});
34 descriptorShouldBe("/(a)*/g.exec('a')", "0", {writable: true, enumerable: true, configurable: true, value:"'a'"});
35 descriptorShouldBe("/(a)*/g.exec('a')", "'length'", {writable: true, enumerable: false, configurable: false, value:2});
36 descriptorShouldBe("function(){}", "'length'", {writable: false, enumerable: false, configurable: true, value:0});
37 descriptorShouldBe("Math.sin", "'length'", {writable: false, enumerable: false, configurable: true, value:1});
38 descriptorShouldBe("Math.sin", "'name'", {writable: false, enumerable: false, configurable: true, value:"'sin'"});
39 var global = this;
40 descriptorShouldBe("global", "'global'", {writable: true, enumerable: true, configurable: false, value:"global"});
41 descriptorShouldBe("global", "'undefined'", {writable: false, enumerable: false, configurable: false, value:"undefined"});
42 descriptorShouldBe("global", "'NaN'", {writable: false, enumerable: false, configurable: false, value:"NaN"});
43 descriptorShouldBe("global", "'Infinity'", {writable: false, enumerable: false, configurable: false, value:"Infinity"});
44 var globalWindowGetter = Object.getOwnPropertyDescriptor(global, 'window').get;
45 descriptorShouldBe("global", "'window'", {get: 'globalWindowGetter', set: undefined, enumerable: true, configurable: false});
46 descriptorShouldBe("global", "'XMLHttpRequest'", {writable: true, enumerable: false, configurable: true, value:"XMLHttpRequest"});
47 descriptorShouldBe("global", "0", {writable: true, enumerable: false, configurable: true, value:"global[0]"});
48 descriptorShouldBe("document.getElementsByTagName('div')", "0", {writable: false, enumerable: true, configurable: true, value:"document.getElementsByTagName('div')[0]"});
49 descriptorShouldBe("document.getElementsByClassName('pass')", "0", {writable: false, enumerable: true, configurable: true, value:"document.getElementsByClassName('pass')[0]"});
50 var canvas = document.createElement("canvas");
51 var canvasPixelArray = canvas.getContext("2d").createImageData(10,10).data;
52 descriptorShouldBe("canvasPixelArray", "0", {writable: true, enumerable: true, configurable: false, value:"canvasPixelArray[0]"});
53 var select = document.createElement("select");
54 select.innerHTML = "<option>foo</option>";
55 descriptorShouldBe("select", "0", {writable: true, enumerable: true, configurable: true, value:"select[0]"});
56
57 var objectWithGetter = {};
58 function getterFunc(){};
59 objectWithGetter.__defineGetter__("getter", getterFunc);
60 descriptorShouldBe("objectWithGetter", "'getter'", {"get": "getterFunc", "set": undefined, enumerable: true, configurable: true});
61 var objectWithSetter = {};
62 function setterFunc(){};
63 objectWithSetter.__defineSetter__("setter", setterFunc);
64 descriptorShouldBe("objectWithSetter", "'setter'", {"set": "setterFunc", "get": undefined, enumerable: true, configurable: true});
65 var objectWithAccessor = {};
66 objectWithAccessor.__defineSetter__("accessor", setterFunc);
67 objectWithAccessor.__defineGetter__("accessor", getterFunc);
68 descriptorShouldBe("objectWithAccessor", "'accessor'", {"set": "setterFunc", "get": "getterFunc", enumerable: true, configurable: true});
69
70 shouldThrow("Object.getOwnPropertyDescriptor(null)");
71 shouldThrow("Object.getOwnPropertyDescriptor(undefined)");
72 shouldBe("Object.getOwnPropertyDescriptor(1)", "undefined");
73 shouldBe("Object.getOwnPropertyDescriptor('')", "undefined");
74 shouldBe("Object.getOwnPropertyDescriptor(true)", "undefined");
75 shouldBe("Object.getOwnPropertyDescriptor(false)", "undefined");
76
77 debug("Checking property ordering");
78 var normalOrder = ["'value'", "'writable'", "'enumerable'", "'configurable'"];
79 var accessorOrder = ["'get'", "'set'", "'enumerable'", "'configurable'"];
80 var i = 0;
81 for (var property in Object.getOwnPropertyDescriptor(Math, "sin"))
82     shouldBe('property', normalOrder[i++]);
83 i = 0;
84 for (var property in Object.getOwnPropertyDescriptor(objectWithGetter, "getter"))
85     shouldBe('property', accessorOrder[i++]);
86 i = 0;
87 for (var property in Object.getOwnPropertyDescriptor(objectWithSetter, "setter"))
88     shouldBe('property', accessorOrder[i++]);
89 i = 0;
90 for (var property in Object.getOwnPropertyDescriptor(objectWithAccessor, "accessor"))
91     shouldBe('property', accessorOrder[i++]);
92
93 var regexpPrototypeGlobalGetter = Object.getOwnPropertyDescriptor(RegExp.prototype, 'global').get;
94 descriptorShouldBe("RegExp.prototype", "'global'", {get: 'regexpPrototypeGlobalGetter', set: undefined, enumerable: false, configurable: true});