1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
23 * pschwartau@netscape.com
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
42 * SUMMARY: Testing obj.__defineSetter__(), obj.__defineGetter__()
43 * Note: this is a non-ECMA language extension
45 //-----------------------------------------------------------------------------
47 var BUGNUMBER = '(none)';
48 var summary = 'Testing obj.__defineSetter__(), obj.__defineGetter__()';
49 var statprefix = 'Status: ';
51 var statusitems = [ ];
53 var actualvalues = [ ];
55 var expectedvalues = [ ];
56 var cnDEFAULT = 'default name';
63 // SECTION1: define getter/setter directly on an object (not its prototype)
67 obj.__defineSetter__('name', function(newValue) {this._name=newValue; this.nameSETS++;});
68 obj.__defineGetter__('name', function() {this.nameGETS++; return this._name;});
70 status = 'In SECTION1 of test after 0 sets, 0 gets';
71 actual = [obj.nameSETS,obj.nameGETS];
76 status = 'In SECTION1 of test after 0 sets, 1 get';
77 actual = [obj.nameSETS,obj.nameGETS];
82 status = 'In SECTION1 of test after 1 set, 1 get';
83 actual = [obj.nameSETS,obj.nameGETS];
88 status = 'In SECTION1 of test after 2 sets, 2 gets';
89 actual = [obj.nameSETS,obj.nameGETS];
94 // SECTION2: define getter/setter in Object.prototype
95 Object.prototype.nameSETS = 0;
96 Object.prototype.nameGETS = 0;
97 Object.prototype.__defineSetter__('name', function(newValue) {this._name=newValue; this.nameSETS++;});
98 Object.prototype.__defineGetter__('name', function() {this.nameGETS++; return this._name;});
101 status = 'In SECTION2 of test after 0 sets, 0 gets';
102 actual = [obj.nameSETS,obj.nameGETS];
107 status = 'In SECTION2 of test after 0 sets, 1 get';
108 actual = [obj.nameSETS,obj.nameGETS];
113 status = 'In SECTION2 of test after 1 set, 1 get';
114 actual = [obj.nameSETS,obj.nameGETS];
119 status = 'In SECTION2 of test after 2 sets, 2 gets';
120 actual = [obj.nameSETS,obj.nameGETS];
125 // SECTION 3: define getter/setter in prototype of user-defined constructor
126 function TestObject()
129 TestObject.prototype.nameSETS = 0;
130 TestObject.prototype.nameGETS = 0;
131 TestObject.prototype.__defineSetter__('name', function(newValue) {this._name=newValue; this.nameSETS++;});
132 TestObject.prototype.__defineGetter__('name', function() {this.nameGETS++; return this._name;});
133 TestObject.prototype.name = cnDEFAULT;
135 obj = new TestObject();
136 status = 'In SECTION3 of test after 1 set, 0 gets'; // (we set a default value in the prototype)
137 actual = [obj.nameSETS,obj.nameGETS];
142 status = 'In SECTION3 of test after 1 set, 1 get';
143 actual = [obj.nameSETS,obj.nameGETS];
148 status = 'In SECTION3 of test after 2 sets, 1 get';
149 actual = [obj.nameSETS,obj.nameGETS];
154 status = 'In SECTION3 of test after 3 sets, 2 gets';
155 actual = [obj.nameSETS,obj.nameGETS];
159 obj2 = new TestObject();
160 status = 'obj2 = new TestObject() after 1 set, 0 gets';
161 actual = [obj2.nameSETS,obj2.nameGETS];
162 expect = [1,0]; // we set a default value in the prototype -
165 // Use both obj and obj2 -
166 obj2.name = obj.name + obj2.name;
167 status = 'obj2 = new TestObject() after 2 sets, 1 get';
168 actual = [obj2.nameSETS,obj2.nameGETS];
172 status = 'In SECTION3 of test after 3 sets, 3 gets';
173 actual = [obj.nameSETS,obj.nameGETS];
174 expect = [3,3]; // we left off at [3,2] above -
178 //---------------------------------------------------------------------------------
180 //---------------------------------------------------------------------------------
185 statusitems[UBound] = status;
186 actualvalues[UBound] = actual.toString();
187 expectedvalues[UBound] = expect.toString();
195 printBugNumber(BUGNUMBER);
196 printStatus (summary);
198 for (var i = 0; i < UBound; i++)
200 reportCompare(expectedvalues[i], actualvalues[i], getStatus(i));
207 function getStatus(i)
209 return statprefix + statusitems[i];