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@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//------------------------------------------------------------------------------------------------- 29var UBound = 0; 30var bug = '(none)'; 31var summary = 'Testing obj.__defineSetter__(), obj.__defineGetter__()'; 32var statprefix = 'Status: '; 33var status = ''; 34var statusitems = [ ]; 35var actual = ''; 36var actualvalues = [ ]; 37var expect= ''; 38var expectedvalues = [ ]; 39var cnName = 'name'; 40var cnDEFAULT = 'default name'; 41var cnFRED = 'Fred'; 42var obj = {}; 43var obj2 = {}; 44var s = ''; 45 46 47// The getter/setter functions we'll use in all three sections below - 48var cnNameSetter = function(newValue) {this._name=newValue; this.nameSETS++;}; 49var cnNameGetter = function() {this.nameGETS++; return this._name;}; 50 51 52// SECTION1: define getter/setter directly on an object (not its prototype) 53obj = new Object(); 54obj.nameSETS = 0; 55obj.nameGETS = 0; 56obj.__defineSetter__(cnName, cnNameSetter); 57obj.__defineGetter__(cnName, cnNameGetter); 58 59status = 'In SECTION1 of test after 0 sets, 0 gets'; 60actual = [obj.nameSETS,obj.nameGETS]; 61expect = [0,0]; 62addThis(); 63 64s = obj.name; 65status = 'In SECTION1 of test after 0 sets, 1 get'; 66actual = [obj.nameSETS,obj.nameGETS]; 67expect = [0,1]; 68addThis(); 69 70obj.name = cnFRED; 71status = 'In SECTION1 of test after 1 set, 1 get'; 72actual = [obj.nameSETS,obj.nameGETS]; 73expect = [1,1]; 74addThis(); 75 76obj.name = obj.name; 77status = 'In SECTION1 of test after 2 sets, 2 gets'; 78actual = [obj.nameSETS,obj.nameGETS]; 79expect = [2,2]; 80addThis(); 81 82 83// SECTION2: define getter/setter in Object.prototype 84Object.prototype.nameSETS = 0; 85Object.prototype.nameGETS = 0; 86Object.prototype.__defineSetter__(cnName, cnNameSetter); 87Object.prototype.__defineGetter__(cnName, cnNameGetter); 88 89obj = new Object(); 90status = 'In SECTION2 of test after 0 sets, 0 gets'; 91actual = [obj.nameSETS,obj.nameGETS]; 92expect = [0,0]; 93addThis(); 94 95s = obj.name; 96status = 'In SECTION2 of test after 0 sets, 1 get'; 97actual = [obj.nameSETS,obj.nameGETS]; 98expect = [0,1]; 99addThis(); 100 101obj.name = cnFRED; 102status = 'In SECTION2 of test after 1 set, 1 get'; 103actual = [obj.nameSETS,obj.nameGETS]; 104expect = [1,1]; 105addThis(); 106 107obj.name = obj.name; 108status = 'In SECTION2 of test after 2 sets, 2 gets'; 109actual = [obj.nameSETS,obj.nameGETS]; 110expect = [2,2]; 111addThis(); 112 113 114// SECTION 3: define getter/setter in prototype of user-defined constructor 115function TestObject() 116{ 117} 118TestObject.prototype.nameSETS = 0; 119TestObject.prototype.nameGETS = 0; 120TestObject.prototype.__defineSetter__(cnName, cnNameSetter); 121TestObject.prototype.__defineGetter__(cnName, cnNameGetter); 122TestObject.prototype.name = cnDEFAULT; 123 124obj = new TestObject(); 125status = 'In SECTION3 of test after 1 set, 0 gets'; // (we set a default value in the prototype) 126actual = [obj.nameSETS,obj.nameGETS]; 127expect = [1,0]; 128addThis(); 129 130s = obj.name; 131status = 'In SECTION3 of test after 1 set, 1 get'; 132actual = [obj.nameSETS,obj.nameGETS]; 133expect = [1,1]; 134addThis(); 135 136obj.name = cnFRED; 137status = 'In SECTION3 of test after 2 sets, 1 get'; 138actual = [obj.nameSETS,obj.nameGETS]; 139expect = [2,1]; 140addThis(); 141 142obj.name = obj.name; 143status = 'In SECTION3 of test after 3 sets, 2 gets'; 144actual = [obj.nameSETS,obj.nameGETS]; 145expect = [3,2]; 146addThis(); 147 148obj2 = new TestObject(); 149status = 'obj2 = new TestObject() after 1 set, 0 gets'; 150actual = [obj2.nameSETS,obj2.nameGETS]; 151expect = [1,0]; // we set a default value in the prototype - 152addThis(); 153 154// Use both obj and obj2 - 155obj2.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//--------------------------------------------------------------------------------- 168test(); 169//--------------------------------------------------------------------------------- 170 171 172function addThis() 173{ 174 statusitems[UBound] = status; 175 actualvalues[UBound] = actual.toString(); 176 expectedvalues[UBound] = expect.toString(); 177 UBound++; 178} 179 180 181function 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 196function getStatus(i) 197{ 198 return statprefix + statusitems[i]; 199} 200