15.2.3.3-01.js (6511B)
1 /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ 2 /* This Source Code Form is subject to the terms of the Mozilla Public 3 * License, v. 2.0. If a copy of the MPL was not distributed with this 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 5 6 //----------------------------------------------------------------------------- 7 var BUGNUMBER = 505587; 8 var summary = 'ES5 Object.getOwnPropertyDescriptor(O)'; 9 var actual = ''; 10 var expect = ''; 11 12 printBugNumber(BUGNUMBER); 13 printStatus (summary); 14 15 /************** 16 * BEGIN TEST * 17 **************/ 18 19 function assertEq(a, e, msg) 20 { 21 function SameValue(v1, v2) 22 { 23 if (v1 === 0 && v2 === 0) 24 return 1 / v1 === 1 / v2; 25 if (v1 !== v1 && v2 !== v2) 26 return true; 27 return v1 === v2; 28 } 29 30 if (!SameValue(a, e)) 31 { 32 var stack = new Error().stack || ""; 33 throw "Assertion failed: got " + a + ", expected " + e + 34 (msg ? ": " + msg : "") + 35 (stack ? "\nStack:\n" + stack : ""); 36 } 37 } 38 39 function expectDescriptor(actual, expected) 40 { 41 if (actual === undefined && expected === undefined) 42 return; 43 44 assertEq(typeof actual, "object"); 45 assertEq(typeof expected, "object"); 46 47 var fields = 48 { 49 value: true, 50 get: true, 51 set: true, 52 enumerable: true, 53 writable: true, 54 configurable: true 55 }; 56 for (var p in fields) 57 assertEq(actual.hasOwnProperty(p), expected.hasOwnProperty(p), p); 58 for (var p in actual) 59 assertEq(p in fields, true, p); 60 for (var p in expected) 61 assertEq(p in fields, true, p); 62 63 assertEq(actual.hasOwnProperty("value"), actual.hasOwnProperty("writable")); 64 assertEq(actual.hasOwnProperty("get"), actual.hasOwnProperty("set")); 65 if (actual.hasOwnProperty("value")) 66 { 67 assertEq(actual.value, expected.value); 68 assertEq(actual.writable, expected.writable); 69 } 70 else 71 { 72 assertEq(actual.get, expected.get); 73 assertEq(actual.set, expected.set); 74 } 75 76 assertEq(actual.hasOwnProperty("enumerable"), true); 77 assertEq(actual.hasOwnProperty("configurable"), true); 78 assertEq(actual.enumerable, expected.enumerable); 79 assertEq(actual.configurable, expected.configurable); 80 } 81 82 function adjustDescriptorField(o, actual, expect, field) 83 { 84 assertEq(field === "get" || field === "set", true); 85 var lookup = "__lookup" + (field === "get" ? "G" : "S") + "etter"; 86 if (typeof o[lookup] === "function") 87 expect[field] = o[lookup](field); 88 else 89 actual[field] = expect[field] = undefined; /* censor if we can't lookup */ 90 } 91 92 /******************************************************************************/ 93 94 var o, pd, expected; 95 96 o = { get x() { return 12; } }; 97 98 pd = Object.getOwnPropertyDescriptor(o, "x"); 99 expected = 100 { 101 set: undefined, 102 enumerable: true, 103 configurable: true 104 }; 105 adjustDescriptorField(o, pd, expected, "get"); 106 107 expectDescriptor(pd, expected); 108 109 /******************************************************************************/ 110 111 var o2; 112 113 o = Object.create(Object.prototype, { x: {get: function () { return 12; } } }); 114 115 pd = Object.getOwnPropertyDescriptor(o, "x"); 116 expected = 117 { 118 set: undefined, 119 enumerable: false, 120 configurable: false 121 }; 122 adjustDescriptorField(o, pd, expected, "get"); 123 124 expectDescriptor(pd, expected); 125 126 o2 = Object.create(o); 127 assertEq(Object.getOwnPropertyDescriptor(o2, "x"), undefined); 128 129 /******************************************************************************/ 130 131 o = {}; 132 o.b = 12; 133 134 pd = Object.getOwnPropertyDescriptor(o, "b"); 135 expected = 136 { 137 value: 12, 138 writable: true, 139 enumerable: true, 140 configurable: true 141 }; 142 expectDescriptor(pd, expected); 143 144 /******************************************************************************/ 145 146 o = { get y() { return 17; }, set y(z) { } }; 147 148 pd = Object.getOwnPropertyDescriptor(o, "y"); 149 expected = 150 { 151 enumerable: true, 152 configurable: true 153 }; 154 adjustDescriptorField(o, pd, expected, "get"); 155 adjustDescriptorField(o, pd, expected, "set"); 156 157 expectDescriptor(pd, expected); 158 159 /******************************************************************************/ 160 161 o = {}; 162 163 pd = Object.getOwnPropertyDescriptor(o, "absent"); 164 165 expectDescriptor(pd, undefined); 166 167 /******************************************************************************/ 168 169 pd = Object.getOwnPropertyDescriptor([], "length"); 170 expected = 171 { 172 value: 0, 173 writable: true, 174 enumerable: false, 175 configurable: false 176 }; 177 178 expectDescriptor(pd, expected); 179 180 pd = Object.getOwnPropertyDescriptor([1], "length"); 181 expected = 182 { 183 value: 1, 184 writable: true, 185 enumerable: false, 186 configurable: false 187 }; 188 189 expectDescriptor(pd, expected); 190 191 pd = Object.getOwnPropertyDescriptor([1,], "length"); 192 expected = 193 { 194 value: 1, 195 writable: true, 196 enumerable: false, 197 configurable: false 198 }; 199 200 expectDescriptor(pd, expected); 201 202 pd = Object.getOwnPropertyDescriptor([1,,], "length"); 203 expected = 204 { 205 value: 2, 206 writable: true, 207 enumerable: false, 208 configurable: false 209 }; 210 211 expectDescriptor(pd, expected); 212 213 /******************************************************************************/ 214 215 pd = Object.getOwnPropertyDescriptor(new String("foobar"), "length"); 216 expected = 217 { 218 value: 6, 219 writable: false, 220 enumerable: false, 221 configurable: false 222 }; 223 224 expectDescriptor(pd, expected); 225 226 /******************************************************************************/ 227 228 function foo() { } 229 o = foo; 230 231 pd = Object.getOwnPropertyDescriptor(o, "length"); 232 expected = 233 { 234 value: 0, 235 writable: false, 236 enumerable: false, 237 configurable: true 238 }; 239 240 expectDescriptor(pd, expected); 241 242 pd = Object.getOwnPropertyDescriptor(o, "prototype"); 243 expected = 244 { 245 value: foo.prototype, 246 writable: true, 247 enumerable: false, 248 configurable: false 249 }; 250 251 expectDescriptor(pd, expected); 252 253 /******************************************************************************/ 254 255 pd = Object.getOwnPropertyDescriptor(Function, "length"); 256 expected = 257 { 258 value: 1, 259 writable: false, 260 enumerable: false, 261 configurable: true 262 }; 263 264 expectDescriptor(pd, expected); 265 266 /******************************************************************************/ 267 268 o = /foo/im; 269 270 pd = Object.getOwnPropertyDescriptor(o, "lastIndex"); 271 expected = 272 { 273 value: 0, 274 writable: true, 275 enumerable: false, 276 configurable: false 277 }; 278 279 expectDescriptor(pd, expected); 280 281 /******************************************************************************/ 282 283 reportCompare(expect, actual, "Object.getOwnPropertyDescriptor"); 284 285 printStatus("All tests passed");