]> git.saurik.com Git - apple/javascriptcore.git/blame - API/tests/testapi.js
JavaScriptCore-525.tar.gz
[apple/javascriptcore.git] / API / tests / testapi.js
CommitLineData
b37bf2e1
A
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
26function shouldBe(a, b)
27{
28 var evalA;
29 try {
30 evalA = eval(a);
31 } catch(e) {
32 evalA = e;
33 }
34
35 if (evalA == b || isNaN(evalA) && typeof evalA == 'number' && isNaN(b) && typeof b == 'number')
36 print("PASS: " + a + " should be " + b + " and is.", "green");
37 else
38 print("__FAIL__: " + a + " should be " + b + " but instead is " + evalA + ".", "red");
39}
40
41function shouldThrow(a)
42{
43 var result = "__FAIL__: " + a + " did not throw an exception.";
44
45 var evalA;
46 try {
47 eval(a);
48 } catch(e) {
49 result = "PASS: " + a + " threw: " + e;
50 }
51
52 print(result);
53}
54
55function globalStaticFunction()
56{
57 return 4;
58}
59
60shouldBe("globalStaticValue", 3);
61shouldBe("globalStaticFunction()", 4);
62
63shouldBe("typeof MyObject", "function"); // our object implements 'call'
64MyObject.cantFind = 1;
65shouldBe("MyObject.cantFind", undefined);
66MyObject.regularType = 1;
67shouldBe("MyObject.regularType", 1);
68MyObject.alwaysOne = 2;
69shouldBe("MyObject.alwaysOne", 1);
70MyObject.cantDelete = 1;
71delete MyObject.cantDelete;
72shouldBe("MyObject.cantDelete", 1);
73shouldBe("delete MyObject.throwOnDelete", 2); // deleteProperty -- should throw 2
74MyObject.cantSet = 1;
75shouldBe("MyObject.cantSet", undefined);
76
77var foundMyPropertyName = false;
78var foundRegularType = false;
79for (var p in MyObject) {
80 if (p == "myPropertyName")
81 foundMyPropertyName = true;
82 if (p == "regularType")
83 foundRegularType = true;
84}
85print(foundMyPropertyName
86 ? "PASS: MyObject.myPropertyName was enumerated"
87 : "__FAIL__: MyObject.myPropertyName was not enumerated");
88print(foundRegularType
89 ? "PASS: MyObject.regularType was enumerated"
90 : "__FAIL__: MyObject.regularType was not enumerated");
91
92myObject = new MyObject();
93
94shouldBe("delete MyObject.regularType", true);
95shouldBe("MyObject.regularType", undefined);
96shouldBe("MyObject(0)", 1);
97shouldBe("MyObject()", undefined);
98shouldBe("typeof myObject", "object");
99shouldBe("MyObject ? 1 : 0", true); // toBoolean
100shouldBe("+MyObject", 1); // toNumber
101shouldBe("(MyObject.toString())", "[object MyObject]"); // toString
9dae56ea 102shouldBe("String(MyObject)", "MyObjectAsString"); // type conversion to string
b37bf2e1
A
103shouldBe("MyObject - 0", NaN); // toPrimitive
104
105shouldBe("typeof MyConstructor", "object");
106constructedObject = new MyConstructor(1);
107shouldBe("typeof constructedObject", "object");
108shouldBe("constructedObject.value", 1);
109shouldBe("myObject instanceof MyObject", true);
110shouldBe("(new Object()) instanceof MyObject", false);
111
112shouldThrow("MyObject.nullGetSet = 1");
113shouldThrow("MyObject.nullGetSet");
114shouldThrow("MyObject.nullCall()");
115shouldThrow("MyObject.hasPropertyLie");
116
117derived = new Derived();
118
119// base properties and functions return 1 when called/gotten; derived, 2
120shouldBe("derived.baseProtoDup()", 2);
121shouldBe("derived.baseProto()", 1);
122shouldBe("derived.baseDup", 2);
123shouldBe("derived.baseOnly", 1);
124shouldBe("derived.protoOnly()", 2);
125shouldBe("derived.protoDup", 2);
126shouldBe("derived.derivedOnly", 2)
127
128// base properties throw 1 when set; derived, 2
129shouldBe("derived.baseDup = 0", 2);
130shouldBe("derived.baseOnly = 0", 1);
131shouldBe("derived.derivedOnly = 0", 2)
132shouldBe("derived.protoDup = 0", 2);