Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / fast / js / mozilla / resources / mozilla-es5-shell.js
blobff081403393d93b20abdb7748dd01df1394b9053
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
3 /*
4 * Any copyright is dedicated to the Public Domain.
5 * http://creativecommons.org/licenses/publicdomain/
6 */
9 /*
10 * Return true if both of these return true:
11 * - LENIENT_PRED applied to CODE
12 * - STRICT_PRED applied to CODE with a use strict directive added to the front
14 * Run STRICT_PRED first, for testing code that affects the global environment
15 * in loose mode, but fails in strict mode.
17 function testLenientAndStrict(code, lenient_pred, strict_pred) {
18 return (strict_pred("'use strict'; " + code) &&
19 lenient_pred(code));
23 * completesNormally(CODE) returns true if evaluating CODE (as eval
24 * code) completes normally (rather than throwing an exception).
26 function completesNormally(code) {
27 try {
28 eval(code);
29 return true;
30 } catch (exception) {
31 return false;
36 * returns(VALUE)(CODE) returns true if evaluating CODE (as eval code)
37 * completes normally (rather than throwing an exception), yielding a value
38 * strictly equal to VALUE.
40 function returns(value) {
41 return function(code) {
42 shouldBe(code, JSON.stringify(value));
43 return true;
48 * returnsCopyOf(VALUE)(CODE) returns true if evaluating CODE (as eval code)
49 * completes normally (rather than throwing an exception), yielding a value
50 * that is deepEqual to VALUE.
52 function returnsCopyOf(value) {
53 return function(code) {
54 try {
55 return deepEqual(eval(code), value);
56 } catch (exception) {
57 return false;
63 * raisesException(EXCEPTION)(CODE) returns true if evaluating CODE (as
64 * eval code) throws an exception object that is an instance of EXCEPTION,
65 * and returns false if it throws any other error or evaluates
66 * successfully. For example: raises(TypeError)("0()") == true.
68 function raisesException(exception) {
69 return function (code) {
70 shouldThrowType(code, exception);
71 return true;
76 * parsesSuccessfully(CODE) returns true if CODE parses as function
77 * code without an error.
79 function parsesSuccessfully(code) {
80 shouldBeTrue("!!Function("+JSON.stringify(code)+")");
81 return true;
85 * parseRaisesException(EXCEPTION)(CODE) returns true if parsing CODE
86 * as function code raises EXCEPTION.
88 function parseRaisesException(exception) {
89 return function (code) {
90 shouldThrowType("Function("+JSON.stringify(code)+")", exception);
91 return true;
96 * Return the result of applying uneval to VAL, and replacing all runs
97 * of whitespace with a single horizontal space (poor man's
98 * tokenization).
100 function clean_uneval(val) {
101 return uneval(val).replace(/\s+/g, ' ');
105 * Return true if A is equal to B, where equality on arrays and objects
106 * means that they have the same set of enumerable properties, the values
107 * of each property are deep_equal, and their 'length' properties are
108 * equal. Equality on other types is ==.
110 function deepEqual(a, b) {
111 if (typeof a != typeof b)
112 return false;
114 if (typeof a == 'object') {
115 var props = {};
116 // For every property of a, does b have that property with an equal value?
117 for (var prop in a) {
118 if (!deepEqual(a[prop], b[prop]))
119 return false;
120 props[prop] = true;
122 // Are all of b's properties present on a?
123 for (var prop in b)
124 if (!props[prop])
125 return false;
126 // length isn't enumerable, but we want to check it, too.
127 return a.length == b.length;
130 if (a === b) {
131 // Distinguish 0 from -0, even though they are ===.
132 return a !== 0 || 1/a === 1/b;
135 // Treat NaNs as equal, even though NaN !== NaN.
136 // NaNs are the only non-reflexive values, i.e., if a !== a, then a is a NaN.
137 // isNaN is broken: it converts its argument to number, so isNaN("foo") => true
138 return a !== a && b !== b;