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/. */
9
* SUMMARY: Testing obj.__defineSetter__(), obj.__defineGetter__()
10
* Note: this is a non-ECMA language extension
12
* This test is the same as getset-004.js, except that here we
13
* store the getter/setter functions in global variables.
15
//-----------------------------------------------------------------------------
17
var BUGNUMBER = '(none)';
18
var summary = 'Testing obj.__defineSetter__(), obj.__defineGetter__()';
19
var statprefix = 'Status: ';
21
var statusitems = [ ];
23
var actualvalues = [ ];
25
var expectedvalues = [ ];
27
var cnDEFAULT = 'default name';
34
// The getter/setter functions we'll use in all three sections below -
35
var cnNameSetter = function(newValue) {this._name=newValue; this.nameSETS++;};
36
var cnNameGetter = function() {this.nameGETS++; return this._name;};
39
// SECTION1: define getter/setter directly on an object (not its prototype)
43
obj.__defineSetter__(cnName, cnNameSetter);
44
obj.__defineGetter__(cnName, cnNameGetter);
46
status = 'In SECTION1 of test after 0 sets, 0 gets';
47
actual = [obj.nameSETS,obj.nameGETS];
52
status = 'In SECTION1 of test after 0 sets, 1 get';
53
actual = [obj.nameSETS,obj.nameGETS];
58
status = 'In SECTION1 of test after 1 set, 1 get';
59
actual = [obj.nameSETS,obj.nameGETS];
64
status = 'In SECTION1 of test after 2 sets, 2 gets';
65
actual = [obj.nameSETS,obj.nameGETS];
70
// SECTION2: define getter/setter in Object.prototype
71
Object.prototype.nameSETS = 0;
72
Object.prototype.nameGETS = 0;
73
Object.prototype.__defineSetter__(cnName, cnNameSetter);
74
Object.prototype.__defineGetter__(cnName, cnNameGetter);
77
status = 'In SECTION2 of test after 0 sets, 0 gets';
78
actual = [obj.nameSETS,obj.nameGETS];
83
status = 'In SECTION2 of test after 0 sets, 1 get';
84
actual = [obj.nameSETS,obj.nameGETS];
89
status = 'In SECTION2 of test after 1 set, 1 get';
90
actual = [obj.nameSETS,obj.nameGETS];
95
status = 'In SECTION2 of test after 2 sets, 2 gets';
96
actual = [obj.nameSETS,obj.nameGETS];
101
// SECTION 3: define getter/setter in prototype of user-defined constructor
102
function TestObject()
105
TestObject.prototype.nameSETS = 0;
106
TestObject.prototype.nameGETS = 0;
107
TestObject.prototype.__defineSetter__(cnName, cnNameSetter);
108
TestObject.prototype.__defineGetter__(cnName, cnNameGetter);
109
TestObject.prototype.name = cnDEFAULT;
111
obj = new TestObject();
112
status = 'In SECTION3 of test after 1 set, 0 gets'; // (we set a default value in the prototype)
113
actual = [obj.nameSETS,obj.nameGETS];
118
status = 'In SECTION3 of test after 1 set, 1 get';
119
actual = [obj.nameSETS,obj.nameGETS];
124
status = 'In SECTION3 of test after 2 sets, 1 get';
125
actual = [obj.nameSETS,obj.nameGETS];
130
status = 'In SECTION3 of test after 3 sets, 2 gets';
131
actual = [obj.nameSETS,obj.nameGETS];
135
obj2 = new TestObject();
136
status = 'obj2 = new TestObject() after 1 set, 0 gets';
137
actual = [obj2.nameSETS,obj2.nameGETS];
138
expect = [1,0]; // we set a default value in the prototype -
141
// Use both obj and obj2 -
142
obj2.name = obj.name + obj2.name;
143
status = 'obj2 = new TestObject() after 2 sets, 1 get';
144
actual = [obj2.nameSETS,obj2.nameGETS];
148
status = 'In SECTION3 of test after 3 sets, 3 gets';
149
actual = [obj.nameSETS,obj.nameGETS];
150
expect = [3,3]; // we left off at [3,2] above -
154
//---------------------------------------------------------------------------------
156
//---------------------------------------------------------------------------------
161
statusitems[UBound] = status;
162
actualvalues[UBound] = actual.toString();
163
expectedvalues[UBound] = expect.toString();
171
printBugNumber(BUGNUMBER);
172
printStatus (summary);
174
for (var i = 0; i < UBound; i++)
176
reportCompare(expectedvalues[i], actualvalues[i], getStatus(i));
183
function getStatus(i)
185
return statprefix + statusitems[i];