1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/tests/ecma_5/Object/15.2.3.3-01.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,329 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +//----------------------------------------------------------------------------- 1.10 +var BUGNUMBER = 505587; 1.11 +var summary = 'ES5 Object.getOwnPropertyDescriptor(O)'; 1.12 +var actual = ''; 1.13 +var expect = ''; 1.14 + 1.15 +printBugNumber(BUGNUMBER); 1.16 +printStatus (summary); 1.17 + 1.18 +/************** 1.19 + * BEGIN TEST * 1.20 + **************/ 1.21 + 1.22 +function assertEq(a, e, msg) 1.23 +{ 1.24 + function SameValue(v1, v2) 1.25 + { 1.26 + if (v1 === 0 && v2 === 0) 1.27 + return 1 / v1 === 1 / v2; 1.28 + if (v1 !== v1 && v2 !== v2) 1.29 + return true; 1.30 + return v1 === v2; 1.31 + } 1.32 + 1.33 + if (!SameValue(a, e)) 1.34 + { 1.35 + var stack = new Error().stack || ""; 1.36 + throw "Assertion failed: got " + a + ", expected " + e + 1.37 + (msg ? ": " + msg : "") + 1.38 + (stack ? "\nStack:\n" + stack : ""); 1.39 + } 1.40 +} 1.41 + 1.42 +function expectDescriptor(actual, expected) 1.43 +{ 1.44 + if (actual === undefined && expected === undefined) 1.45 + return; 1.46 + 1.47 + assertEq(typeof actual, "object"); 1.48 + assertEq(typeof expected, "object"); 1.49 + 1.50 + var fields = 1.51 + { 1.52 + value: true, 1.53 + get: true, 1.54 + set: true, 1.55 + enumerable: true, 1.56 + writable: true, 1.57 + configurable: true 1.58 + }; 1.59 + for (var p in fields) 1.60 + assertEq(actual.hasOwnProperty(p), expected.hasOwnProperty(p), p); 1.61 + for (var p in actual) 1.62 + assertEq(p in fields, true, p); 1.63 + for (var p in expected) 1.64 + assertEq(p in fields, true, p); 1.65 + 1.66 + assertEq(actual.hasOwnProperty("value"), actual.hasOwnProperty("writable")); 1.67 + assertEq(actual.hasOwnProperty("get"), actual.hasOwnProperty("set")); 1.68 + if (actual.hasOwnProperty("value")) 1.69 + { 1.70 + assertEq(actual.value, expected.value); 1.71 + assertEq(actual.writable, expected.writable); 1.72 + } 1.73 + else 1.74 + { 1.75 + assertEq(actual.get, expected.get); 1.76 + assertEq(actual.set, expected.set); 1.77 + } 1.78 + 1.79 + assertEq(actual.hasOwnProperty("enumerable"), true); 1.80 + assertEq(actual.hasOwnProperty("configurable"), true); 1.81 + assertEq(actual.enumerable, expected.enumerable); 1.82 + assertEq(actual.configurable, expected.configurable); 1.83 +} 1.84 + 1.85 +function adjustDescriptorField(o, actual, expect, field) 1.86 +{ 1.87 + assertEq(field === "get" || field === "set", true); 1.88 + var lookup = "__lookup" + (field === "get" ? "G" : "S") + "etter"; 1.89 + if (typeof o[lookup] === "function") 1.90 + expect[field] = o[lookup](field); 1.91 + else 1.92 + actual[field] = expect[field] = undefined; /* censor if we can't lookup */ 1.93 +} 1.94 + 1.95 +/******************************************************************************/ 1.96 + 1.97 +var o, pd, expected; 1.98 + 1.99 +o = { get x() { return 12; } }; 1.100 + 1.101 +pd = Object.getOwnPropertyDescriptor(o, "x"); 1.102 +expected = 1.103 + { 1.104 + set: undefined, 1.105 + enumerable: true, 1.106 + configurable: true 1.107 + }; 1.108 +adjustDescriptorField(o, pd, expected, "get"); 1.109 + 1.110 +expectDescriptor(pd, expected); 1.111 + 1.112 +/******************************************************************************/ 1.113 + 1.114 +var o2; 1.115 + 1.116 +o = Object.create(Object.prototype, { x: {get: function () { return 12; } } }); 1.117 + 1.118 +pd = Object.getOwnPropertyDescriptor(o, "x"); 1.119 +expected = 1.120 + { 1.121 + set: undefined, 1.122 + enumerable: false, 1.123 + configurable: false 1.124 + }; 1.125 +adjustDescriptorField(o, pd, expected, "get"); 1.126 + 1.127 +expectDescriptor(pd, expected); 1.128 + 1.129 +o2 = Object.create(o); 1.130 +assertEq(Object.getOwnPropertyDescriptor(o2, "x"), undefined); 1.131 + 1.132 +/******************************************************************************/ 1.133 + 1.134 +o = {}; 1.135 +o.b = 12; 1.136 + 1.137 +pd = Object.getOwnPropertyDescriptor(o, "b"); 1.138 +expected = 1.139 + { 1.140 + value: 12, 1.141 + writable: true, 1.142 + enumerable: true, 1.143 + configurable: true 1.144 + }; 1.145 +expectDescriptor(pd, expected); 1.146 + 1.147 +/******************************************************************************/ 1.148 + 1.149 +o = { get y() { return 17; }, set y(z) { } }; 1.150 + 1.151 +pd = Object.getOwnPropertyDescriptor(o, "y"); 1.152 +expected = 1.153 + { 1.154 + enumerable: true, 1.155 + configurable: true 1.156 + }; 1.157 +adjustDescriptorField(o, pd, expected, "get"); 1.158 +adjustDescriptorField(o, pd, expected, "set"); 1.159 + 1.160 +expectDescriptor(pd, expected); 1.161 + 1.162 +/******************************************************************************/ 1.163 + 1.164 +o = {}; 1.165 + 1.166 +pd = Object.getOwnPropertyDescriptor(o, "absent"); 1.167 + 1.168 +expectDescriptor(pd, undefined); 1.169 + 1.170 +/******************************************************************************/ 1.171 + 1.172 +pd = Object.getOwnPropertyDescriptor([], "length"); 1.173 +expected = 1.174 + { 1.175 + value: 0, 1.176 + writable: true, 1.177 + enumerable: false, 1.178 + configurable: false 1.179 + }; 1.180 + 1.181 +expectDescriptor(pd, expected); 1.182 + 1.183 +pd = Object.getOwnPropertyDescriptor([1], "length"); 1.184 +expected = 1.185 + { 1.186 + value: 1, 1.187 + writable: true, 1.188 + enumerable: false, 1.189 + configurable: false 1.190 + }; 1.191 + 1.192 +expectDescriptor(pd, expected); 1.193 + 1.194 +pd = Object.getOwnPropertyDescriptor([1,], "length"); 1.195 +expected = 1.196 + { 1.197 + value: 1, 1.198 + writable: true, 1.199 + enumerable: false, 1.200 + configurable: false 1.201 + }; 1.202 + 1.203 +expectDescriptor(pd, expected); 1.204 + 1.205 +pd = Object.getOwnPropertyDescriptor([1,,], "length"); 1.206 +expected = 1.207 + { 1.208 + value: 2, 1.209 + writable: true, 1.210 + enumerable: false, 1.211 + configurable: false 1.212 + }; 1.213 + 1.214 +expectDescriptor(pd, expected); 1.215 + 1.216 +/******************************************************************************/ 1.217 + 1.218 +pd = Object.getOwnPropertyDescriptor(new String("foobar"), "length"); 1.219 +expected = 1.220 + { 1.221 + value: 6, 1.222 + writable: false, 1.223 + enumerable: false, 1.224 + configurable: false 1.225 + }; 1.226 + 1.227 +expectDescriptor(pd, expected); 1.228 + 1.229 +/******************************************************************************/ 1.230 + 1.231 +function foo() { } 1.232 +o = foo; 1.233 + 1.234 +pd = Object.getOwnPropertyDescriptor(o, "length"); 1.235 +expected = 1.236 + { 1.237 + value: 0, 1.238 + writable: false, 1.239 + enumerable: false, 1.240 + configurable: false 1.241 + }; 1.242 + 1.243 +expectDescriptor(pd, expected); 1.244 + 1.245 +pd = Object.getOwnPropertyDescriptor(o, "prototype"); 1.246 +expected = 1.247 + { 1.248 + value: foo.prototype, 1.249 + writable: true, 1.250 + enumerable: false, 1.251 + configurable: false 1.252 + }; 1.253 + 1.254 +expectDescriptor(pd, expected); 1.255 + 1.256 +/******************************************************************************/ 1.257 + 1.258 +pd = Object.getOwnPropertyDescriptor(Function, "length"); 1.259 +expected = 1.260 + { 1.261 + value: 1, 1.262 + writable: false, 1.263 + enumerable: false, 1.264 + configurable: false 1.265 + }; 1.266 + 1.267 +expectDescriptor(pd, expected); 1.268 + 1.269 +/******************************************************************************/ 1.270 + 1.271 +o = /foo/im; 1.272 + 1.273 +pd = Object.getOwnPropertyDescriptor(o, "source"); 1.274 +expected = 1.275 + { 1.276 + value: "foo", 1.277 + writable: false, 1.278 + enumerable: false, 1.279 + configurable: false 1.280 + }; 1.281 + 1.282 +expectDescriptor(pd, expected); 1.283 + 1.284 +pd = Object.getOwnPropertyDescriptor(o, "global"); 1.285 +expected = 1.286 + { 1.287 + value: false, 1.288 + writable: false, 1.289 + enumerable: false, 1.290 + configurable: false 1.291 + }; 1.292 + 1.293 +expectDescriptor(pd, expected); 1.294 + 1.295 +pd = Object.getOwnPropertyDescriptor(o, "ignoreCase"); 1.296 +expected = 1.297 + { 1.298 + value: true, 1.299 + writable: false, 1.300 + enumerable: false, 1.301 + configurable: false 1.302 + }; 1.303 + 1.304 +expectDescriptor(pd, expected); 1.305 + 1.306 +pd = Object.getOwnPropertyDescriptor(o, "multiline"); 1.307 +expected = 1.308 + { 1.309 + value: true, 1.310 + writable: false, 1.311 + enumerable: false, 1.312 + configurable: false 1.313 + }; 1.314 + 1.315 +expectDescriptor(pd, expected); 1.316 + 1.317 +pd = Object.getOwnPropertyDescriptor(o, "lastIndex"); 1.318 +expected = 1.319 + { 1.320 + value: 0, 1.321 + writable: true, 1.322 + enumerable: false, 1.323 + configurable: false 1.324 + }; 1.325 + 1.326 +expectDescriptor(pd, expected); 1.327 + 1.328 +/******************************************************************************/ 1.329 + 1.330 +reportCompare(expect, actual, "Object.getOwnPropertyDescriptor"); 1.331 + 1.332 +printStatus("All tests passed");