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/
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.
12 * The Original Code is mozilla.org code.
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
19 * Contributor(s): pschwartau@netscape.com
22 * SUMMARY: Testing obj.__defineSetter__(), obj.__defineGetter__()
23 * Note: this is a non-ECMA language extension
25 //-------------------------------------------------------------------------------------------------
28 var summary = 'Testing obj.__defineSetter__(), obj.__defineGetter__()';
29 var statprefix = 'Status: ';
31 var statusitems = [ ];
33 var actualvalues = [ ];
35 var expectedvalues = [ ];
36 var cnDEFAULT = 'default name';
43 // SECTION1: define getter/setter directly on an object (not its prototype)
47 obj.__defineSetter__('name', function(newValue) {this._name=newValue; this.nameSETS++;});
48 obj.__defineGetter__('name', function() {this.nameGETS++; return this._name;});
50 status = 'In SECTION1 of test after 0 sets, 0 gets';
51 actual = [obj.nameSETS,obj.nameGETS];
56 status = 'In SECTION1 of test after 0 sets, 1 get';
57 actual = [obj.nameSETS,obj.nameGETS];
62 status = 'In SECTION1 of test after 1 set, 1 get';
63 actual = [obj.nameSETS,obj.nameGETS];
68 status = 'In SECTION1 of test after 2 sets, 2 gets';
69 actual = [obj.nameSETS,obj.nameGETS];
74 // SECTION2: define getter/setter in Object.prototype
75 Object.prototype.nameSETS = 0;
76 Object.prototype.nameGETS = 0;
77 Object.prototype.__defineSetter__('name', function(newValue) {this._name=newValue; this.nameSETS++;});
78 Object.prototype.__defineGetter__('name', function() {this.nameGETS++; return this._name;});
81 status = 'In SECTION2 of test after 0 sets, 0 gets';
82 actual = [obj.nameSETS,obj.nameGETS];
87 status = 'In SECTION2 of test after 0 sets, 1 get';
88 actual = [obj.nameSETS,obj.nameGETS];
93 status = 'In SECTION2 of test after 1 set, 1 get';
94 actual = [obj.nameSETS,obj.nameGETS];
99 status = 'In SECTION2 of test after 2 sets, 2 gets';
100 actual = [obj.nameSETS,obj.nameGETS];
105 // SECTION 3: define getter/setter in prototype of user-defined constructor
106 function TestObject()
109 TestObject.prototype.nameSETS = 0;
110 TestObject.prototype.nameGETS = 0;
111 TestObject.prototype.__defineSetter__('name', function(newValue) {this._name=newValue; this.nameSETS++;});
112 TestObject.prototype.__defineGetter__('name', function() {this.nameGETS++; return this._name;});
113 TestObject.prototype.name = cnDEFAULT;
115 obj = new TestObject();
116 status = 'In SECTION3 of test after 1 set, 0 gets'; // (we set a default value in the prototype)
117 actual = [obj.nameSETS,obj.nameGETS];
122 status = 'In SECTION3 of test after 1 set, 1 get';
123 actual = [obj.nameSETS,obj.nameGETS];
128 status = 'In SECTION3 of test after 2 sets, 1 get';
129 actual = [obj.nameSETS,obj.nameGETS];
134 status = 'In SECTION3 of test after 3 sets, 2 gets';
135 actual = [obj.nameSETS,obj.nameGETS];
139 obj2 = new TestObject();
140 status = 'obj2 = new TestObject() after 1 set, 0 gets';
141 actual = [obj2.nameSETS,obj2.nameGETS];
142 expect = [1,0]; // we set a default value in the prototype -
145 // Use both obj and obj2 -
146 obj2.name = obj.name + obj2.name;
147 status = 'obj2 = new TestObject() after 2 sets, 1 get';
148 actual = [obj2.nameSETS,obj2.nameGETS];
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 -
158 //---------------------------------------------------------------------------------
160 //---------------------------------------------------------------------------------
165 statusitems[UBound] = status;
166 actualvalues[UBound] = actual.toString();
167 expectedvalues[UBound] = expect.toString();
175 printBugNumber (bug);
176 printStatus (summary);
178 for (var i = 0; i < UBound; i++)
180 reportCompare(expectedvalues[i], actualvalues[i], getStatus(i));
187 function getStatus(i)
189 return statprefix + statusitems[i];