initial import
[vuplus_webkit] / Source / JavaScriptCore / tests / mozilla / js1_5 / GetSet / getset-004.js
1 /*
2 * The contents of this file are subject to the Netscape Public
3 * License Version 1.1 (the "License"); you may not use this file
4 * except in compliance with the License. You may obtain a copy of
5 * the License at http://www.mozilla.org/NPL/
6 *
7 * Software distributed under the License is distributed on an "AS IS" 
8 * basis, WITHOUT WARRANTY OF ANY KIND, either expressed
9 * or implied. See the License for the specific language governing
10 * rights and limitations under the License.
11 *
12 * The Original Code is mozilla.org code.
13 *
14 * The Initial Developer of the Original Code is Netscape
15 * Communications Corporation.  Portions created by Netscape are
16 * Copyright (C) 1998 Netscape Communications Corporation. All
17 * Rights Reserved.
18 *
19 * Contributor(s): pschwartau@netscape.com
20 * Date: 14 April 2001
21 *
22 * SUMMARY: Testing  obj.__defineSetter__(), obj.__defineGetter__()
23 * Note: this is a non-ECMA language extension
24 */
25 //-------------------------------------------------------------------------------------------------
26 var UBound = 0;
27 var bug = '(none)';
28 var summary = 'Testing  obj.__defineSetter__(), obj.__defineGetter__()';
29 var statprefix = 'Status: ';
30 var status = '';
31 var statusitems = [ ];
32 var actual = '';
33 var actualvalues = [ ];
34 var expect= '';
35 var expectedvalues = [ ];
36 var cnDEFAULT = 'default name';
37 var cnFRED = 'Fred';
38 var obj = {};
39 var obj2 = {};
40 var s = '';
41
42
43 // SECTION1: define getter/setter directly on an object (not its prototype)
44 obj = new Object();
45 obj.nameSETS = 0;
46 obj.nameGETS = 0;
47 obj.__defineSetter__('name', function(newValue) {this._name=newValue; this.nameSETS++;});
48 obj.__defineGetter__('name', function() {this.nameGETS++; return this._name;});
49
50 status = 'In SECTION1 of test after 0 sets, 0 gets';
51 actual = [obj.nameSETS,obj.nameGETS];
52 expect = [0,0];
53 addThis();
54
55 s = obj.name;
56 status = 'In SECTION1 of test after 0 sets, 1 get';
57 actual = [obj.nameSETS,obj.nameGETS];
58 expect = [0,1];
59 addThis();
60
61 obj.name = cnFRED;
62 status = 'In SECTION1 of test after 1 set, 1 get';
63 actual = [obj.nameSETS,obj.nameGETS];
64 expect = [1,1];
65 addThis();
66
67 obj.name = obj.name;
68 status = 'In SECTION1 of test after 2 sets, 2 gets';
69 actual = [obj.nameSETS,obj.nameGETS];
70 expect = [2,2];
71 addThis();
72
73
74 // SECTION2: define getter/setter in Object.prototype
75 Object.prototype.nameSETS = 0;
76 Object.prototype.nameGETS = 0;
77 Object.prototype.__defineSetter__('name', function(newValue) {this._name=newValue; this.nameSETS++;});
78 Object.prototype.__defineGetter__('name', function() {this.nameGETS++; return this._name;});
79
80 obj = new Object();
81 status = 'In SECTION2 of test after 0 sets, 0 gets';
82 actual = [obj.nameSETS,obj.nameGETS];
83 expect = [0,0];
84 addThis();
85
86 s = obj.name;
87 status = 'In SECTION2 of test after 0 sets, 1 get';
88 actual = [obj.nameSETS,obj.nameGETS];
89 expect = [0,1];
90 addThis();
91
92 obj.name = cnFRED;
93 status = 'In SECTION2 of test after 1 set, 1 get';
94 actual = [obj.nameSETS,obj.nameGETS];
95 expect = [1,1];
96 addThis();
97
98 obj.name = obj.name;
99 status = 'In SECTION2 of test after 2 sets, 2 gets';
100 actual = [obj.nameSETS,obj.nameGETS];
101 expect = [2,2];
102 addThis();
103
104
105 // SECTION 3: define getter/setter in prototype of user-defined constructor
106 function TestObject()
107 {
108 }
109 TestObject.prototype.nameSETS = 0;
110 TestObject.prototype.nameGETS = 0;
111 TestObject.prototype.__defineSetter__('name', function(newValue) {this._name=newValue; this.nameSETS++;});
112 TestObject.prototype.__defineGetter__('name', function() {this.nameGETS++; return this._name;});
113 TestObject.prototype.name = cnDEFAULT;
114
115 obj = new TestObject();
116 status = 'In SECTION3 of test after 1 set, 0 gets'; // (we set a default value in the prototype)
117 actual = [obj.nameSETS,obj.nameGETS];
118 expect = [1,0];
119 addThis();
120
121 s = obj.name;
122 status = 'In SECTION3 of test after 1 set, 1 get';
123 actual = [obj.nameSETS,obj.nameGETS];
124 expect = [1,1];
125 addThis();
126
127 obj.name = cnFRED;
128 status = 'In SECTION3 of test after 2 sets, 1 get';
129 actual = [obj.nameSETS,obj.nameGETS];
130 expect = [2,1];
131 addThis();
132
133 obj.name = obj.name;
134 status = 'In SECTION3 of test after 3 sets, 2 gets';
135 actual = [obj.nameSETS,obj.nameGETS];
136 expect = [3,2];
137 addThis();
138
139 obj2 = new TestObject();
140 status = 'obj2 = new TestObject() after 1 set, 0 gets';
141 actual = [obj2.nameSETS,obj2.nameGETS];
142 expect = [1,0]; // we set a default value in the prototype -
143 addThis();
144
145 // Use both obj and obj2  -
146 obj2.name = obj.name +  obj2.name;
147   status = 'obj2 = new TestObject() after 2 sets, 1 get';
148   actual = [obj2.nameSETS,obj2.nameGETS];
149   expect = [2,1];
150   addThis();
151
152   status = 'In SECTION3 of test after 3 sets, 3 gets';
153   actual = [obj.nameSETS,obj.nameGETS];
154   expect = [3,3];  // we left off at [3,2] above -
155   addThis();
156
157
158 //---------------------------------------------------------------------------------
159 test();
160 //---------------------------------------------------------------------------------
161
162
163 function addThis()
164 {
165   statusitems[UBound] = status;
166   actualvalues[UBound] = actual.toString();
167   expectedvalues[UBound] = expect.toString();
168   UBound++;
169 }
170
171
172 function test()
173 {
174   enterFunc ('test');
175   printBugNumber (bug);
176   printStatus (summary);
177  
178   for (var i = 0; i < UBound; i++)
179   {
180     reportCompare(expectedvalues[i], actualvalues[i], getStatus(i));
181   }
182
183   exitFunc ('test');
184 }
185
186
187 function getStatus(i)
188 {
189   return statprefix + statusitems[i];
190 }