Debian Codegraph @

rhino/testsrc/tests/js1_5/extensions/getset-005.js

raw symbols SCIP
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6 /*
7 * Date: 14 April 2001
8 *
9 * SUMMARY: Testing obj.__defineSetter__(), obj.__defineGetter__()
10 * Note: this is a non-ECMA language extension
11 *
12 * This test is the same as getset-004.js, except that here we
13 * store the getter/setter functions in global variables.
14 */
15 //-----------------------------------------------------------------------------
16 var gTestfile = 'getset-005.js';
17 var UBound = 0;
18 var BUGNUMBER = '(none)';
19 var summary = 'Testing obj.__defineSetter__(), obj.__defineGetter__()';
20 var statprefix = 'Status: ';
21 var status = '';
22 var statusitems = [ ];
23 var actual = '';
24 var actualvalues = [ ];
25 var expect= '';
26 var expectedvalues = [ ];
27 var cnName = 'name';
28 var cnDEFAULT = 'default name';
29 var cnFRED = 'Fred';
30 var obj = {};
31 var obj2 = {};
32 var s = '';
33
34
35 // The getter/setter functions we'll use in all three sections below -
36 var cnNameSetter = function(newValue) {this._name=newValue; this.nameSETS++;};
37 var cnNameGetter = function() {this.nameGETS++; return this._name;};
38
39
40 // SECTION1: define getter/setter directly on an object (not its prototype)
41 obj = new Object();
42 obj.nameSETS = 0;
43 obj.nameGETS = 0;
44 obj.__defineSetter__(cnName, cnNameSetter);
45 obj.__defineGetter__(cnName, cnNameGetter);
46
47 status = 'In SECTION1 of test after 0 sets, 0 gets';
48 actual = [obj.nameSETS,obj.nameGETS];
49 expect = [0,0];
50 addThis();
51
52 s = obj.name;
53 status = 'In SECTION1 of test after 0 sets, 1 get';
54 actual = [obj.nameSETS,obj.nameGETS];
55 expect = [0,1];
56 addThis();
57
58 obj.name = cnFRED;
59 status = 'In SECTION1 of test after 1 set, 1 get';
60 actual = [obj.nameSETS,obj.nameGETS];
61 expect = [1,1];
62 addThis();
63
64 obj.name = obj.name;
65 status = 'In SECTION1 of test after 2 sets, 2 gets';
66 actual = [obj.nameSETS,obj.nameGETS];
67 expect = [2,2];
68 addThis();
69
70
71 // SECTION2: define getter/setter in Object.prototype
72 Object.prototype.nameSETS = 0;
73 Object.prototype.nameGETS = 0;
74 Object.prototype.__defineSetter__(cnName, cnNameSetter);
75 Object.prototype.__defineGetter__(cnName, cnNameGetter);
76
77 obj = new Object();
78 status = 'In SECTION2 of test after 0 sets, 0 gets';
79 actual = [obj.nameSETS,obj.nameGETS];
80 expect = [0,0];
81 addThis();
82
83 s = obj.name;
84 status = 'In SECTION2 of test after 0 sets, 1 get';
85 actual = [obj.nameSETS,obj.nameGETS];
86 expect = [0,1];
87 addThis();
88
89 obj.name = cnFRED;
90 status = 'In SECTION2 of test after 1 set, 1 get';
91 actual = [obj.nameSETS,obj.nameGETS];
92 expect = [1,1];
93 addThis();
94
95 obj.name = obj.name;
96 status = 'In SECTION2 of test after 2 sets, 2 gets';
97 actual = [obj.nameSETS,obj.nameGETS];
98 expect = [2,2];
99 addThis();
100
101
102 // SECTION 3: define getter/setter in prototype of user-defined constructor
103 function TestObject()
104 {
105 }
106 TestObject.prototype.nameSETS = 0;
107 TestObject.prototype.nameGETS = 0;
108 TestObject.prototype.__defineSetter__(cnName, cnNameSetter);
109 TestObject.prototype.__defineGetter__(cnName, cnNameGetter);
110 TestObject.prototype.name = cnDEFAULT;
111
112 obj = new TestObject();
113 status = 'In SECTION3 of test after 1 set, 0 gets'; // (we set a default value in the prototype)
114 actual = [obj.nameSETS,obj.nameGETS];
115 expect = [1,0];
116 addThis();
117
118 s = obj.name;
119 status = 'In SECTION3 of test after 1 set, 1 get';
120 actual = [obj.nameSETS,obj.nameGETS];
121 expect = [1,1];
122 addThis();
123
124 obj.name = cnFRED;
125 status = 'In SECTION3 of test after 2 sets, 1 get';
126 actual = [obj.nameSETS,obj.nameGETS];
127 expect = [2,1];
128 addThis();
129
130 obj.name = obj.name;
131 status = 'In SECTION3 of test after 3 sets, 2 gets';
132 actual = [obj.nameSETS,obj.nameGETS];
133 expect = [3,2];
134 addThis();
135
136 obj2 = new TestObject();
137 status = 'obj2 = new TestObject() after 1 set, 0 gets';
138 actual = [obj2.nameSETS,obj2.nameGETS];
139 expect = [1,0]; // we set a default value in the prototype -
140 addThis();
141
142 // Use both obj and obj2 -
143 obj2.name = obj.name + obj2.name;
144 status = 'obj2 = new TestObject() after 2 sets, 1 get';
145 actual = [obj2.nameSETS,obj2.nameGETS];
146 expect = [2,1];
147 addThis();
148
149 status = 'In SECTION3 of test after 3 sets, 3 gets';
150 actual = [obj.nameSETS,obj.nameGETS];
151 expect = [3,3]; // we left off at [3,2] above -
152 addThis();
153
154
155 //---------------------------------------------------------------------------------
156 test();
157 //---------------------------------------------------------------------------------
158
159
160 function addThis()
161 {
162 statusitems[UBound] = status;
163 actualvalues[UBound] = actual.toString();
164 expectedvalues[UBound] = expect.toString();
165 UBound++;
166 }
167
168
169 function test()
170 {
171 enterFunc ('test');
172 printBugNumber(BUGNUMBER);
173 printStatus (summary);
174
175 for (var i = 0; i < UBound; i++)
176 {
177 reportCompare(expectedvalues[i], actualvalues[i], getStatus(i));
178 }
179
180 exitFunc ('test');
181 }
182
183
184 function getStatus(i)
185 {
186 return statprefix + statusitems[i];
187 }