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 * This test is the same as getset-004.js, except that here we
46 * store the getter/setter functions in global variables.
48 //-----------------------------------------------------------------------------
50 var BUGNUMBER = '(none)';
51 var summary = 'Testing obj.__defineSetter__(), obj.__defineGetter__()';
52 var statprefix = 'Status: ';
54 var statusitems = [ ];
56 var actualvalues = [ ];
58 var expectedvalues = [ ];
60 var cnDEFAULT = 'default name';
67 // The getter/setter functions we'll use in all three sections below -
68 var cnNameSetter = function(newValue) {this._name=newValue; this.nameSETS++;};
69 var cnNameGetter = function() {this.nameGETS++; return this._name;};
72 // SECTION1: define getter/setter directly on an object (not its prototype)
76 obj.__defineSetter__(cnName, cnNameSetter);
77 obj.__defineGetter__(cnName, cnNameGetter);
79 status = 'In SECTION1 of test after 0 sets, 0 gets';
80 actual = [obj.nameSETS,obj.nameGETS];
85 status = 'In SECTION1 of test after 0 sets, 1 get';
86 actual = [obj.nameSETS,obj.nameGETS];
91 status = 'In SECTION1 of test after 1 set, 1 get';
92 actual = [obj.nameSETS,obj.nameGETS];
97 status = 'In SECTION1 of test after 2 sets, 2 gets';
98 actual = [obj.nameSETS,obj.nameGETS];
103 // SECTION2: define getter/setter in Object.prototype
104 Object.prototype.nameSETS = 0;
105 Object.prototype.nameGETS = 0;
106 Object.prototype.__defineSetter__(cnName, cnNameSetter);
107 Object.prototype.__defineGetter__(cnName, cnNameGetter);
110 status = 'In SECTION2 of test after 0 sets, 0 gets';
111 actual = [obj.nameSETS,obj.nameGETS];
116 status = 'In SECTION2 of test after 0 sets, 1 get';
117 actual = [obj.nameSETS,obj.nameGETS];
122 status = 'In SECTION2 of test after 1 set, 1 get';
123 actual = [obj.nameSETS,obj.nameGETS];
128 status = 'In SECTION2 of test after 2 sets, 2 gets';
129 actual = [obj.nameSETS,obj.nameGETS];
134 // SECTION 3: define getter/setter in prototype of user-defined constructor
135 function TestObject()
138 TestObject.prototype.nameSETS = 0;
139 TestObject.prototype.nameGETS = 0;
140 TestObject.prototype.__defineSetter__(cnName, cnNameSetter);
141 TestObject.prototype.__defineGetter__(cnName, cnNameGetter);
142 TestObject.prototype.name = cnDEFAULT;
144 obj = new TestObject();
145 status = 'In SECTION3 of test after 1 set, 0 gets'; // (we set a default value in the prototype)
146 actual = [obj.nameSETS,obj.nameGETS];
151 status = 'In SECTION3 of test after 1 set, 1 get';
152 actual = [obj.nameSETS,obj.nameGETS];
157 status = 'In SECTION3 of test after 2 sets, 1 get';
158 actual = [obj.nameSETS,obj.nameGETS];
163 status = 'In SECTION3 of test after 3 sets, 2 gets';
164 actual = [obj.nameSETS,obj.nameGETS];
168 obj2 = new TestObject();
169 status = 'obj2 = new TestObject() after 1 set, 0 gets';
170 actual = [obj2.nameSETS,obj2.nameGETS];
171 expect = [1,0]; // we set a default value in the prototype -
174 // Use both obj and obj2 -
175 obj2.name = obj.name + obj2.name;
176 status = 'obj2 = new TestObject() after 2 sets, 1 get';
177 actual = [obj2.nameSETS,obj2.nameGETS];
181 status = 'In SECTION3 of test after 3 sets, 3 gets';
182 actual = [obj.nameSETS,obj.nameGETS];
183 expect = [3,3]; // we left off at [3,2] above -
187 //---------------------------------------------------------------------------------
189 //---------------------------------------------------------------------------------
194 statusitems[UBound] = status;
195 actualvalues[UBound] = actual.toString();
196 expectedvalues[UBound] = expect.toString();
204 printBugNumber(BUGNUMBER);
205 printStatus (summary);
207 for (var i = 0; i < UBound; i++)
209 reportCompare(expectedvalues[i], actualvalues[i], getStatus(i));
216 function getStatus(i)
218 return statprefix + statusitems[i];