js/src/tests/js1_5/extensions/getset-004.js

branch
TOR_BUG_9701
changeset 15
b8a032363ba2
equal deleted inserted replaced
-1:000000000000 0:17b75b066b05
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 //-----------------------------------------------------------------------------
13 var UBound = 0;
14 var BUGNUMBER = '(none)';
15 var summary = 'Testing obj.__defineSetter__(), obj.__defineGetter__()';
16 var statprefix = 'Status: ';
17 var status = '';
18 var statusitems = [ ];
19 var actual = '';
20 var actualvalues = [ ];
21 var expect= '';
22 var expectedvalues = [ ];
23 var cnDEFAULT = 'default name';
24 var cnFRED = 'Fred';
25 var obj = {};
26 var obj2 = {};
27 var s = '';
28
29
30 // SECTION1: define getter/setter directly on an object (not its prototype)
31 obj = new Object();
32 obj.nameSETS = 0;
33 obj.nameGETS = 0;
34 obj.__defineSetter__('name', function(newValue) {this._name=newValue; this.nameSETS++;});
35 obj.__defineGetter__('name', function() {this.nameGETS++; return this._name;});
36
37 status = 'In SECTION1 of test after 0 sets, 0 gets';
38 actual = [obj.nameSETS,obj.nameGETS];
39 expect = [0,0];
40 addThis();
41
42 s = obj.name;
43 status = 'In SECTION1 of test after 0 sets, 1 get';
44 actual = [obj.nameSETS,obj.nameGETS];
45 expect = [0,1];
46 addThis();
47
48 obj.name = cnFRED;
49 status = 'In SECTION1 of test after 1 set, 1 get';
50 actual = [obj.nameSETS,obj.nameGETS];
51 expect = [1,1];
52 addThis();
53
54 obj.name = obj.name;
55 status = 'In SECTION1 of test after 2 sets, 2 gets';
56 actual = [obj.nameSETS,obj.nameGETS];
57 expect = [2,2];
58 addThis();
59
60
61 // SECTION2: define getter/setter in Object.prototype
62 Object.prototype.nameSETS = 0;
63 Object.prototype.nameGETS = 0;
64 Object.prototype.__defineSetter__('name', function(newValue) {this._name=newValue; this.nameSETS++;});
65 Object.prototype.__defineGetter__('name', function() {this.nameGETS++; return this._name;});
66
67 obj = new Object();
68 status = 'In SECTION2 of test after 0 sets, 0 gets';
69 actual = [obj.nameSETS,obj.nameGETS];
70 expect = [0,0];
71 addThis();
72
73 s = obj.name;
74 status = 'In SECTION2 of test after 0 sets, 1 get';
75 actual = [obj.nameSETS,obj.nameGETS];
76 expect = [0,1];
77 addThis();
78
79 obj.name = cnFRED;
80 status = 'In SECTION2 of test after 1 set, 1 get';
81 actual = [obj.nameSETS,obj.nameGETS];
82 expect = [1,1];
83 addThis();
84
85 obj.name = obj.name;
86 status = 'In SECTION2 of test after 2 sets, 2 gets';
87 actual = [obj.nameSETS,obj.nameGETS];
88 expect = [2,2];
89 addThis();
90
91
92 // SECTION 3: define getter/setter in prototype of user-defined constructor
93 function TestObject()
94 {
95 }
96 TestObject.prototype.nameSETS = 0;
97 TestObject.prototype.nameGETS = 0;
98 TestObject.prototype.__defineSetter__('name', function(newValue) {this._name=newValue; this.nameSETS++;});
99 TestObject.prototype.__defineGetter__('name', function() {this.nameGETS++; return this._name;});
100 TestObject.prototype.name = cnDEFAULT;
101
102 obj = new TestObject();
103 status = 'In SECTION3 of test after 1 set, 0 gets'; // (we set a default value in the prototype)
104 actual = [obj.nameSETS,obj.nameGETS];
105 expect = [1,0];
106 addThis();
107
108 s = obj.name;
109 status = 'In SECTION3 of test after 1 set, 1 get';
110 actual = [obj.nameSETS,obj.nameGETS];
111 expect = [1,1];
112 addThis();
113
114 obj.name = cnFRED;
115 status = 'In SECTION3 of test after 2 sets, 1 get';
116 actual = [obj.nameSETS,obj.nameGETS];
117 expect = [2,1];
118 addThis();
119
120 obj.name = obj.name;
121 status = 'In SECTION3 of test after 3 sets, 2 gets';
122 actual = [obj.nameSETS,obj.nameGETS];
123 expect = [3,2];
124 addThis();
125
126 obj2 = new TestObject();
127 status = 'obj2 = new TestObject() after 1 set, 0 gets';
128 actual = [obj2.nameSETS,obj2.nameGETS];
129 expect = [1,0]; // we set a default value in the prototype -
130 addThis();
131
132 // Use both obj and obj2 -
133 obj2.name = obj.name + obj2.name;
134 status = 'obj2 = new TestObject() after 2 sets, 1 get';
135 actual = [obj2.nameSETS,obj2.nameGETS];
136 expect = [2,1];
137 addThis();
138
139 status = 'In SECTION3 of test after 3 sets, 3 gets';
140 actual = [obj.nameSETS,obj.nameGETS];
141 expect = [3,3]; // we left off at [3,2] above -
142 addThis();
143
144
145 //---------------------------------------------------------------------------------
146 test();
147 //---------------------------------------------------------------------------------
148
149
150 function addThis()
151 {
152 statusitems[UBound] = status;
153 actualvalues[UBound] = actual.toString();
154 expectedvalues[UBound] = expect.toString();
155 UBound++;
156 }
157
158
159 function test()
160 {
161 enterFunc ('test');
162 printBugNumber(BUGNUMBER);
163 printStatus (summary);
164
165 for (var i = 0; i < UBound; i++)
166 {
167 reportCompare(expectedvalues[i], actualvalues[i], getStatus(i));
168 }
169
170 exitFunc ('test');
171 }
172
173
174 function getStatus(i)
175 {
176 return statprefix + statusitems[i];
177 }

mercurial