Debian Codegraph @

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