]>
git.saurik.com Git - apple/javascriptcore.git/blob - tests/mozilla/js1_5/GetSet/getset-005.js
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/
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.
12 * The Original Code is mozilla.org code.
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
19 * Contributor(s): pschwartau@netscape.com
22 * SUMMARY: Testing obj.__defineSetter__(), obj.__defineGetter__()
23 * Note: this is a non-ECMA language extension
25 * This test is the same as getset-004.js, except that here we
26 * store the getter/setter functions in global variables.
28 //-------------------------------------------------------------------------------------------------
31 var summary
= 'Testing obj.__defineSetter__(), obj.__defineGetter__()';
32 var statprefix
= 'Status: ';
34 var statusitems
= [ ];
36 var actualvalues
= [ ];
38 var expectedvalues
= [ ];
40 var cnDEFAULT
= 'default name';
47 // The getter/setter functions we'll use in all three sections below -
48 var cnNameSetter = function(newValue
) {this._name
=newValue
; this.nameSETS
++;};
49 var cnNameGetter = function() {this.nameGETS
++; return this._name
;};
52 // SECTION1: define getter/setter directly on an object (not its prototype)
56 obj
.__defineSetter__(cnName
, cnNameSetter
);
57 obj
.__defineGetter__(cnName
, cnNameGetter
);
59 status
= 'In SECTION1 of test after 0 sets, 0 gets';
60 actual
= [obj
.nameSETS
,obj
.nameGETS
];
65 status
= 'In SECTION1 of test after 0 sets, 1 get';
66 actual
= [obj
.nameSETS
,obj
.nameGETS
];
71 status
= 'In SECTION1 of test after 1 set, 1 get';
72 actual
= [obj
.nameSETS
,obj
.nameGETS
];
77 status
= 'In SECTION1 of test after 2 sets, 2 gets';
78 actual
= [obj
.nameSETS
,obj
.nameGETS
];
83 // SECTION2: define getter/setter in Object.prototype
84 Object
.prototype.nameSETS
= 0;
85 Object
.prototype.nameGETS
= 0;
86 Object
.prototype.__defineSetter__(cnName
, cnNameSetter
);
87 Object
.prototype.__defineGetter__(cnName
, cnNameGetter
);
90 status
= 'In SECTION2 of test after 0 sets, 0 gets';
91 actual
= [obj
.nameSETS
,obj
.nameGETS
];
96 status
= 'In SECTION2 of test after 0 sets, 1 get';
97 actual
= [obj
.nameSETS
,obj
.nameGETS
];
102 status
= 'In SECTION2 of test after 1 set, 1 get';
103 actual
= [obj
.nameSETS
,obj
.nameGETS
];
108 status
= 'In SECTION2 of test after 2 sets, 2 gets';
109 actual
= [obj
.nameSETS
,obj
.nameGETS
];
114 // SECTION 3: define getter/setter in prototype of user-defined constructor
115 function TestObject()
118 TestObject
.prototype.nameSETS
= 0;
119 TestObject
.prototype.nameGETS
= 0;
120 TestObject
.prototype.__defineSetter__(cnName
, cnNameSetter
);
121 TestObject
.prototype.__defineGetter__(cnName
, cnNameGetter
);
122 TestObject
.prototype.name
= cnDEFAULT
;
124 obj
= new TestObject();
125 status
= 'In SECTION3 of test after 1 set, 0 gets'; // (we set a default value in the prototype)
126 actual
= [obj
.nameSETS
,obj
.nameGETS
];
131 status
= 'In SECTION3 of test after 1 set, 1 get';
132 actual
= [obj
.nameSETS
,obj
.nameGETS
];
137 status
= 'In SECTION3 of test after 2 sets, 1 get';
138 actual
= [obj
.nameSETS
,obj
.nameGETS
];
143 status
= 'In SECTION3 of test after 3 sets, 2 gets';
144 actual
= [obj
.nameSETS
,obj
.nameGETS
];
148 obj2
= new TestObject();
149 status
= 'obj2 = new TestObject() after 1 set, 0 gets';
150 actual
= [obj2
.nameSETS
,obj2
.nameGETS
];
151 expect
= [1,0]; // we set a default value in the prototype -
154 // Use both obj and obj2 -
155 obj2
.name
= obj
.name
+ obj2
.name
;
156 status
= 'obj2 = new TestObject() after 2 sets, 1 get';
157 actual
= [obj2
.nameSETS
,obj2
.nameGETS
];
161 status
= 'In SECTION3 of test after 3 sets, 3 gets';
162 actual
= [obj
.nameSETS
,obj
.nameGETS
];
163 expect
= [3,3]; // we left off at [3,2] above -
167 //---------------------------------------------------------------------------------
169 //---------------------------------------------------------------------------------
174 statusitems
[UBound
] = status
;
175 actualvalues
[UBound
] = actual
.toString();
176 expectedvalues
[UBound
] = expect
.toString();
184 printBugNumber (bug
);
185 printStatus (summary
);
187 for (var i
= 0; i
< UBound
; i
++)
189 reportCompare(expectedvalues
[i
], actualvalues
[i
], getStatus(i
));
196 function getStatus(i
)
198 return statprefix
+ statusitems
[i
];