|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 |
|
3 /* |
|
4 * Any copyright is dedicated to the Public Domain. |
|
5 * http://creativecommons.org/licenses/publicdomain/ |
|
6 */ |
|
7 |
|
8 |
|
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 |
|
13 * |
|
14 * Run STRICT_PRED first, for testing code that affects the global environment |
|
15 * in loose mode, but fails in strict mode. |
|
16 */ |
|
17 function testLenientAndStrict(code, lenient_pred, strict_pred) { |
|
18 return (strict_pred("'use strict'; " + code) && |
|
19 lenient_pred(code)); |
|
20 } |
|
21 |
|
22 /* |
|
23 * completesNormally(CODE) returns true if evaluating CODE (as eval |
|
24 * code) completes normally (rather than throwing an exception). |
|
25 */ |
|
26 function completesNormally(code) { |
|
27 try { |
|
28 eval(code); |
|
29 return true; |
|
30 } catch (exception) { |
|
31 return false; |
|
32 } |
|
33 } |
|
34 |
|
35 /* |
|
36 * raisesException(EXCEPTION)(CODE) returns true if evaluating CODE (as eval |
|
37 * code) throws an exception object whose prototype is |
|
38 * EXCEPTION.prototype, and returns false if it throws any other error |
|
39 * or evaluates successfully. For example: raises(TypeError)("0()") == |
|
40 * true. |
|
41 */ |
|
42 function raisesException(exception) { |
|
43 return function (code) { |
|
44 try { |
|
45 eval(code); |
|
46 return false; |
|
47 } catch (actual) { |
|
48 return exception.prototype.isPrototypeOf(actual); |
|
49 } |
|
50 }; |
|
51 }; |
|
52 |
|
53 /* |
|
54 * parsesSuccessfully(CODE) returns true if CODE parses as function |
|
55 * code without an error. |
|
56 */ |
|
57 function parsesSuccessfully(code) { |
|
58 try { |
|
59 Function(code); |
|
60 return true; |
|
61 } catch (exception) { |
|
62 return false; |
|
63 } |
|
64 }; |
|
65 |
|
66 /* |
|
67 * parseRaisesException(EXCEPTION)(CODE) returns true if parsing CODE |
|
68 * as function code raises EXCEPTION. |
|
69 */ |
|
70 function parseRaisesException(exception) { |
|
71 return function (code) { |
|
72 try { |
|
73 Function(code); |
|
74 return false; |
|
75 } catch (actual) { |
|
76 return exception.prototype.isPrototypeOf(actual); |
|
77 } |
|
78 }; |
|
79 }; |
|
80 |
|
81 /* |
|
82 * Return the result of applying uneval to VAL, and replacing all runs |
|
83 * of whitespace with a single horizontal space (poor man's |
|
84 * tokenization). |
|
85 */ |
|
86 function clean_uneval(val) { |
|
87 return uneval(val).replace(/\s+/g, ' '); |
|
88 } |