Imported Upstream version 1.0.0
[platform/upstream/js.git] / js / src / tests / js1_5 / extensions / getset-004.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 //-----------------------------------------------------------------------------
46 var UBound = 0;
47 var BUGNUMBER = '(none)';
48 var summary = 'Testing  obj.__defineSetter__(), obj.__defineGetter__()';
49 var statprefix = 'Status: ';
50 var status = '';
51 var statusitems = [ ];
52 var actual = '';
53 var actualvalues = [ ];
54 var expect= '';
55 var expectedvalues = [ ];
56 var cnDEFAULT = 'default name';
57 var cnFRED = 'Fred';
58 var obj = {};
59 var obj2 = {};
60 var s = '';
61
62
63 // SECTION1: define getter/setter directly on an object (not its prototype)
64 obj = new Object();
65 obj.nameSETS = 0;
66 obj.nameGETS = 0;
67 obj.__defineSetter__('name', function(newValue) {this._name=newValue; this.nameSETS++;});
68 obj.__defineGetter__('name', function() {this.nameGETS++; return this._name;});
69
70 status = 'In SECTION1 of test after 0 sets, 0 gets';
71 actual = [obj.nameSETS,obj.nameGETS];
72 expect = [0,0];
73 addThis();
74
75 s = obj.name;
76 status = 'In SECTION1 of test after 0 sets, 1 get';
77 actual = [obj.nameSETS,obj.nameGETS];
78 expect = [0,1];
79 addThis();
80
81 obj.name = cnFRED;
82 status = 'In SECTION1 of test after 1 set, 1 get';
83 actual = [obj.nameSETS,obj.nameGETS];
84 expect = [1,1];
85 addThis();
86
87 obj.name = obj.name;
88 status = 'In SECTION1 of test after 2 sets, 2 gets';
89 actual = [obj.nameSETS,obj.nameGETS];
90 expect = [2,2];
91 addThis();
92
93
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;});
99
100 obj = new Object();
101 status = 'In SECTION2 of test after 0 sets, 0 gets';
102 actual = [obj.nameSETS,obj.nameGETS];
103 expect = [0,0];
104 addThis();
105
106 s = obj.name;
107 status = 'In SECTION2 of test after 0 sets, 1 get';
108 actual = [obj.nameSETS,obj.nameGETS];
109 expect = [0,1];
110 addThis();
111
112 obj.name = cnFRED;
113 status = 'In SECTION2 of test after 1 set, 1 get';
114 actual = [obj.nameSETS,obj.nameGETS];
115 expect = [1,1];
116 addThis();
117
118 obj.name = obj.name;
119 status = 'In SECTION2 of test after 2 sets, 2 gets';
120 actual = [obj.nameSETS,obj.nameGETS];
121 expect = [2,2];
122 addThis();
123
124
125 // SECTION 3: define getter/setter in prototype of user-defined constructor
126 function TestObject()
127 {
128 }
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;
134
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];
138 expect = [1,0];
139 addThis();
140
141 s = obj.name;
142 status = 'In SECTION3 of test after 1 set, 1 get';
143 actual = [obj.nameSETS,obj.nameGETS];
144 expect = [1,1];
145 addThis();
146
147 obj.name = cnFRED;
148 status = 'In SECTION3 of test after 2 sets, 1 get';
149 actual = [obj.nameSETS,obj.nameGETS];
150 expect = [2,1];
151 addThis();
152
153 obj.name = obj.name;
154 status = 'In SECTION3 of test after 3 sets, 2 gets';
155 actual = [obj.nameSETS,obj.nameGETS];
156 expect = [3,2];
157 addThis();
158
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 -
163 addThis();
164
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];
169 expect = [2,1];
170 addThis();
171
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 -
175 addThis();
176
177
178 //---------------------------------------------------------------------------------
179 test();
180 //---------------------------------------------------------------------------------
181
182
183 function addThis()
184 {
185   statusitems[UBound] = status;
186   actualvalues[UBound] = actual.toString();
187   expectedvalues[UBound] = expect.toString();
188   UBound++;
189 }
190
191
192 function test()
193 {
194   enterFunc ('test');
195   printBugNumber(BUGNUMBER);
196   printStatus (summary);
197
198   for (var i = 0; i < UBound; i++)
199   {
200     reportCompare(expectedvalues[i], actualvalues[i], getStatus(i));
201   }
202
203   exitFunc ('test');
204 }
205
206
207 function getStatus(i)
208 {
209   return statprefix + statusitems[i];
210 }