tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

getset-003.js (4722B)


      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 * Date: 14 April 2001
      8 *
      9 * SUMMARY: Testing obj.prop getter/setter
     10 * Note: this is a non-ECMA extension to the language.
     11 */
     12 //-----------------------------------------------------------------------------
     13 var UBound = 0;
     14 var BUGNUMBER = '(none)';
     15 var summary = 'Testing obj.prop getter/setter';
     16 var statprefix = 'Status: ';
     17 var status = '';
     18 var statusitems = [ ];
     19 var actual = '';
     20 var actualvalues = [ ];
     21 var expect= '';
     22 var expectedvalues = [ ];
     23 var cnDEFAULT = 'default name';
     24 var cnFRED = 'Fred';
     25 var obj = {};
     26 var obj2 = {};
     27 var s = '';
     28 
     29 
     30 // SECTION1: define getter/setter directly on an object (not its prototype)
     31 obj = new Object();
     32 obj.nameSETS = 0;
     33 obj.nameGETS = 0;
     34 Object.defineProperty(obj, "name",
     35 {
     36  enumerable: true, configurable: true,
     37  set: function(newValue) {this._name=newValue; this.nameSETS++;},
     38  get: function() {this.nameGETS++; return this._name;}
     39 });
     40 
     41    status = 'In SECTION1 of test after 0 sets, 0 gets';
     42 actual = [obj.nameSETS,obj.nameGETS];
     43 expect = [0,0];
     44 addThis();
     45 
     46 s = obj.name;
     47 status = 'In SECTION1 of test after 0 sets, 1 get';
     48 actual = [obj.nameSETS,obj.nameGETS];
     49 expect = [0,1];
     50 addThis();
     51 
     52 obj.name = cnFRED;
     53 status = 'In SECTION1 of test after 1 set, 1 get';
     54 actual = [obj.nameSETS,obj.nameGETS];
     55 expect = [1,1];
     56 addThis();
     57 
     58 obj.name = obj.name;
     59 status = 'In SECTION1 of test after 2 sets, 2 gets';
     60 actual = [obj.nameSETS,obj.nameGETS];
     61 expect = [2,2];
     62 addThis();
     63 
     64 
     65 // SECTION2: define getter/setter in Object.prototype
     66 Object.prototype.nameSETS = 0;
     67 Object.prototype.nameGETS = 0;
     68 Object.defineProperty(Object.prototype, "name",
     69 {
     70  enumerable: true, configurable: true,
     71  set: function(newValue) {this._name=newValue; this.nameSETS++;},
     72  get: function() {this.nameGETS++; return this._name;}
     73 });
     74 
     75    obj = new Object();
     76 status = 'In SECTION2 of test after 0 sets, 0 gets';
     77 actual = [obj.nameSETS,obj.nameGETS];
     78 expect = [0,0];
     79 addThis();
     80 
     81 s = obj.name;
     82 status = 'In SECTION2 of test after 0 sets, 1 get';
     83 actual = [obj.nameSETS,obj.nameGETS];
     84 expect = [0,1];
     85 addThis();
     86 
     87 obj.name = cnFRED;
     88 status = 'In SECTION2 of test after 1 set, 1 get';
     89 actual = [obj.nameSETS,obj.nameGETS];
     90 expect = [1,1];
     91 addThis();
     92 
     93 obj.name = obj.name;
     94 status = 'In SECTION2 of test after 2 sets, 2 gets';
     95 actual = [obj.nameSETS,obj.nameGETS];
     96 expect = [2,2];
     97 addThis();
     98 
     99 
    100 // SECTION 3: define getter/setter in prototype of user-defined constructor
    101 function TestObject()
    102 {
    103 }
    104 TestObject.prototype.nameSETS = 0;
    105 TestObject.prototype.nameGETS = 0;
    106 Object.defineProperty(TestObject.prototype, "name",
    107 {
    108  enumerable: true, configurable: true,
    109  set: function(newValue) {this._name=newValue; this.nameSETS++;},
    110  get: function() {this.nameGETS++; return this._name;}
    111 });
    112    TestObject.prototype.name = cnDEFAULT;
    113 
    114 obj = new TestObject();
    115 status = 'In SECTION3 of test after 1 set, 0 gets'; // (we set a default value in the prototype)
    116 actual = [obj.nameSETS,obj.nameGETS];
    117 expect = [1,0];
    118 addThis();
    119 
    120 s = obj.name;
    121 status = 'In SECTION3 of test after 1 set, 1 get';
    122 actual = [obj.nameSETS,obj.nameGETS];
    123 expect = [1,1];
    124 addThis();
    125 
    126 obj.name = cnFRED;
    127 status = 'In SECTION3 of test after 2 sets, 1 get';
    128 actual = [obj.nameSETS,obj.nameGETS];
    129 expect = [2,1];
    130 addThis();
    131 
    132 obj.name = obj.name;
    133 status = 'In SECTION3 of test after 3 sets, 2 gets';
    134 actual = [obj.nameSETS,obj.nameGETS];
    135 expect = [3,2];
    136 addThis();
    137 
    138 obj2 = new TestObject();
    139 status = 'obj2 = new TestObject() after 1 set, 0 gets';
    140 actual = [obj2.nameSETS,obj2.nameGETS];
    141 expect = [1,0]; // we set a default value in the prototype -
    142 addThis();
    143 
    144 // Use both obj and obj2  -
    145 obj2.name = obj.name +  obj2.name;
    146 status = 'obj2 = new TestObject() after 2 sets, 1 get';
    147 actual = [obj2.nameSETS,obj2.nameGETS];
    148 expect = [2,1];
    149 addThis();
    150 
    151 status = 'In SECTION3 of test after 3 sets, 3 gets';
    152 actual = [obj.nameSETS,obj.nameGETS];
    153 expect = [3,3];  // we left off at [3,2] above -
    154 addThis();
    155 
    156 
    157 //---------------------------------------------------------------------------------
    158 test();
    159 //---------------------------------------------------------------------------------
    160 
    161 
    162 function addThis()
    163 {
    164  statusitems[UBound] = status;
    165  actualvalues[UBound] = actual.toString();
    166  expectedvalues[UBound] = expect.toString();
    167  UBound++;
    168 }
    169 
    170 
    171 function test()
    172 {
    173  printBugNumber(BUGNUMBER);
    174  printStatus (summary);
    175 
    176  for (var i = 0; i < UBound; i++)
    177  {
    178    reportCompare(expectedvalues[i], actualvalues[i], getStatus(i));
    179  }
    180 }
    181 
    182 
    183 function getStatus(i)
    184 {
    185  return statprefix + statusitems[i];
    186 }