js/src/tests/js1_5/extensions/getset-005.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  * Date: 14 April 2001
     8  *
     9  * SUMMARY: Testing  obj.__defineSetter__(), obj.__defineGetter__()
    10  * Note: this is a non-ECMA language extension
    11  *
    12  * This test is the same as getset-004.js, except that here we
    13  * store the getter/setter functions in global variables.
    14  */
    15 //-----------------------------------------------------------------------------
    16 var UBound = 0;
    17 var BUGNUMBER = '(none)';
    18 var summary = 'Testing  obj.__defineSetter__(), obj.__defineGetter__()';
    19 var statprefix = 'Status: ';
    20 var status = '';
    21 var statusitems = [ ];
    22 var actual = '';
    23 var actualvalues = [ ];
    24 var expect= '';
    25 var expectedvalues = [ ];
    26 var cnName = 'name';
    27 var cnDEFAULT = 'default name';
    28 var cnFRED = 'Fred';
    29 var obj = {};
    30 var obj2 = {};
    31 var s = '';
    34 // The getter/setter functions we'll use in all three sections below -
    35 var cnNameSetter = function(newValue) {this._name=newValue; this.nameSETS++;};
    36 var cnNameGetter = function() {this.nameGETS++; return this._name;};
    39 // SECTION1: define getter/setter directly on an object (not its prototype)
    40 obj = new Object();
    41 obj.nameSETS = 0;
    42 obj.nameGETS = 0;
    43 obj.__defineSetter__(cnName, cnNameSetter);
    44 obj.__defineGetter__(cnName, cnNameGetter);
    46 status = 'In SECTION1 of test after 0 sets, 0 gets';
    47 actual = [obj.nameSETS,obj.nameGETS];
    48 expect = [0,0];
    49 addThis();
    51 s = obj.name;
    52 status = 'In SECTION1 of test after 0 sets, 1 get';
    53 actual = [obj.nameSETS,obj.nameGETS];
    54 expect = [0,1];
    55 addThis();
    57 obj.name = cnFRED;
    58 status = 'In SECTION1 of test after 1 set, 1 get';
    59 actual = [obj.nameSETS,obj.nameGETS];
    60 expect = [1,1];
    61 addThis();
    63 obj.name = obj.name;
    64 status = 'In SECTION1 of test after 2 sets, 2 gets';
    65 actual = [obj.nameSETS,obj.nameGETS];
    66 expect = [2,2];
    67 addThis();
    70 // SECTION2: define getter/setter in Object.prototype
    71 Object.prototype.nameSETS = 0;
    72 Object.prototype.nameGETS = 0;
    73 Object.prototype.__defineSetter__(cnName, cnNameSetter);
    74 Object.prototype.__defineGetter__(cnName, cnNameGetter);
    76 obj = new Object();
    77 status = 'In SECTION2 of test after 0 sets, 0 gets';
    78 actual = [obj.nameSETS,obj.nameGETS];
    79 expect = [0,0];
    80 addThis();
    82 s = obj.name;
    83 status = 'In SECTION2 of test after 0 sets, 1 get';
    84 actual = [obj.nameSETS,obj.nameGETS];
    85 expect = [0,1];
    86 addThis();
    88 obj.name = cnFRED;
    89 status = 'In SECTION2 of test after 1 set, 1 get';
    90 actual = [obj.nameSETS,obj.nameGETS];
    91 expect = [1,1];
    92 addThis();
    94 obj.name = obj.name;
    95 status = 'In SECTION2 of test after 2 sets, 2 gets';
    96 actual = [obj.nameSETS,obj.nameGETS];
    97 expect = [2,2];
    98 addThis();
   101 // SECTION 3: define getter/setter in prototype of user-defined constructor
   102 function TestObject()
   103 {
   104 }
   105 TestObject.prototype.nameSETS = 0;
   106 TestObject.prototype.nameGETS = 0;
   107 TestObject.prototype.__defineSetter__(cnName, cnNameSetter);
   108 TestObject.prototype.__defineGetter__(cnName, cnNameGetter);
   109 TestObject.prototype.name = cnDEFAULT;
   111 obj = new TestObject();
   112 status = 'In SECTION3 of test after 1 set, 0 gets'; // (we set a default value in the prototype)
   113 actual = [obj.nameSETS,obj.nameGETS];
   114 expect = [1,0];
   115 addThis();
   117 s = obj.name;
   118 status = 'In SECTION3 of test after 1 set, 1 get';
   119 actual = [obj.nameSETS,obj.nameGETS];
   120 expect = [1,1];
   121 addThis();
   123 obj.name = cnFRED;
   124 status = 'In SECTION3 of test after 2 sets, 1 get';
   125 actual = [obj.nameSETS,obj.nameGETS];
   126 expect = [2,1];
   127 addThis();
   129 obj.name = obj.name;
   130 status = 'In SECTION3 of test after 3 sets, 2 gets';
   131 actual = [obj.nameSETS,obj.nameGETS];
   132 expect = [3,2];
   133 addThis();
   135 obj2 = new TestObject();
   136 status = 'obj2 = new TestObject() after 1 set, 0 gets';
   137 actual = [obj2.nameSETS,obj2.nameGETS];
   138 expect = [1,0]; // we set a default value in the prototype -
   139 addThis();
   141 // Use both obj and obj2  -
   142 obj2.name = obj.name +  obj2.name;
   143 status = 'obj2 = new TestObject() after 2 sets, 1 get';
   144 actual = [obj2.nameSETS,obj2.nameGETS];
   145 expect = [2,1];
   146 addThis();
   148 status = 'In SECTION3 of test after 3 sets, 3 gets';
   149 actual = [obj.nameSETS,obj.nameGETS];
   150 expect = [3,3];  // we left off at [3,2] above -
   151 addThis();
   154 //---------------------------------------------------------------------------------
   155 test();
   156 //---------------------------------------------------------------------------------
   159 function addThis()
   160 {
   161   statusitems[UBound] = status;
   162   actualvalues[UBound] = actual.toString();
   163   expectedvalues[UBound] = expect.toString();
   164   UBound++;
   165 }
   168 function test()
   169 {
   170   enterFunc ('test');
   171   printBugNumber(BUGNUMBER);
   172   printStatus (summary);
   174   for (var i = 0; i < UBound; i++)
   175   {
   176     reportCompare(expectedvalues[i], actualvalues[i], getStatus(i));
   177   }
   179   exitFunc ('test');
   180 }
   183 function getStatus(i)
   184 {
   185   return statprefix + statusitems[i];
   186 }

mercurial