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