diff --git a/lib/jasmine-core/jasmine.js b/lib/jasmine-core/jasmine.js index 1976a797..d642adc5 100644 --- a/lib/jasmine-core/jasmine.js +++ b/lib/jasmine-core/jasmine.js @@ -3702,58 +3702,30 @@ getJasmineRequireObj().toThrowError = function(j$) { function toThrowError () { return { compare: function(actual) { - var threw = false, - pass = {pass: true}, - fail = {pass: false}, + var errorMatcher = getMatcher.apply(null, arguments), thrown; if (typeof actual != 'function') { throw new Error(getErrorMsg('Actual is not a Function')); } - var errorMatcher = getMatcher.apply(null, arguments); - try { actual(); + return fail('Expected function to throw an Error.'); } catch (e) { - threw = true; thrown = e; } - if (!threw) { - fail.message = 'Expected function to throw an Error.'; - return fail; - } - - // Get Error constructor of thrown if (!isErrorObject(thrown)) { - fail.message = function() { return 'Expected function to throw an Error, but it threw ' + j$.pp(thrown) + '.'; }; - return fail; + return fail(function() { return 'Expected function to throw an Error, but it threw ' + j$.pp(thrown) + '.'; }); } - if (errorMatcher.hasNoSpecifics()) { - pass.message = 'Expected function not to throw an Error, but it threw ' + j$.fnNameFor(thrown) + '.'; - return pass; - } - - if (errorMatcher.matches(thrown)) { - pass.message = function() { - return 'Expected function not to throw ' + errorMatcher.errorTypeDescription + errorMatcher.messageDescription() + '.'; - }; - return pass; - } else { - fail.message = function() { - return 'Expected function to throw ' + errorMatcher.errorTypeDescription + errorMatcher.messageDescription() + - ', but it threw ' + errorMatcher.thrownDescription(thrown) + '.'; - }; - return fail; - } + return errorMatcher.match(thrown); } }; function getMatcher() { - var expected = null, - errorType = null; + var expected, errorType; if (arguments[2]) { errorType = arguments[1]; @@ -3761,14 +3733,29 @@ getJasmineRequireObj().toThrowError = function(j$) { if (!isAnErrorType(errorType)) { throw new Error(getErrorMsg('Expected error type is not an Error.')); } + + return conditionalMatcher(expected, errorType); } else if (arguments[1]) { expected = arguments[1]; - if (isAnErrorType(expected)) { - errorType = expected; - expected = null; + if (isAnErrorType(arguments[1])) { + return conditionalMatcher(null, arguments[1]); + } else { + return conditionalMatcher(arguments[1], null); } + } else { + return anyMatcher(); } + } + function anyMatcher() { + return { + match: function(error) { + return pass('Expected function not to throw an Error, but it threw ' + j$.fnNameFor(error) + '.'); + } + }; + } + + function conditionalMatcher(expected, errorType) { if (expected && !isStringOrRegExp(expected)) { if (errorType) { throw new Error(getErrorMsg('Expected error message is not a string or RegExp.')); @@ -3785,33 +3772,46 @@ getJasmineRequireObj().toThrowError = function(j$) { } } + var errorTypeDescription = errorType ? j$.fnNameFor(errorType) : 'an exception'; + + function thrownDescription(thrown) { + var thrownName = errorType ? j$.fnNameFor(thrown.constructor) : 'an exception', + thrownMessage = ''; + + if (expected) { + thrownMessage = ' with message ' + j$.pp(thrown.message); + } + + return thrownName + thrownMessage; + } + + function messageDescription() { + if (expected === null) { + return ''; + } else if (expected instanceof RegExp) { + return ' with a message matching ' + j$.pp(expected); + } else { + return ' with message ' + j$.pp(expected); + } + } + + function matches(error) { + return (errorType === null || error instanceof errorType) && + (expected === null || messageMatch(error.message)); + } + return { - errorTypeDescription: errorType ? j$.fnNameFor(errorType) : 'an exception', - thrownDescription: function(thrown) { - var thrownName = errorType ? j$.fnNameFor(thrown.constructor) : 'an exception', - thrownMessage = ''; - - if (expected) { - thrownMessage = ' with message ' + j$.pp(thrown.message); - } - - return thrownName + thrownMessage; - }, - messageDescription: function() { - if (expected === null) { - return ''; - } else if (expected instanceof RegExp) { - return ' with a message matching ' + j$.pp(expected); + match: function(thrown) { + if (matches(thrown)) { + return pass(function() { + return 'Expected function not to throw ' + errorTypeDescription + messageDescription() + '.'; + }); } else { - return ' with message ' + j$.pp(expected); + return fail(function() { + return 'Expected function to throw ' + errorTypeDescription + messageDescription() + + ', but it threw ' + thrownDescription(thrown) + '.'; + }); } - }, - hasNoSpecifics: function() { - return expected === null && errorType === null; - }, - matches: function(error) { - return (errorType === null || error instanceof errorType) && - (expected === null || messageMatch(error.message)); } }; } @@ -3842,6 +3842,20 @@ getJasmineRequireObj().toThrowError = function(j$) { } } + function pass(message) { + return { + pass: true, + message: message + }; + } + + function fail(message) { + return { + pass: false, + message: message + }; + } + return toThrowError; }; diff --git a/src/core/matchers/toThrowError.js b/src/core/matchers/toThrowError.js index 7cdc1d9f..d6e772a1 100644 --- a/src/core/matchers/toThrowError.js +++ b/src/core/matchers/toThrowError.js @@ -18,58 +18,30 @@ getJasmineRequireObj().toThrowError = function(j$) { function toThrowError () { return { compare: function(actual) { - var threw = false, - pass = {pass: true}, - fail = {pass: false}, + var errorMatcher = getMatcher.apply(null, arguments), thrown; if (typeof actual != 'function') { throw new Error(getErrorMsg('Actual is not a Function')); } - var errorMatcher = getMatcher.apply(null, arguments); - try { actual(); + return fail('Expected function to throw an Error.'); } catch (e) { - threw = true; thrown = e; } - if (!threw) { - fail.message = 'Expected function to throw an Error.'; - return fail; - } - - // Get Error constructor of thrown if (!isErrorObject(thrown)) { - fail.message = function() { return 'Expected function to throw an Error, but it threw ' + j$.pp(thrown) + '.'; }; - return fail; + return fail(function() { return 'Expected function to throw an Error, but it threw ' + j$.pp(thrown) + '.'; }); } - if (errorMatcher.hasNoSpecifics()) { - pass.message = 'Expected function not to throw an Error, but it threw ' + j$.fnNameFor(thrown) + '.'; - return pass; - } - - if (errorMatcher.matches(thrown)) { - pass.message = function() { - return 'Expected function not to throw ' + errorMatcher.errorTypeDescription + errorMatcher.messageDescription() + '.'; - }; - return pass; - } else { - fail.message = function() { - return 'Expected function to throw ' + errorMatcher.errorTypeDescription + errorMatcher.messageDescription() + - ', but it threw ' + errorMatcher.thrownDescription(thrown) + '.'; - }; - return fail; - } + return errorMatcher.match(thrown); } }; function getMatcher() { - var expected = null, - errorType = null; + var expected, errorType; if (arguments[2]) { errorType = arguments[1]; @@ -77,14 +49,29 @@ getJasmineRequireObj().toThrowError = function(j$) { if (!isAnErrorType(errorType)) { throw new Error(getErrorMsg('Expected error type is not an Error.')); } + + return conditionalMatcher(expected, errorType); } else if (arguments[1]) { expected = arguments[1]; - if (isAnErrorType(expected)) { - errorType = expected; - expected = null; + if (isAnErrorType(arguments[1])) { + return conditionalMatcher(null, arguments[1]); + } else { + return conditionalMatcher(arguments[1], null); } + } else { + return anyMatcher(); } + } + function anyMatcher() { + return { + match: function(error) { + return pass('Expected function not to throw an Error, but it threw ' + j$.fnNameFor(error) + '.'); + } + }; + } + + function conditionalMatcher(expected, errorType) { if (expected && !isStringOrRegExp(expected)) { if (errorType) { throw new Error(getErrorMsg('Expected error message is not a string or RegExp.')); @@ -101,33 +88,46 @@ getJasmineRequireObj().toThrowError = function(j$) { } } + var errorTypeDescription = errorType ? j$.fnNameFor(errorType) : 'an exception'; + + function thrownDescription(thrown) { + var thrownName = errorType ? j$.fnNameFor(thrown.constructor) : 'an exception', + thrownMessage = ''; + + if (expected) { + thrownMessage = ' with message ' + j$.pp(thrown.message); + } + + return thrownName + thrownMessage; + } + + function messageDescription() { + if (expected === null) { + return ''; + } else if (expected instanceof RegExp) { + return ' with a message matching ' + j$.pp(expected); + } else { + return ' with message ' + j$.pp(expected); + } + } + + function matches(error) { + return (errorType === null || error instanceof errorType) && + (expected === null || messageMatch(error.message)); + } + return { - errorTypeDescription: errorType ? j$.fnNameFor(errorType) : 'an exception', - thrownDescription: function(thrown) { - var thrownName = errorType ? j$.fnNameFor(thrown.constructor) : 'an exception', - thrownMessage = ''; - - if (expected) { - thrownMessage = ' with message ' + j$.pp(thrown.message); - } - - return thrownName + thrownMessage; - }, - messageDescription: function() { - if (expected === null) { - return ''; - } else if (expected instanceof RegExp) { - return ' with a message matching ' + j$.pp(expected); + match: function(thrown) { + if (matches(thrown)) { + return pass(function() { + return 'Expected function not to throw ' + errorTypeDescription + messageDescription() + '.'; + }); } else { - return ' with message ' + j$.pp(expected); + return fail(function() { + return 'Expected function to throw ' + errorTypeDescription + messageDescription() + + ', but it threw ' + thrownDescription(thrown) + '.'; + }); } - }, - hasNoSpecifics: function() { - return expected === null && errorType === null; - }, - matches: function(error) { - return (errorType === null || error instanceof errorType) && - (expected === null || messageMatch(error.message)); } }; } @@ -158,5 +158,19 @@ getJasmineRequireObj().toThrowError = function(j$) { } } + function pass(message) { + return { + pass: true, + message: message + }; + } + + function fail(message) { + return { + pass: false, + message: message + }; + } + return toThrowError; };