1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
4 * Any copyright is dedicated to the Public Domain.
5 * http://creativecommons.org/licenses/publicdomain/
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
) &&
23 * completesNormally(CODE) returns true if evaluating CODE (as eval
24 * code) completes normally (rather than throwing an exception).
26 function completesNormally(code
) {
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
));
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
) {
55 return deepEqual(eval(code
), value
);
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
);
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
)+")");
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
);
96 * Return the result of applying uneval to VAL, and replacing all runs
97 * of whitespace with a single horizontal space (poor man's
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
)
114 if (typeof a
== 'object') {
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
]))
122 // Are all of b's properties present on a?
126 // length isn't enumerable, but we want to check it, too.
127 return a
.length
== b
.length
;
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
;