]> git.saurik.com Git - apple/javascriptcore.git/blob - API/tests/testapi.js
82756b57c6e3cc1353adcc0c31fa5e8b2476f05b
[apple/javascriptcore.git] / API / tests / testapi.js
1 /*
2 * Copyright (C) 2006 Apple Computer, Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
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.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, 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 COMPUTER, 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.
24 */
25
26 function bludgeonArguments() { if (0) arguments; return function g() {} }
27 h = bludgeonArguments();
28 gc();
29
30 var failed = false;
31 function pass(msg)
32 {
33 print("PASS: " + msg, "green");
34 }
35
36 function fail(msg)
37 {
38 print("FAIL: " + msg, "red");
39 failed = true;
40 }
41
42 function shouldBe(a, b)
43 {
44 var evalA;
45 try {
46 evalA = eval(a);
47 } catch(e) {
48 evalA = e;
49 }
50
51 if (evalA == b || isNaN(evalA) && typeof evalA == 'number' && isNaN(b) && typeof b == 'number')
52 pass(a + " should be " + b + " and is.");
53 else
54 fail(a + " should be " + b + " but instead is " + evalA + ".");
55 }
56
57 function shouldThrow(a)
58 {
59 var evalA;
60 try {
61 eval(a);
62 } catch(e) {
63 pass(a + " threw: " + e);
64 return;
65 }
66
67 fail(a + " did not throw an exception.");
68 }
69
70 function globalStaticFunction()
71 {
72 return 4;
73 }
74
75 shouldBe("globalStaticValue", 3);
76 shouldBe("globalStaticFunction()", 4);
77
78 shouldBe("typeof MyObject", "function"); // our object implements 'call'
79 MyObject.cantFind = 1;
80 shouldBe("MyObject.cantFind", undefined);
81 MyObject.regularType = 1;
82 shouldBe("MyObject.regularType", 1);
83 MyObject.alwaysOne = 2;
84 shouldBe("MyObject.alwaysOne", 1);
85 MyObject.cantDelete = 1;
86 delete MyObject.cantDelete;
87 shouldBe("MyObject.cantDelete", 1);
88 shouldBe("delete MyObject.throwOnDelete", "an exception");
89 MyObject.cantSet = 1;
90 shouldBe("MyObject.cantSet", undefined);
91 shouldBe("MyObject.throwOnGet", "an exception");
92 shouldBe("MyObject.throwOnSet = 5", "an exception");
93 shouldBe("MyObject('throwOnCall')", "an exception");
94 shouldBe("new MyObject('throwOnConstruct')", "an exception");
95 shouldBe("'throwOnHasInstance' instanceof MyObject", "an exception");
96
97 var foundMyPropertyName = false;
98 var foundRegularType = false;
99 for (var p in MyObject) {
100 if (p == "myPropertyName")
101 foundMyPropertyName = true;
102 if (p == "regularType")
103 foundRegularType = true;
104 }
105
106 if (foundMyPropertyName)
107 pass("MyObject.myPropertyName was enumerated");
108 else
109 fail("MyObject.myPropertyName was not enumerated");
110
111 if (foundRegularType)
112 pass("MyObject.regularType was enumerated");
113 else
114 fail("MyObject.regularType was not enumerated");
115
116 myObject = new MyObject();
117
118 shouldBe("delete MyObject.regularType", true);
119 shouldBe("MyObject.regularType", undefined);
120 shouldBe("MyObject(0)", 1);
121 shouldBe("MyObject()", undefined);
122 shouldBe("typeof myObject", "object");
123 shouldBe("MyObject ? 1 : 0", true); // toBoolean
124 shouldBe("+MyObject", 1); // toNumber
125 shouldBe("(MyObject.toString())", "[object MyObject]"); // toString
126 shouldBe("String(MyObject)", "MyObjectAsString"); // type conversion to string
127 shouldBe("MyObject - 0", 1); // toNumber
128
129 shouldBe("typeof MyConstructor", "object");
130 constructedObject = new MyConstructor(1);
131 shouldBe("typeof constructedObject", "object");
132 shouldBe("constructedObject.value", 1);
133 shouldBe("myObject instanceof MyObject", true);
134 shouldBe("(new Object()) instanceof MyObject", false);
135
136 shouldThrow("MyObject.nullGetSet = 1");
137 shouldThrow("MyObject.nullGetSet");
138 shouldThrow("MyObject.nullCall()");
139 shouldThrow("MyObject.hasPropertyLie");
140
141 derived = new Derived();
142
143 // base properties and functions return 1 when called/gotten; derived, 2
144 shouldBe("derived.baseProtoDup()", 2);
145 shouldBe("derived.baseProto()", 1);
146 shouldBe("derived.baseDup", 2);
147 shouldBe("derived.baseOnly", 1);
148 shouldBe("derived.protoOnly()", 2);
149 shouldBe("derived.protoDup", 2);
150 shouldBe("derived.derivedOnly", 2)
151
152 // base properties throw 1 when set; derived, 2
153 shouldBe("derived.baseDup = 0", 2);
154 shouldBe("derived.baseOnly = 0", 1);
155 shouldBe("derived.derivedOnly = 0", 2)
156 shouldBe("derived.protoDup = 0", 2);
157
158 shouldBe("undefined instanceof MyObject", false);
159 EvilExceptionObject.hasInstance = function f() { return f(); };
160 EvilExceptionObject.__proto__ = undefined;
161 shouldThrow("undefined instanceof EvilExceptionObject");
162 EvilExceptionObject.hasInstance = function () { return true; };
163 shouldBe("undefined instanceof EvilExceptionObject", true);
164
165 EvilExceptionObject.toNumber = function f() { return f(); }
166 shouldThrow("EvilExceptionObject*5");
167 EvilExceptionObject.toStringExplicit = function f() { return f(); }
168 shouldThrow("String(EvilExceptionObject)");
169
170 shouldBe("EmptyObject", "[object CallbackObject]");
171
172 if (failed)
173 throw "Some tests failed";
174