1 description("Test to ensure correct behaviour of Object.getOwnPropertyDescriptor");
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')");
11 shouldBe(test
+'.get', '' + expected
.get);
12 shouldBe(test
+'.set', '' + expected
.set);
13 shouldBeFalse(test
+".hasOwnProperty('value')");
14 shouldBeFalse(test
+".hasOwnProperty('writable')");
16 shouldBe(test
+'.enumerable', '' + expected
.enumerable
);
17 shouldBe(test
+'.configurable', '' + expected
.configurable
);
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("window", "'location'", {writable
: false, enumerable
: true, configurable
: false, value
: "window.location"});
28 descriptorShouldBe("Number", "'NEGATIVE_INFINITY'", {writable
: false, enumerable
: false, configurable
: false, value
:"Number.NEGATIVE_INFINITY"});
29 descriptorShouldBe("RegExp", "'$_'", {writable
: true, enumerable
: false, configurable
: true, value
:"RegExp.$_"});
30 descriptorShouldBe("/a/g", "'global'", {writable
: true, enumerable
: false, configurable
: false, value
:true});
31 descriptorShouldBe("Node", "'DOCUMENT_POSITION_DISCONNECTED'", {writable
: false, enumerable
: true, configurable
: false, value
:"Node.DOCUMENT_POSITION_DISCONNECTED"});
32 descriptorShouldBe("Math", "'sin'", {writable
: true, enumerable
: false, configurable
: true, value
:"Math.sin"});
33 descriptorShouldBe("[1,2,3]", "0", {writable
: true, enumerable
: true, configurable
: true, value
:"1"});
34 descriptorShouldBe("[1,2,3]", "'length'", {writable
: true, enumerable
: false, configurable
: false, value
:"3"});
35 descriptorShouldBe("[1,2,3]", "'length'", {writable
: true, enumerable
: false, configurable
: false, value
:"3"});
36 descriptorShouldBe("/(a)*/g.exec('a')", "0", {writable
: true, enumerable
: true, configurable
: true, value
:"'a'"});
37 descriptorShouldBe("/(a)*/g.exec('a')", "'length'", {writable
: true, enumerable
: false, configurable
: false, value
:2});
38 descriptorShouldBe("function(){}", "'length'", {writable
: false, enumerable
: false, configurable
: true, value
:0});
39 descriptorShouldBe("Math.sin", "'length'", {writable
: false, enumerable
: false, configurable
: true, value
:1});
40 descriptorShouldBe("Math.sin", "'name'", {writable
: false, enumerable
: false, configurable
: true, value
:"'sin'"});
42 descriptorShouldBe("global", "'global'", {writable
: true, enumerable
: true, configurable
: false, value
:"global"});
43 descriptorShouldBe("global", "'undefined'", {writable
: false, enumerable
: false, configurable
: false, value
:"undefined"});
44 descriptorShouldBe("global", "'NaN'", {writable
: false, enumerable
: false, configurable
: false, value
:"NaN"});
45 descriptorShouldBe("global", "'Infinity'", {writable
: false, enumerable
: false, configurable
: false, value
:"Infinity"});
46 descriptorShouldBe("global", "'window'", {writable
: false, enumerable
: true, configurable
: false, value
:"global"});
47 descriptorShouldBe("global", "'XMLHttpRequest'", {writable
: true, enumerable
: false, configurable
: true, value
:"XMLHttpRequest"});
48 descriptorShouldBe("global", "'length'", {writable
: true, enumerable
: true, configurable
: false, value
:"global.length"});
49 descriptorShouldBe("global", "0", {writable
: true, enumerable
: false, configurable
: false, value
:"global[0]"});
51 var objectWithGetter
= {};
52 function getterFunc(){};
53 objectWithGetter
.__defineGetter__("getter", getterFunc
);
54 descriptorShouldBe("objectWithGetter", "'getter'", {"get": "getterFunc", "set": undefined, enumerable
: true, configurable
: true});
55 var objectWithSetter
= {};
56 function setterFunc(){};
57 objectWithSetter
.__defineSetter__("setter", setterFunc
);
58 descriptorShouldBe("objectWithSetter", "'setter'", {"set": "setterFunc", "get": undefined, enumerable
: true, configurable
: true});
59 var objectWithAccessor
= {};
60 objectWithAccessor
.__defineSetter__("accessor", setterFunc
);
61 objectWithAccessor
.__defineGetter__("accessor", getterFunc
);
62 descriptorShouldBe("objectWithAccessor", "'accessor'", {"set": "setterFunc", "get": "getterFunc", enumerable
: true, configurable
: true});
64 shouldThrow("Object.getOwnPropertyDescriptor(null)");
65 shouldThrow("Object.getOwnPropertyDescriptor(undefined)");
66 shouldBeUndefined("Object.getOwnPropertyDescriptor(1)");
67 shouldBeUndefined("Object.getOwnPropertyDescriptor('')");
68 shouldBeUndefined("Object.getOwnPropertyDescriptor(true)");
69 shouldBeUndefined("Object.getOwnPropertyDescriptor(false)");
71 debug("Checking property ordering");
72 var normalOrder
= ["'value'", "'writable'", "'enumerable'", "'configurable'"];
73 var accessorOrder
= ["'get'", "'set'", "'enumerable'", "'configurable'"];
75 for (var property
in Object
.getOwnPropertyDescriptor(Math
, "sin"))
76 shouldBe('property', normalOrder
[i
++]);
78 for (var property
in Object
.getOwnPropertyDescriptor(objectWithGetter
, "getter"))
79 shouldBe('property', accessorOrder
[i
++]);
81 for (var property
in Object
.getOwnPropertyDescriptor(objectWithSetter
, "setter"))
82 shouldBe('property', accessorOrder
[i
++]);
84 for (var property
in Object
.getOwnPropertyDescriptor(objectWithAccessor
, "accessor"))
85 shouldBe('property', accessorOrder
[i
++]);