michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: michael@0: /* michael@0: * Any copyright is dedicated to the Public Domain. michael@0: * http://creativecommons.org/licenses/publicdomain/ michael@0: */ michael@0: michael@0: michael@0: /* michael@0: * Return true if both of these return true: michael@0: * - LENIENT_PRED applied to CODE michael@0: * - STRICT_PRED applied to CODE with a use strict directive added to the front michael@0: * michael@0: * Run STRICT_PRED first, for testing code that affects the global environment michael@0: * in loose mode, but fails in strict mode. michael@0: */ michael@0: function testLenientAndStrict(code, lenient_pred, strict_pred) { michael@0: return (strict_pred("'use strict'; " + code) && michael@0: lenient_pred(code)); michael@0: } michael@0: michael@0: /* michael@0: * completesNormally(CODE) returns true if evaluating CODE (as eval michael@0: * code) completes normally (rather than throwing an exception). michael@0: */ michael@0: function completesNormally(code) { michael@0: try { michael@0: eval(code); michael@0: return true; michael@0: } catch (exception) { michael@0: return false; michael@0: } michael@0: } michael@0: michael@0: /* michael@0: * raisesException(EXCEPTION)(CODE) returns true if evaluating CODE (as eval michael@0: * code) throws an exception object whose prototype is michael@0: * EXCEPTION.prototype, and returns false if it throws any other error michael@0: * or evaluates successfully. For example: raises(TypeError)("0()") == michael@0: * true. michael@0: */ michael@0: function raisesException(exception) { michael@0: return function (code) { michael@0: try { michael@0: eval(code); michael@0: return false; michael@0: } catch (actual) { michael@0: return exception.prototype.isPrototypeOf(actual); michael@0: } michael@0: }; michael@0: }; michael@0: michael@0: /* michael@0: * parsesSuccessfully(CODE) returns true if CODE parses as function michael@0: * code without an error. michael@0: */ michael@0: function parsesSuccessfully(code) { michael@0: try { michael@0: Function(code); michael@0: return true; michael@0: } catch (exception) { michael@0: return false; michael@0: } michael@0: }; michael@0: michael@0: /* michael@0: * parseRaisesException(EXCEPTION)(CODE) returns true if parsing CODE michael@0: * as function code raises EXCEPTION. michael@0: */ michael@0: function parseRaisesException(exception) { michael@0: return function (code) { michael@0: try { michael@0: Function(code); michael@0: return false; michael@0: } catch (actual) { michael@0: return exception.prototype.isPrototypeOf(actual); michael@0: } michael@0: }; michael@0: }; michael@0: michael@0: /* michael@0: * Return the result of applying uneval to VAL, and replacing all runs michael@0: * of whitespace with a single horizontal space (poor man's michael@0: * tokenization). michael@0: */ michael@0: function clean_uneval(val) { michael@0: return uneval(val).replace(/\s+/g, ' '); michael@0: }