diff options
| author | Ken Sheedlo | 2013-08-13 15:30:52 -0700 |
|---|---|---|
| committer | Ken Sheedlo | 2013-08-15 13:23:18 -0700 |
| commit | 37123cd2858b4e318ed8109af745312df4848577 (patch) | |
| tree | 118abe5fd50e1c6fef4281972cf52c0162482fa8 /test/matchers.js | |
| parent | fe267e30b95e8f48ddc670b97d485f18fb64d39e (diff) | |
| download | angular.js-37123cd2858b4e318ed8109af745312df4848577.tar.bz2 | |
feat(minerr): log minerr doc url in development
Closes #3566
Diffstat (limited to 'test/matchers.js')
| -rw-r--r-- | test/matchers.js | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/test/matchers.js b/test/matchers.js index b7d336b7..57bf35c7 100644 --- a/test/matchers.js +++ b/test/matchers.js @@ -165,6 +165,53 @@ beforeEach(function() { toThrowMatching: function(expected) { return jasmine.Matchers.prototype.toThrow.call(this, expected); + }, + + toThrowMinErr: function(namespace, code, content) { + var result, + exception, + exceptionMessage = '', + escapeRegexp = function (str) { + // This function escapes all special regex characters. + // We use it to create matching regex from arbitrary strings. + // http://stackoverflow.com/questions/3446170/escape-string-for-use-in-javascript-regex + return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&"); + }, + codeRegex = new RegExp('^\\[' + escapeRegexp(namespace) + ':' + escapeRegexp(code) + '\\]'), + not = this.isNot ? "not " : "", + regex = jasmine.isA_("RegExp", content) ? content : + isDefined(content) ? new RegExp(escapeRegexp(content)) : undefined; + + if(!isFunction(this.actual)) { + throw new Error('Actual is not a function'); + } + + try { + this.actual(); + } catch (e) { + exception = e; + } + + if (exception) { + exceptionMessage = exception.message || exception; + } + + this.message = function () { + return "Expected function " + not + "to throw " + + namespace + "MinErr('" + code + "')" + + (regex ? " matching " + regex.toString() : "") + + (exception ? ", but it threw " + exceptionMessage : "."); + }; + + result = codeRegex.test(exceptionMessage); + if (!result) { + return result; + } + + if (isDefined(regex)) { + return regex.test(exceptionMessage); + } + return result; } }); }); |
