Imported Upstream version 1.0.0
[platform/upstream/js.git] / js / src / tests / js1_5 / extensions / getset-005.js
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
4  *
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/
9  *
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
13  * License.
14  *
15  * The Original Code is mozilla.org code.
16  *
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.
21  *
22  * Contributor(s):
23  *   pschwartau@netscape.com
24  *
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.
36  *
37  * ***** END LICENSE BLOCK ***** */
38
39 /*
40  * Date: 14 April 2001
41  *
42  * SUMMARY: Testing  obj.__defineSetter__(), obj.__defineGetter__()
43  * Note: this is a non-ECMA language extension
44  *
45  * This test is the same as getset-004.js, except that here we
46  * store the getter/setter functions in global variables.
47  */
48 //-----------------------------------------------------------------------------
49 var UBound = 0;
50 var BUGNUMBER = '(none)';
51 var summary = 'Testing  obj.__defineSetter__(), obj.__defineGetter__()';
52 var statprefix = 'Status: ';
53 var status = '';
54 var statusitems = [ ];
55 var actual = '';
56 var actualvalues = [ ];
57 var expect= '';
58 var expectedvalues = [ ];
59 var cnName = 'name';
60 var cnDEFAULT = 'default name';
61 var cnFRED = 'Fred';
62 var obj = {};
63 var obj2 = {};
64 var s = '';
65
66
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;};
70
71
72 // SECTION1: define getter/setter directly on an object (not its prototype)
73 obj = new Object();
74 obj.nameSETS = 0;
75 obj.nameGETS = 0;
76 obj.__defineSetter__(cnName, cnNameSetter);
77 obj.__defineGetter__(cnName, cnNameGetter);
78
79 status = 'In SECTION1 of test after 0 sets, 0 gets';
80 actual = [obj.nameSETS,obj.nameGETS];
81 expect = [0,0];
82 addThis();
83
84 s = obj.name;
85 status = 'In SECTION1 of test after 0 sets, 1 get';
86 actual = [obj.nameSETS,obj.nameGETS];
87 expect = [0,1];
88 addThis();
89
90 obj.name = cnFRED;
91 status = 'In SECTION1 of test after 1 set, 1 get';
92 actual = [obj.nameSETS,obj.nameGETS];
93 expect = [1,1];
94 addThis();
95
96 obj.name = obj.name;
97 status = 'In SECTION1 of test after 2 sets, 2 gets';
98 actual = [obj.nameSETS,obj.nameGETS];
99 expect = [2,2];
100 addThis();
101
102
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);
108
109 obj = new Object();
110 status = 'In SECTION2 of test after 0 sets, 0 gets';
111 actual = [obj.nameSETS,obj.nameGETS];
112 expect = [0,0];
113 addThis();
114
115 s = obj.name;
116 status = 'In SECTION2 of test after 0 sets, 1 get';
117 actual = [obj.nameSETS,obj.nameGETS];
118 expect = [0,1];
119 addThis();
120
121 obj.name = cnFRED;
122 status = 'In SECTION2 of test after 1 set, 1 get';
123 actual = [obj.nameSETS,obj.nameGETS];
124 expect = [1,1];
125 addThis();
126
127 obj.name = obj.name;
128 status = 'In SECTION2 of test after 2 sets, 2 gets';
129 actual = [obj.nameSETS,obj.nameGETS];
130 expect = [2,2];
131 addThis();
132
133
134 // SECTION 3: define getter/setter in prototype of user-defined constructor
135 function TestObject()
136 {
137 }
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;
143
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];
147 expect = [1,0];
148 addThis();
149
150 s = obj.name;
151 status = 'In SECTION3 of test after 1 set, 1 get';
152 actual = [obj.nameSETS,obj.nameGETS];
153 expect = [1,1];
154 addThis();
155
156 obj.name = cnFRED;
157 status = 'In SECTION3 of test after 2 sets, 1 get';
158 actual = [obj.nameSETS,obj.nameGETS];
159 expect = [2,1];
160 addThis();
161
162 obj.name = obj.name;
163 status = 'In SECTION3 of test after 3 sets, 2 gets';
164 actual = [obj.nameSETS,obj.nameGETS];
165 expect = [3,2];
166 addThis();
167
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 -
172 addThis();
173
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];
178 expect = [2,1];
179 addThis();
180
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 -
184 addThis();
185
186
187 //---------------------------------------------------------------------------------
188 test();
189 //---------------------------------------------------------------------------------
190
191
192 function addThis()
193 {
194   statusitems[UBound] = status;
195   actualvalues[UBound] = actual.toString();
196   expectedvalues[UBound] = expect.toString();
197   UBound++;
198 }
199
200
201 function test()
202 {
203   enterFunc ('test');
204   printBugNumber(BUGNUMBER);
205   printStatus (summary);
206
207   for (var i = 0; i < UBound; i++)
208   {
209     reportCompare(expectedvalues[i], actualvalues[i], getStatus(i));
210   }
211
212   exitFunc ('test');
213 }
214
215
216 function getStatus(i)
217 {
218   return statprefix + statusitems[i];
219 }