]>
git.saurik.com Git - apple/javascriptcore.git/blob - API/tests/testapi.js
2 * Copyright (C) 2006 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 function bludgeonArguments() { if (0) arguments
; return function g() {} }
27 h
= bludgeonArguments();
33 print("PASS: " + msg
, "green");
38 print("FAIL: " + msg
, "red");
42 function shouldBe(a
, b
)
51 if (evalA
== b
|| isNaN(evalA
) && typeof evalA
== 'number' && isNaN(b
) && typeof b
== 'number')
52 pass(a
+ " should be " + b
+ " and is.");
54 fail(a
+ " should be " + b
+ " but instead is " + evalA
+ ".");
57 function shouldThrow(a
)
63 pass(a
+ " threw: " + e
);
67 fail(a
+ " did not throw an exception.");
70 function globalStaticFunction()
75 shouldBe("globalStaticValue", 3);
76 shouldBe("globalStaticFunction()", 4);
77 shouldBe("this.globalStaticFunction()", 4);
79 function globalStaticFunction2() {
82 shouldBe("globalStaticFunction2();", 10);
83 this.globalStaticFunction2 = function() { return 20; }
84 shouldBe("globalStaticFunction2();", 20);
85 shouldBe("this.globalStaticFunction2();", 20);
87 function iAmNotAStaticFunction() { return 10; }
88 shouldBe("iAmNotAStaticFunction();", 10);
89 this.iAmNotAStaticFunction = function() { return 20; }
90 shouldBe("iAmNotAStaticFunction();", 20);
92 shouldBe("typeof MyObject", "function"); // our object implements 'call'
93 MyObject
.cantFind
= 1;
94 shouldBe("MyObject.cantFind", undefined);
95 MyObject
.regularType
= 1;
96 shouldBe("MyObject.regularType", 1);
97 MyObject
.alwaysOne
= 2;
98 shouldBe("MyObject.alwaysOne", 1);
99 MyObject
.cantDelete
= 1;
100 delete MyObject
.cantDelete
;
101 shouldBe("MyObject.cantDelete", 1);
102 shouldBe("delete MyObject.throwOnDelete", "an exception");
103 MyObject
.cantSet
= 1;
104 shouldBe("MyObject.cantSet", undefined);
105 shouldBe("MyObject.throwOnGet", "an exception");
106 shouldBe("MyObject.throwOnSet = 5", "an exception");
107 shouldBe("MyObject('throwOnCall')", "an exception");
108 shouldBe("new MyObject('throwOnConstruct')", "an exception");
109 shouldBe("'throwOnHasInstance' instanceof MyObject", "an exception");
111 MyObject
.nullGetForwardSet
= 1;
112 shouldBe("MyObject.nullGetForwardSet", 1);
114 var foundMyPropertyName
= false;
115 var foundRegularType
= false;
116 for (var p
in MyObject
) {
117 if (p
== "myPropertyName")
118 foundMyPropertyName
= true;
119 if (p
== "regularType")
120 foundRegularType
= true;
123 if (foundMyPropertyName
)
124 pass("MyObject.myPropertyName was enumerated");
126 fail("MyObject.myPropertyName was not enumerated");
128 if (foundRegularType
)
129 pass("MyObject.regularType was enumerated");
131 fail("MyObject.regularType was not enumerated");
133 var alwaysOneDescriptor
= Object
.getOwnPropertyDescriptor(MyObject
, "alwaysOne");
134 shouldBe('typeof alwaysOneDescriptor', "object");
135 shouldBe('alwaysOneDescriptor.value', MyObject
.alwaysOne
);
136 shouldBe('alwaysOneDescriptor.configurable', true);
137 shouldBe('alwaysOneDescriptor.enumerable', false); // Actually it is.
138 var cantFindDescriptor
= Object
.getOwnPropertyDescriptor(MyObject
, "cantFind");
139 shouldBe('typeof cantFindDescriptor', "object");
140 shouldBe('cantFindDescriptor.value', MyObject
.cantFind
);
141 shouldBe('cantFindDescriptor.configurable', true);
142 shouldBe('cantFindDescriptor.enumerable', false);
144 // If getOwnPropertyDescriptor() returned an access descriptor, this wouldn't throw.
145 Object
.getOwnPropertyDescriptor(MyObject
, "throwOnGet");
147 pass("getting property descriptor of throwOnGet threw exception");
149 var myPropertyNameDescriptor
= Object
.getOwnPropertyDescriptor(MyObject
, "myPropertyName");
150 shouldBe('typeof myPropertyNameDescriptor', "object");
151 shouldBe('myPropertyNameDescriptor.value', MyObject
.myPropertyName
);
152 shouldBe('myPropertyNameDescriptor.configurable', true);
153 shouldBe('myPropertyNameDescriptor.enumerable', false); // Actually it is.
155 // if getOwnPropertyDescriptor() returned an access descriptor, this wouldn't throw.
156 Object
.getOwnPropertyDescriptor(MyObject
, "hasPropertyLie");
158 pass("getting property descriptor of hasPropertyLie threw exception");
160 shouldBe('Object.getOwnPropertyDescriptor(MyObject, "doesNotExist")', undefined);
162 myObject
= new MyObject();
164 shouldBe("delete MyObject.regularType", true);
165 shouldBe("MyObject.regularType", undefined);
166 shouldBe("MyObject(0)", 1);
167 shouldBe("MyObject()", undefined);
168 shouldBe("typeof myObject", "object");
169 shouldBe("MyObject ? 1 : 0", true); // toBoolean
170 shouldBe("+MyObject", 1); // toNumber
171 shouldBe("(Object.prototype.toString.call(MyObject))", "[object MyObject]"); // Object.prototype.toString
172 shouldBe("(MyObject.toString())", "[object MyObject]"); // toString
173 shouldBe("String(MyObject)", "MyObjectAsString"); // toString
174 shouldBe("MyObject - 0", 1); // toNumber
175 shouldBe("MyObject.valueOf()", 1); // valueOf
177 shouldBe("typeof MyConstructor", "object");
178 constructedObject
= new MyConstructor(1);
179 shouldBe("typeof constructedObject", "object");
180 shouldBe("constructedObject.value", 1);
181 shouldBe("myObject instanceof MyObject", true);
182 shouldBe("(new Object()) instanceof MyObject", false);
184 shouldThrow("new MyBadConstructor()");
186 MyObject
.nullGetSet
= 1;
187 shouldBe("MyObject.nullGetSet", 1);
188 shouldThrow("MyObject.nullCall()");
189 shouldThrow("MyObject.hasPropertyLie");
191 derived
= new Derived();
193 shouldBe("derived instanceof Derived", true);
194 shouldBe("derived instanceof Base", true);
196 // base properties and functions return 1 when called/gotten; derived, 2
197 shouldBe("derived.baseProtoDup()", 2);
198 shouldBe("derived.baseProto()", 1);
199 shouldBe("derived.baseDup", 2);
200 shouldBe("derived.baseOnly", 1);
201 shouldBe("derived.protoOnly()", 2);
202 shouldBe("derived.protoDup", 2);
203 shouldBe("derived.derivedOnly", 2)
205 shouldBe("derived.baseHardNull()", undefined)
207 // base properties throw 1 when set; derived, 2
208 shouldBe("derived.baseDup = 0", 2);
209 shouldBe("derived.baseOnly = 0", 1);
210 shouldBe("derived.derivedOnly = 0", 2)
211 shouldBe("derived.protoDup = 0", 2);
213 derived2
= new Derived2();
215 shouldBe("derived2 instanceof Derived2", true);
216 shouldBe("derived2 instanceof Derived", true);
217 shouldBe("derived2 instanceof Base", true);
219 // base properties and functions return 1 when called/gotten; derived, 2
220 shouldBe("derived2.baseProtoDup()", 2);
221 shouldBe("derived2.baseProto()", 1);
222 shouldBe("derived2.baseDup", 2);
223 shouldBe("derived2.baseOnly", 1);
224 shouldBe("derived2.protoOnly()", 2);
225 shouldBe("derived2.protoDup", 2);
226 shouldBe("derived2.derivedOnly", 2)
228 // base properties throw 1 when set; derived, 2
229 shouldBe("derived2.baseDup = 0", 2);
230 shouldBe("derived2.baseOnly = 0", 1);
231 shouldBe("derived2.derivedOnly = 0", 2)
232 shouldBe("derived2.protoDup = 0", 2);
234 shouldBe('Object.getOwnPropertyDescriptor(derived, "baseProto")', undefined);
235 shouldBe('Object.getOwnPropertyDescriptor(derived, "baseProtoDup")', undefined);
236 var baseDupDescriptor
= Object
.getOwnPropertyDescriptor(derived
, "baseDup");
237 shouldBe('typeof baseDupDescriptor', "object");
238 shouldBe('baseDupDescriptor.value', derived
.baseDup
);
239 shouldBe('baseDupDescriptor.configurable', true);
240 shouldBe('baseDupDescriptor.enumerable', false);
241 var baseOnlyDescriptor
= Object
.getOwnPropertyDescriptor(derived
, "baseOnly");
242 shouldBe('typeof baseOnlyDescriptor', "object");
243 shouldBe('baseOnlyDescriptor.value', derived
.baseOnly
);
244 shouldBe('baseOnlyDescriptor.configurable', true);
245 shouldBe('baseOnlyDescriptor.enumerable', false);
246 shouldBe('Object.getOwnPropertyDescriptor(derived, "protoOnly")', undefined);
247 var protoDupDescriptor
= Object
.getOwnPropertyDescriptor(derived
, "protoDup");
248 shouldBe('typeof protoDupDescriptor', "object");
249 shouldBe('protoDupDescriptor.value', derived
.protoDup
);
250 shouldBe('protoDupDescriptor.configurable', true);
251 shouldBe('protoDupDescriptor.enumerable', false);
252 var derivedOnlyDescriptor
= Object
.getOwnPropertyDescriptor(derived
, "derivedOnly");
253 shouldBe('typeof derivedOnlyDescriptor', "object");
254 shouldBe('derivedOnlyDescriptor.value', derived
.derivedOnly
);
255 shouldBe('derivedOnlyDescriptor.configurable', true);
256 shouldBe('derivedOnlyDescriptor.enumerable', false);
258 shouldBe("undefined instanceof MyObject", false);
259 EvilExceptionObject
.hasInstance
= function f() { return f(); };
260 EvilExceptionObject
.__proto__
= undefined;
261 shouldThrow("undefined instanceof EvilExceptionObject");
262 EvilExceptionObject
.hasInstance = function () { return true; };
263 shouldBe("undefined instanceof EvilExceptionObject", true);
265 EvilExceptionObject
.toNumber
= function f() { return f(); }
266 shouldThrow("EvilExceptionObject*5");
267 EvilExceptionObject
.toStringExplicit
= function f() { return f(); }
268 shouldThrow("String(EvilExceptionObject)");
270 shouldBe("console", "[object Console]");
271 shouldBe("typeof console.log", "function");
273 shouldBe("EmptyObject", "[object CallbackObject]");
275 for (var i
= 0; i
< 6; ++i
)
276 PropertyCatchalls
.x
= i
;
277 shouldBe("PropertyCatchalls.x", 4);
279 for (var i
= 0; i
< 6; ++i
)
280 var x
= PropertyCatchalls
.x
;
282 var make_throw
= 'make_throw';
283 shouldThrow("PropertyCatchalls[make_throw]=1");
285 shouldThrow("PropertyCatchalls[make_throw]=1");
287 for (var i
= 0; i
< 10; ++i
) {
288 for (var p
in PropertyCatchalls
) {
291 shouldBe("p", i
% 10);
296 PropertyCatchalls
.__proto__
= { y: 1 };
297 for (var i
= 0; i
< 6; ++i
)
298 var y
= PropertyCatchalls
.y
;
301 var o
= { __proto__: PropertyCatchalls
};
302 for (var i
= 0; i
< 6; ++i
)
303 var z
= PropertyCatchalls
.z
;
307 throw "Some tests failed";