]>
Commit | Line | Data |
---|---|---|
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 | ||
26 | function 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 | ||
41 | function 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 | ||
55 | function globalStaticFunction() | |
56 | { | |
57 | return 4; | |
58 | } | |
59 | ||
60 | shouldBe("globalStaticValue", 3); | |
61 | shouldBe("globalStaticFunction()", 4); | |
62 | ||
63 | shouldBe("typeof MyObject", "function"); // our object implements 'call' | |
64 | MyObject.cantFind = 1; | |
65 | shouldBe("MyObject.cantFind", undefined); | |
66 | MyObject.regularType = 1; | |
67 | shouldBe("MyObject.regularType", 1); | |
68 | MyObject.alwaysOne = 2; | |
69 | shouldBe("MyObject.alwaysOne", 1); | |
70 | MyObject.cantDelete = 1; | |
71 | delete MyObject.cantDelete; | |
72 | shouldBe("MyObject.cantDelete", 1); | |
73 | shouldBe("delete MyObject.throwOnDelete", 2); // deleteProperty -- should throw 2 | |
74 | MyObject.cantSet = 1; | |
75 | shouldBe("MyObject.cantSet", undefined); | |
76 | ||
77 | var foundMyPropertyName = false; | |
78 | var foundRegularType = false; | |
79 | for (var p in MyObject) { | |
80 | if (p == "myPropertyName") | |
81 | foundMyPropertyName = true; | |
82 | if (p == "regularType") | |
83 | foundRegularType = true; | |
84 | } | |
85 | print(foundMyPropertyName | |
86 | ? "PASS: MyObject.myPropertyName was enumerated" | |
87 | : "__FAIL__: MyObject.myPropertyName was not enumerated"); | |
88 | print(foundRegularType | |
89 | ? "PASS: MyObject.regularType was enumerated" | |
90 | : "__FAIL__: MyObject.regularType was not enumerated"); | |
91 | ||
92 | myObject = new MyObject(); | |
93 | ||
94 | shouldBe("delete MyObject.regularType", true); | |
95 | shouldBe("MyObject.regularType", undefined); | |
96 | shouldBe("MyObject(0)", 1); | |
97 | shouldBe("MyObject()", undefined); | |
98 | shouldBe("typeof myObject", "object"); | |
99 | shouldBe("MyObject ? 1 : 0", true); // toBoolean | |
100 | shouldBe("+MyObject", 1); // toNumber | |
101 | shouldBe("(MyObject.toString())", "[object MyObject]"); // toString | |
9dae56ea | 102 | shouldBe("String(MyObject)", "MyObjectAsString"); // type conversion to string |
b37bf2e1 A |
103 | shouldBe("MyObject - 0", NaN); // toPrimitive |
104 | ||
105 | shouldBe("typeof MyConstructor", "object"); | |
106 | constructedObject = new MyConstructor(1); | |
107 | shouldBe("typeof constructedObject", "object"); | |
108 | shouldBe("constructedObject.value", 1); | |
109 | shouldBe("myObject instanceof MyObject", true); | |
110 | shouldBe("(new Object()) instanceof MyObject", false); | |
111 | ||
112 | shouldThrow("MyObject.nullGetSet = 1"); | |
113 | shouldThrow("MyObject.nullGetSet"); | |
114 | shouldThrow("MyObject.nullCall()"); | |
115 | shouldThrow("MyObject.hasPropertyLie"); | |
116 | ||
117 | derived = new Derived(); | |
118 | ||
119 | // base properties and functions return 1 when called/gotten; derived, 2 | |
120 | shouldBe("derived.baseProtoDup()", 2); | |
121 | shouldBe("derived.baseProto()", 1); | |
122 | shouldBe("derived.baseDup", 2); | |
123 | shouldBe("derived.baseOnly", 1); | |
124 | shouldBe("derived.protoOnly()", 2); | |
125 | shouldBe("derived.protoDup", 2); | |
126 | shouldBe("derived.derivedOnly", 2) | |
127 | ||
128 | // base properties throw 1 when set; derived, 2 | |
129 | shouldBe("derived.baseDup = 0", 2); | |
130 | shouldBe("derived.baseOnly = 0", 1); | |
131 | shouldBe("derived.derivedOnly = 0", 2) | |
132 | shouldBe("derived.protoDup = 0", 2); |