getset-005.js (4800B)
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.__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 = ''; 32 33 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;}; 37 38 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); 45 46 status = 'In SECTION1 of test after 0 sets, 0 gets'; 47 actual = [obj.nameSETS,obj.nameGETS]; 48 expect = [0,0]; 49 addThis(); 50 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(); 56 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(); 62 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(); 68 69 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); 75 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(); 81 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(); 87 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(); 93 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(); 99 100 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; 110 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(); 116 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(); 122 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(); 128 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(); 134 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(); 140 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(); 147 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(); 152 153 154 //--------------------------------------------------------------------------------- 155 test(); 156 //--------------------------------------------------------------------------------- 157 158 159 function addThis() 160 { 161 statusitems[UBound] = status; 162 actualvalues[UBound] = actual.toString(); 163 expectedvalues[UBound] = expect.toString(); 164 UBound++; 165 } 166 167 168 function test() 169 { 170 printBugNumber(BUGNUMBER); 171 printStatus (summary); 172 173 for (var i = 0; i < UBound; i++) 174 { 175 reportCompare(expectedvalues[i], actualvalues[i], getStatus(i)); 176 } 177 } 178 179 180 function getStatus(i) 181 { 182 return statprefix + statusitems[i]; 183 }