blob: ed27f981af048a12031fd03693fb71515738ccfb [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// Copyright 2013 the V8 project authors. All rights reserved.
2// Copyright (C) 2005, 2006, 2007, 2008, 2009 Apple 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 INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
14// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16// DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
17// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23
24description(
25"This test checks that using the typeof operator on a JavaScript value and comparing it to a constant string works as expected."
26);
27
28function isUndefined(a)
29{
30 return typeof a == "undefined";
31}
32
33shouldBeTrue("isUndefined(undefined)");
34shouldBeFalse("isUndefined(1)");
35
36function isUndefinedStrict(a)
37{
38 return typeof a === "undefined";
39}
40
41shouldBeTrue("isUndefinedStrict(undefined)");
42shouldBeFalse("isUndefinedStrict(1)");
43
44function isBoolean(a)
45{
46 return typeof a == "boolean";
47}
48
49shouldBeTrue("isBoolean(true)");
50shouldBeTrue("isBoolean(false)");
51shouldBeFalse("isBoolean(1)");
52
53function isBooleanStrict(a)
54{
55 return typeof a === "boolean";
56}
57
58shouldBeTrue("isBooleanStrict(true)");
59shouldBeTrue("isBooleanStrict(false)");
60shouldBeFalse("isBooleanStrict(1)");
61
62function isNumber(a)
63{
64 return typeof a == "number";
65}
66
67shouldBeTrue("isNumber(1)");
68shouldBeFalse("isNumber(undefined)");
69
70function isNumberStrict(a)
71{
72 return typeof a === "number";
73}
74
75shouldBeTrue("isNumberStrict(1)");
76shouldBeFalse("isNumberStrict(undefined)");
77
78function isString(a)
79{
80 return typeof a == "string";
81}
82
83shouldBeTrue("isString('string')");
84shouldBeFalse("isString(1)");
85
86function isStringStrict(a)
87{
88 return typeof a === "string";
89}
90
91shouldBeTrue("isStringStrict('string')");
92shouldBeFalse("isStringStrict(1)");
93
94function isObject(a)
95{
96 return typeof a == "object";
97}
98
99shouldBeTrue("isObject({ })");
100shouldBeFalse("isObject(1)");
101
102function isObjectStrict(a)
103{
104 return typeof a === "object";
105}
106
107shouldBeTrue("isObjectStrict({ })");
108shouldBeFalse("isObjectStrict(1)");
109
110function isFunction(a)
111{
112 return typeof a == "function";
113}
114
115shouldBeTrue("isFunction(function () { })");
116shouldBeFalse("isFunction(1)");
117
118function isFunctionStrict(a)
119{
120 return typeof a === "function";
121}
122
123shouldBeTrue("isFunctionStrict(function () { })");
124shouldBeFalse("isFunctionStrict(1)");
125
126function complexIsUndefinedTest()
127{
128 function replace_formats() {
129 var o = ["text", 0];
130 if (typeof o == "string") {
131 } else if (typeof o == "undefined") {
132 } else if (typeof o == "object" && typeof o[0] == "string") {
133 return "PASS";
134 }
135 return "FAIL";
136 };
137
138 return "%d".replace(/%d/, replace_formats);
139}
140shouldBe("complexIsUndefinedTest()", "'PASS'");
141
142function complexIsBooleanTest()
143{
144 function replace_formats() {
145 var o = ["text", 0];
146 if (typeof o == "string") {
147 } else if (typeof o == "boolean") {
148 } else if (typeof o == "object" && typeof o[0] == "string") {
149 return "PASS";
150 }
151 return "FAIL";
152 };
153
154 return "%d".replace(/%d/, replace_formats);
155}
156shouldBe("complexIsBooleanTest()", "'PASS'");
157
158function complexIsNumberTest()
159{
160 function replace_formats() {
161 var o = ["text", 0];
162 if (typeof o == "string") {
163 } else if (typeof o == "number") {
164 } else if (typeof o == "object" && typeof o[0] == "string") {
165 return "PASS";
166 }
167 return "FAIL";
168 };
169
170 return "%d".replace(/%d/, replace_formats);
171}
172shouldBe("complexIsNumberTest()", "'PASS'");
173
174function complexIsStringTest()
175{
176 function replace_formats() {
177 var o = ["text", 0];
178 if (typeof o == "string") {
179 } else if (typeof o == "string") {
180 } else if (typeof o == "object" && typeof o[0] == "string") {
181 return "PASS";
182 }
183 return "FAIL";
184 };
185
186 return "%d".replace(/%d/, replace_formats);
187}
188shouldBe("complexIsStringTest()", "'PASS'");
189
190function complexIsObjectTest()
191{
192 var a = ["text", 0];
193 function replace_formats() {
194 var o = function () { };
195 if (typeof o == "string") {
196 } else if (typeof o == "object") {
197 } else if (typeof o == "function" && typeof a[0] == "string") {
198 return "PASS";
199 }
200 return "FAIL";
201 };
202
203 return "%d".replace(/%d/, replace_formats);
204}
205shouldBe("complexIsObjectTest()", "'PASS'");
206
207function complexIsFunctionTest()
208{
209 function replace_formats() {
210 var o = ["text", 0];
211 if (typeof o == "string") {
212 } else if (typeof o == "function") {
213 } else if (typeof o == "object" && typeof o[0] == "string") {
214 return "PASS";
215 }
216 return "FAIL";
217 };
218
219 return "%d".replace(/%d/, replace_formats);
220}
221shouldBe("complexIsFunctionTest()", "'PASS'");
222
223function complexIsUndefinedStrictTest()
224{
225 function replace_formats() {
226 var o = ["text", 0];
227 if (typeof o == "string") {
228 } else if (typeof o === "undefined") {
229 } else if (typeof o == "object" && typeof o[0] == "string") {
230 return "PASS";
231 }
232 return "FAIL";
233 };
234
235 return "%d".replace(/%d/, replace_formats);
236}
237shouldBe("complexIsUndefinedStrictTest()", "'PASS'");
238
239function complexIsBooleanStrictTest()
240{
241 function replace_formats() {
242 var o = ["text", 0];
243 if (typeof o == "string") {
244 } else if (typeof o === "boolean") {
245 } else if (typeof o == "object" && typeof o[0] == "string") {
246 return "PASS";
247 }
248 return "FAIL";
249 };
250
251 return "%d".replace(/%d/, replace_formats);
252}
253shouldBe("complexIsBooleanStrictTest()", "'PASS'");
254
255function complexIsNumberStrictTest()
256{
257 function replace_formats() {
258 var o = ["text", 0];
259 if (typeof o == "string") {
260 } else if (typeof o === "number") {
261 } else if (typeof o == "object" && typeof o[0] == "string") {
262 return "PASS";
263 }
264 return "FAIL";
265 };
266
267 return "%d".replace(/%d/, replace_formats);
268}
269shouldBe("complexIsNumberStrictTest()", "'PASS'");
270
271function complexIsStringStrictTest()
272{
273 function replace_formats() {
274 var o = ["text", 0];
275 if (typeof o == "string") {
276 } else if (typeof o === "string") {
277 } else if (typeof o == "object" && typeof o[0] == "string") {
278 return "PASS";
279 }
280 return "FAIL";
281 };
282
283 return "%d".replace(/%d/, replace_formats);
284}
285shouldBe("complexIsStringStrictTest()", "'PASS'");
286
287function complexIsObjectStrictTest()
288{
289 var a = ["text", 0];
290 function replace_formats() {
291 var o = function () { };
292 if (typeof o == "string") {
293 } else if (typeof o === "object") {
294 } else if (typeof o == "function" && typeof a[0] == "string") {
295 return "PASS";
296 }
297 return "FAIL";
298 };
299
300 return "%d".replace(/%d/, replace_formats);
301}
302shouldBe("complexIsObjectStrictTest()", "'PASS'");
303
304function complexIsFunctionStrictTest()
305{
306 function replace_formats() {
307 var o = ["text", 0];
308 if (typeof o == "string") {
309 } else if (typeof o === "function") {
310 } else if (typeof o == "object" && typeof o[0] == "string") {
311 return "PASS";
312 }
313 return "FAIL";
314 };
315
316 return "%d".replace(/%d/, replace_formats);
317}
318shouldBe("complexIsFunctionStrictTest()", "'PASS'");