1 /* 2 * The contents of this file are subject to the Netscape Public 3 * License Version 1.1 (the "License"); you may not use this file 4 * except in compliance with the License. You may obtain a copy of 5 * the License at http://www.mozilla.org/NPL/ 6 * 7 * Software distributed under the License is distributed on an "AS IS" 8 * basis, WITHOUT WARRANTY OF ANY KIND, either expressed 9 * or implied. See the License for the specific language governing 10 * rights and limitations under the License. 11 * 12 * The Original Code is mozilla.org code. 13 * 14 * The Initial Developer of the Original Code is Netscape 15 * Communications Corporation. Portions created by Netscape are 16 * Copyright (C) 1998 Netscape Communications Corporation. All 17 * Rights Reserved. 18 * 19 * Contributor(s): pschwartau (at) netscape.com 20 * Date: 14 April 2001 21 * 22 * SUMMARY: Testing obj.__defineSetter__(), obj.__defineGetter__() 23 * Note: this is a non-ECMA language extension 24 * 25 * This test is the same as getset-004.js, except that here we 26 * store the getter/setter functions in global variables. 27 */ 28 //------------------------------------------------------------------------------------------------- 29 var UBound = 0; 30 var bug = '(none)'; 31 var summary = 'Testing obj.__defineSetter__(), obj.__defineGetter__()'; 32 var statprefix = 'Status: '; 33 var status = ''; 34 var statusitems = [ ]; 35 var actual = ''; 36 var actualvalues = [ ]; 37 var expect= ''; 38 var expectedvalues = [ ]; 39 var cnName = 'name'; 40 var cnDEFAULT = 'default name'; 41 var cnFRED = 'Fred'; 42 var obj = {}; 43 var obj2 = {}; 44 var s = ''; 45 46 47 // The getter/setter functions we'll use in all three sections below - 48 var cnNameSetter = function(newValue) {this._name=newValue; this.nameSETS++;}; 49 var cnNameGetter = function() {this.nameGETS++; return this._name;}; 50 51 52 // SECTION1: define getter/setter directly on an object (not its prototype) 53 obj = new Object(); 54 obj.nameSETS = 0; 55 obj.nameGETS = 0; 56 obj.__defineSetter__(cnName, cnNameSetter); 57 obj.__defineGetter__(cnName, cnNameGetter); 58 59 status = 'In SECTION1 of test after 0 sets, 0 gets'; 60 actual = [obj.nameSETS,obj.nameGETS]; 61 expect = [0,0]; 62 addThis(); 63 64 s = obj.name; 65 status = 'In SECTION1 of test after 0 sets, 1 get'; 66 actual = [obj.nameSETS,obj.nameGETS]; 67 expect = [0,1]; 68 addThis(); 69 70 obj.name = cnFRED; 71 status = 'In SECTION1 of test after 1 set, 1 get'; 72 actual = [obj.nameSETS,obj.nameGETS]; 73 expect = [1,1]; 74 addThis(); 75 76 obj.name = obj.name; 77 status = 'In SECTION1 of test after 2 sets, 2 gets'; 78 actual = [obj.nameSETS,obj.nameGETS]; 79 expect = [2,2]; 80 addThis(); 81 82 83 // SECTION2: define getter/setter in Object.prototype 84 Object.prototype.nameSETS = 0; 85 Object.prototype.nameGETS = 0; 86 Object.prototype.__defineSetter__(cnName, cnNameSetter); 87 Object.prototype.__defineGetter__(cnName, cnNameGetter); 88 89 obj = new Object(); 90 status = 'In SECTION2 of test after 0 sets, 0 gets'; 91 actual = [obj.nameSETS,obj.nameGETS]; 92 expect = [0,0]; 93 addThis(); 94 95 s = obj.name; 96 status = 'In SECTION2 of test after 0 sets, 1 get'; 97 actual = [obj.nameSETS,obj.nameGETS]; 98 expect = [0,1]; 99 addThis(); 100 101 obj.name = cnFRED; 102 status = 'In SECTION2 of test after 1 set, 1 get'; 103 actual = [obj.nameSETS,obj.nameGETS]; 104 expect = [1,1]; 105 addThis(); 106 107 obj.name = obj.name; 108 status = 'In SECTION2 of test after 2 sets, 2 gets'; 109 actual = [obj.nameSETS,obj.nameGETS]; 110 expect = [2,2]; 111 addThis(); 112 113 114 // SECTION 3: define getter/setter in prototype of user-defined constructor 115 function TestObject() 116 { 117 } 118 TestObject.prototype.nameSETS = 0; 119 TestObject.prototype.nameGETS = 0; 120 TestObject.prototype.__defineSetter__(cnName, cnNameSetter); 121 TestObject.prototype.__defineGetter__(cnName, cnNameGetter); 122 TestObject.prototype.name = cnDEFAULT; 123 124 obj = new TestObject(); 125 status = 'In SECTION3 of test after 1 set, 0 gets'; // (we set a default value in the prototype) 126 actual = [obj.nameSETS,obj.nameGETS]; 127 expect = [1,0]; 128 addThis(); 129 130 s = obj.name; 131 status = 'In SECTION3 of test after 1 set, 1 get'; 132 actual = [obj.nameSETS,obj.nameGETS]; 133 expect = [1,1]; 134 addThis(); 135 136 obj.name = cnFRED; 137 status = 'In SECTION3 of test after 2 sets, 1 get'; 138 actual = [obj.nameSETS,obj.nameGETS]; 139 expect = [2,1]; 140 addThis(); 141 142 obj.name = obj.name; 143 status = 'In SECTION3 of test after 3 sets, 2 gets'; 144 actual = [obj.nameSETS,obj.nameGETS]; 145 expect = [3,2]; 146 addThis(); 147 148 obj2 = new TestObject(); 149 status = 'obj2 = new TestObject() after 1 set, 0 gets'; 150 actual = [obj2.nameSETS,obj2.nameGETS]; 151 expect = [1,0]; // we set a default value in the prototype - 152 addThis(); 153 154 // Use both obj and obj2 - 155 obj2.name = obj.name + obj2.name; 156 status = 'obj2 = new TestObject() after 2 sets, 1 get'; 157 actual = [obj2.nameSETS,obj2.nameGETS]; 158 expect = [2,1]; 159 addThis(); 160 161 status = 'In SECTION3 of test after 3 sets, 3 gets'; 162 actual = [obj.nameSETS,obj.nameGETS]; 163 expect = [3,3]; // we left off at [3,2] above - 164 addThis(); 165 166 167 //--------------------------------------------------------------------------------- 168 test(); 169 //--------------------------------------------------------------------------------- 170 171 172 function addThis() 173 { 174 statusitems[UBound] = status; 175 actualvalues[UBound] = actual.toString(); 176 expectedvalues[UBound] = expect.toString(); 177 UBound++; 178 } 179 180 181 function test() 182 { 183 enterFunc ('test'); 184 printBugNumber (bug); 185 printStatus (summary); 186 187 for (var i = 0; i < UBound; i++) 188 { 189 reportCompare(expectedvalues[i], actualvalues[i], getStatus(i)); 190 } 191 192 exitFunc ('test'); 193 } 194 195 196 function getStatus(i) 197 { 198 return statprefix + statusitems[i]; 199 } 200