js/src/tests/ecma_5/Object/15.2.3.3-01.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 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/. */
     6 //-----------------------------------------------------------------------------
     7 var BUGNUMBER = 505587;
     8 var summary = 'ES5 Object.getOwnPropertyDescriptor(O)';
     9 var actual = '';
    10 var expect = '';
    12 printBugNumber(BUGNUMBER);
    13 printStatus (summary);
    15 /**************
    16  * BEGIN TEST *
    17  **************/
    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   }
    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 }
    39 function expectDescriptor(actual, expected)
    40 {
    41   if (actual === undefined && expected === undefined)
    42     return;
    44   assertEq(typeof actual, "object");
    45   assertEq(typeof expected, "object");
    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);
    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   }
    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 }
    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 }
    92 /******************************************************************************/
    94 var o, pd, expected;
    96 o = { get x() { return 12; } };
    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");
   107 expectDescriptor(pd, expected);
   109 /******************************************************************************/
   111 var o2;
   113 o = Object.create(Object.prototype, { x: {get: function () { return 12; } } });
   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");
   124 expectDescriptor(pd, expected);
   126 o2 = Object.create(o);
   127 assertEq(Object.getOwnPropertyDescriptor(o2, "x"), undefined);
   129 /******************************************************************************/
   131 o = {};
   132 o.b = 12;
   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);
   144 /******************************************************************************/
   146 o = { get y() { return 17; }, set y(z) { } };
   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");
   157 expectDescriptor(pd, expected);
   159 /******************************************************************************/
   161 o = {};
   163 pd = Object.getOwnPropertyDescriptor(o, "absent");
   165 expectDescriptor(pd, undefined);
   167 /******************************************************************************/
   169 pd = Object.getOwnPropertyDescriptor([], "length");
   170 expected =
   171   {
   172     value: 0,
   173     writable: true,
   174     enumerable: false,
   175     configurable: false
   176   };
   178 expectDescriptor(pd, expected);
   180 pd = Object.getOwnPropertyDescriptor([1], "length");
   181 expected =
   182   {
   183     value: 1,
   184     writable: true,
   185     enumerable: false,
   186     configurable: false
   187   };
   189 expectDescriptor(pd, expected);
   191 pd = Object.getOwnPropertyDescriptor([1,], "length");
   192 expected =
   193   {
   194     value: 1,
   195     writable: true,
   196     enumerable: false,
   197     configurable: false
   198   };
   200 expectDescriptor(pd, expected);
   202 pd = Object.getOwnPropertyDescriptor([1,,], "length");
   203 expected =
   204   {
   205     value: 2,
   206     writable: true,
   207     enumerable: false,
   208     configurable: false
   209   };
   211 expectDescriptor(pd, expected);
   213 /******************************************************************************/
   215 pd = Object.getOwnPropertyDescriptor(new String("foobar"), "length");
   216 expected =
   217   {
   218     value: 6,
   219     writable: false,
   220     enumerable: false,
   221     configurable: false
   222   };
   224 expectDescriptor(pd, expected);
   226 /******************************************************************************/
   228 function foo() { }
   229 o = foo;
   231 pd = Object.getOwnPropertyDescriptor(o, "length");
   232 expected =
   233   {
   234     value: 0,
   235     writable: false,
   236     enumerable: false,
   237     configurable: false
   238   };
   240 expectDescriptor(pd, expected);
   242 pd = Object.getOwnPropertyDescriptor(o, "prototype");
   243 expected =
   244   {
   245     value: foo.prototype,
   246     writable: true,
   247     enumerable: false,
   248     configurable: false
   249   };
   251 expectDescriptor(pd, expected);
   253 /******************************************************************************/
   255 pd = Object.getOwnPropertyDescriptor(Function, "length");
   256 expected =
   257   {
   258     value: 1,
   259     writable: false,
   260     enumerable: false,
   261     configurable: false
   262   };
   264 expectDescriptor(pd, expected);
   266 /******************************************************************************/
   268 o = /foo/im;
   270 pd = Object.getOwnPropertyDescriptor(o, "source");
   271 expected =
   272   {
   273     value: "foo",
   274     writable: false,
   275     enumerable: false,
   276     configurable: false
   277   };
   279 expectDescriptor(pd, expected);
   281 pd = Object.getOwnPropertyDescriptor(o, "global");
   282 expected =
   283   {
   284     value: false,
   285     writable: false,
   286     enumerable: false,
   287     configurable: false
   288   };
   290 expectDescriptor(pd, expected);
   292 pd = Object.getOwnPropertyDescriptor(o, "ignoreCase");
   293 expected =
   294   {
   295     value: true,
   296     writable: false,
   297     enumerable: false,
   298     configurable: false
   299   };
   301 expectDescriptor(pd, expected);
   303 pd = Object.getOwnPropertyDescriptor(o, "multiline");
   304 expected =
   305   {
   306     value: true,
   307     writable: false,
   308     enumerable: false,
   309     configurable: false
   310   };
   312 expectDescriptor(pd, expected);
   314 pd = Object.getOwnPropertyDescriptor(o, "lastIndex");
   315 expected =
   316   {
   317     value: 0,
   318     writable: true,
   319     enumerable: false,
   320     configurable: false
   321   };
   323 expectDescriptor(pd, expected);
   325 /******************************************************************************/
   327 reportCompare(expect, actual, "Object.getOwnPropertyDescriptor");
   329 printStatus("All tests passed");

mercurial