Imported Upstream version 1.0.0
[platform/upstream/js.git] / js / src / tests / js1_5 / extensions / getset-003.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.prop getter/setter
43  * Note: this is a non-ECMA extension to the language.
44  */
45 //-----------------------------------------------------------------------------
46 var UBound = 0;
47 var BUGNUMBER = '(none)';
48 var summary = 'Testing obj.prop getter/setter';
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 Object.defineProperty(obj, "name",
68 {
69   enumerable: true, configurable: true,
70   set: function(newValue) {this._name=newValue; this.nameSETS++;},
71   get: function() {this.nameGETS++; return this._name;}
72 });
73
74     status = 'In SECTION1 of test after 0 sets, 0 gets';
75 actual = [obj.nameSETS,obj.nameGETS];
76 expect = [0,0];
77 addThis();
78
79 s = obj.name;
80 status = 'In SECTION1 of test after 0 sets, 1 get';
81 actual = [obj.nameSETS,obj.nameGETS];
82 expect = [0,1];
83 addThis();
84
85 obj.name = cnFRED;
86 status = 'In SECTION1 of test after 1 set, 1 get';
87 actual = [obj.nameSETS,obj.nameGETS];
88 expect = [1,1];
89 addThis();
90
91 obj.name = obj.name;
92 status = 'In SECTION1 of test after 2 sets, 2 gets';
93 actual = [obj.nameSETS,obj.nameGETS];
94 expect = [2,2];
95 addThis();
96
97
98 // SECTION2: define getter/setter in Object.prototype
99 Object.prototype.nameSETS = 0;
100 Object.prototype.nameGETS = 0;
101 Object.defineProperty(Object.prototype, "name",
102 {
103   enumerable: true, configurable: true,
104   set: function(newValue) {this._name=newValue; this.nameSETS++;},
105   get: function() {this.nameGETS++; return this._name;}
106 });
107
108     obj = new Object();
109 status = 'In SECTION2 of test after 0 sets, 0 gets';
110 actual = [obj.nameSETS,obj.nameGETS];
111 expect = [0,0];
112 addThis();
113
114 s = obj.name;
115 status = 'In SECTION2 of test after 0 sets, 1 get';
116 actual = [obj.nameSETS,obj.nameGETS];
117 expect = [0,1];
118 addThis();
119
120 obj.name = cnFRED;
121 status = 'In SECTION2 of test after 1 set, 1 get';
122 actual = [obj.nameSETS,obj.nameGETS];
123 expect = [1,1];
124 addThis();
125
126 obj.name = obj.name;
127 status = 'In SECTION2 of test after 2 sets, 2 gets';
128 actual = [obj.nameSETS,obj.nameGETS];
129 expect = [2,2];
130 addThis();
131
132
133 // SECTION 3: define getter/setter in prototype of user-defined constructor
134 function TestObject()
135 {
136 }
137 TestObject.prototype.nameSETS = 0;
138 TestObject.prototype.nameGETS = 0;
139 Object.defineProperty(TestObject.prototype, "name",
140 {
141   enumerable: true, configurable: true,
142   set: function(newValue) {this._name=newValue; this.nameSETS++;},
143   get: function() {this.nameGETS++; return this._name;}
144 });
145     TestObject.prototype.name = cnDEFAULT;
146
147 obj = new TestObject();
148 status = 'In SECTION3 of test after 1 set, 0 gets'; // (we set a default value in the prototype)
149 actual = [obj.nameSETS,obj.nameGETS];
150 expect = [1,0];
151 addThis();
152
153 s = obj.name;
154 status = 'In SECTION3 of test after 1 set, 1 get';
155 actual = [obj.nameSETS,obj.nameGETS];
156 expect = [1,1];
157 addThis();
158
159 obj.name = cnFRED;
160 status = 'In SECTION3 of test after 2 sets, 1 get';
161 actual = [obj.nameSETS,obj.nameGETS];
162 expect = [2,1];
163 addThis();
164
165 obj.name = obj.name;
166 status = 'In SECTION3 of test after 3 sets, 2 gets';
167 actual = [obj.nameSETS,obj.nameGETS];
168 expect = [3,2];
169 addThis();
170
171 obj2 = new TestObject();
172 status = 'obj2 = new TestObject() after 1 set, 0 gets';
173 actual = [obj2.nameSETS,obj2.nameGETS];
174 expect = [1,0]; // we set a default value in the prototype -
175 addThis();
176
177 // Use both obj and obj2  -
178 obj2.name = obj.name +  obj2.name;
179 status = 'obj2 = new TestObject() after 2 sets, 1 get';
180 actual = [obj2.nameSETS,obj2.nameGETS];
181 expect = [2,1];
182 addThis();
183
184 status = 'In SECTION3 of test after 3 sets, 3 gets';
185 actual = [obj.nameSETS,obj.nameGETS];
186 expect = [3,3];  // we left off at [3,2] above -
187 addThis();
188
189
190 //---------------------------------------------------------------------------------
191 test();
192 //---------------------------------------------------------------------------------
193
194
195 function addThis()
196 {
197   statusitems[UBound] = status;
198   actualvalues[UBound] = actual.toString();
199   expectedvalues[UBound] = expect.toString();
200   UBound++;
201 }
202
203
204 function test()
205 {
206   enterFunc ('test');
207   printBugNumber(BUGNUMBER);
208   printStatus (summary);
209
210   for (var i = 0; i < UBound; i++)
211   {
212     reportCompare(expectedvalues[i], actualvalues[i], getStatus(i));
213   }
214
215   exitFunc ('test');
216 }
217
218
219 function getStatus(i)
220 {
221   return statprefix + statusitems[i];
222 }