must
Version:
Testing and assertion library with friendly BDD syntax — awesome.must.be.true(). Many expressive matchers and is test runner and framework agnostic. Follows RFC 2119 with its use of MUST. Good and well tested stuff.
74 lines (65 loc) • 2.04 kB
JavaScript
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
})