1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
|
var O = require("oolong")
var FIRST_LINE = /^.*$/m
module.exports = AssertionError
/**
* Error object thrown when an assertion fails.
*
* @class AssertionError
* @constructor
* @param message
* @param [options]
*/
function AssertionError(msg, opts) {
this.message = msg
/**
* The asserted object.
*
* @property actual
*/
if (opts && "actual" in opts) this.actual = opts.actual
/**
* If the matcher took an argument or asserted against something (like
* `foo.must.be.true()`), then this is the expected value.
*
* @property expected
*/
if (opts && "expected" in opts) this.expected = opts.expected
/**
* Whether it makes sense to compare objects granularly or even show a diff
* view of the objects involved.
*
* Most matchers (e.g. [`empty`](#Must.prototype.empty) and
* [`string`](#Must.prototype.string)) are concrete, strict and atomic and
* don't lend themselves to be compared property by property. Others however,
* like [`eql`](#Must.prototype.eql), are more granular and comparing them
* line by line helps understand how they differ.
*
* @property diffable
*/
if (opts && "diffable" in opts) this.diffable = opts.diffable
/**
* The stack trace starting from the code that called `must`.
*
* @property stack
*/
if (opts && opts.stack != null) Object.defineProperty(this, "stack", {
value: opts.stack.replace(FIRST_LINE, this),
configurable: true, writable: true
})
else if (Error.captureStackTrace)
Error.captureStackTrace(this, opts && opts.caller || this.constructor)
}
AssertionError.prototype = Object.create(Error.prototype, {
constructor: {value: AssertionError, configurable: true, writable: true}
})
AssertionError.prototype.name = "AssertionError"
/**
* Some test runners (like [Mocha](http://visionmedia.github.io/mocha/)) expect
* this property instead.
*
* @property showDiff
* @alias diffable
*/
O.defineGetter(AssertionError.prototype, "showDiff", function() {
return this.diffable
})
|