mirror of
https://github.com/chaijs/chai
synced 2024-11-15 00:07:11 +00:00
core: [assertion-error] replace internal assertion error with dep
This commit is contained in:
parent
9e8b050e02
commit
7bc580910e
4 changed files with 134 additions and 138 deletions
15
lib/chai.js
15
lib/chai.js
|
@ -13,17 +13,11 @@ var used = []
|
|||
|
||||
exports.version = '1.6.1';
|
||||
|
||||
/*!
|
||||
* Primary `Assertion` prototype
|
||||
*/
|
||||
|
||||
exports.Assertion = require('./chai/assertion');
|
||||
|
||||
/*!
|
||||
* Assertion Error
|
||||
*/
|
||||
|
||||
exports.AssertionError = require('./chai/error');
|
||||
exports.AssertionError = require('assertion-error');
|
||||
|
||||
/*!
|
||||
* Utils for plugins (not exported)
|
||||
|
@ -50,6 +44,13 @@ exports.use = function (fn) {
|
|||
return this;
|
||||
};
|
||||
|
||||
/*!
|
||||
* Primary `Assertion` prototype
|
||||
*/
|
||||
|
||||
var assertion = require('./chai/assertion');
|
||||
exports.use(assertion);
|
||||
|
||||
/*!
|
||||
* Core Assertions
|
||||
*/
|
||||
|
|
|
@ -5,128 +5,126 @@
|
|||
* MIT Licensed
|
||||
*/
|
||||
|
||||
/*!
|
||||
* Module dependencies.
|
||||
*/
|
||||
module.exports = function (_chai, util) {
|
||||
/*!
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var AssertionError = require('./error')
|
||||
, util = require('./utils')
|
||||
, flag = util.flag;
|
||||
var AssertionError = _chai.AssertionError
|
||||
, flag = util.flag;
|
||||
|
||||
/*!
|
||||
* Module export.
|
||||
*/
|
||||
/*!
|
||||
* Module export.
|
||||
*/
|
||||
|
||||
module.exports = Assertion;
|
||||
_chai.Assertion = Assertion;
|
||||
|
||||
/*!
|
||||
* Assertion Constructor
|
||||
*
|
||||
* Creates object for chaining.
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
|
||||
/*!
|
||||
* Assertion Constructor
|
||||
*
|
||||
* Creates object for chaining.
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
|
||||
function Assertion (obj, msg, stack) {
|
||||
flag(this, 'ssfi', stack || arguments.callee);
|
||||
flag(this, 'object', obj);
|
||||
flag(this, 'message', msg);
|
||||
}
|
||||
|
||||
/*!
|
||||
* ### Assertion.includeStack
|
||||
*
|
||||
* User configurable property, influences whether stack trace
|
||||
* is included in Assertion error message. Default of false
|
||||
* suppresses stack trace in the error message
|
||||
*
|
||||
* Assertion.includeStack = true; // enable stack on error
|
||||
*
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Assertion.includeStack = false;
|
||||
|
||||
/*!
|
||||
* ### Assertion.showDiff
|
||||
*
|
||||
* User configurable property, influences whether or not
|
||||
* the `showDiff` flag should be included in the thrown
|
||||
* AssertionErrors. `false` will always be `false`; `true`
|
||||
* will be true when the assertion has requested a diff
|
||||
* be shown.
|
||||
*
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Assertion.showDiff = true;
|
||||
|
||||
Assertion.addProperty = function (name, fn) {
|
||||
util.addProperty(this.prototype, name, fn);
|
||||
};
|
||||
|
||||
Assertion.addMethod = function (name, fn) {
|
||||
util.addMethod(this.prototype, name, fn);
|
||||
};
|
||||
|
||||
Assertion.addChainableMethod = function (name, fn, chainingBehavior) {
|
||||
util.addChainableMethod(this.prototype, name, fn, chainingBehavior);
|
||||
};
|
||||
|
||||
Assertion.overwriteProperty = function (name, fn) {
|
||||
util.overwriteProperty(this.prototype, name, fn);
|
||||
};
|
||||
|
||||
Assertion.overwriteMethod = function (name, fn) {
|
||||
util.overwriteMethod(this.prototype, name, fn);
|
||||
};
|
||||
|
||||
/*!
|
||||
* ### .assert(expression, message, negateMessage, expected, actual)
|
||||
*
|
||||
* Executes an expression and check expectations. Throws AssertionError for reporting if test doesn't pass.
|
||||
*
|
||||
* @name assert
|
||||
* @param {Philosophical} expression to be tested
|
||||
* @param {String} message to display if fails
|
||||
* @param {String} negatedMessage to display if negated expression fails
|
||||
* @param {Mixed} expected value (remember to check for negation)
|
||||
* @param {Mixed} actual (optional) will default to `this.obj`
|
||||
* @api private
|
||||
*/
|
||||
|
||||
Assertion.prototype.assert = function (expr, msg, negateMsg, expected, _actual, showDiff) {
|
||||
var ok = util.test(this, arguments);
|
||||
if (true !== showDiff) showDiff = false;
|
||||
if (true !== Assertion.showDiff) showDiff = false;
|
||||
|
||||
if (!ok) {
|
||||
var msg = util.getMessage(this, arguments)
|
||||
, actual = util.getActual(this, arguments);
|
||||
throw new AssertionError({
|
||||
message: msg
|
||||
, actual: actual
|
||||
, expected: expected
|
||||
, stackStartFunction: (Assertion.includeStack) ? this.assert : flag(this, 'ssfi')
|
||||
, showDiff: showDiff
|
||||
});
|
||||
function Assertion (obj, msg, stack) {
|
||||
flag(this, 'ssfi', stack || arguments.callee);
|
||||
flag(this, 'object', obj);
|
||||
flag(this, 'message', msg);
|
||||
}
|
||||
|
||||
/*!
|
||||
* ### Assertion.includeStack
|
||||
*
|
||||
* User configurable property, influences whether stack trace
|
||||
* is included in Assertion error message. Default of false
|
||||
* suppresses stack trace in the error message
|
||||
*
|
||||
* Assertion.includeStack = true; // enable stack on error
|
||||
*
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Assertion.includeStack = false;
|
||||
|
||||
/*!
|
||||
* ### Assertion.showDiff
|
||||
*
|
||||
* User configurable property, influences whether or not
|
||||
* the `showDiff` flag should be included in the thrown
|
||||
* AssertionErrors. `false` will always be `false`; `true`
|
||||
* will be true when the assertion has requested a diff
|
||||
* be shown.
|
||||
*
|
||||
* @api public
|
||||
*/
|
||||
|
||||
Assertion.showDiff = true;
|
||||
|
||||
Assertion.addProperty = function (name, fn) {
|
||||
util.addProperty(this.prototype, name, fn);
|
||||
};
|
||||
|
||||
Assertion.addMethod = function (name, fn) {
|
||||
util.addMethod(this.prototype, name, fn);
|
||||
};
|
||||
|
||||
Assertion.addChainableMethod = function (name, fn, chainingBehavior) {
|
||||
util.addChainableMethod(this.prototype, name, fn, chainingBehavior);
|
||||
};
|
||||
|
||||
Assertion.overwriteProperty = function (name, fn) {
|
||||
util.overwriteProperty(this.prototype, name, fn);
|
||||
};
|
||||
|
||||
Assertion.overwriteMethod = function (name, fn) {
|
||||
util.overwriteMethod(this.prototype, name, fn);
|
||||
};
|
||||
|
||||
/*!
|
||||
* ### .assert(expression, message, negateMessage, expected, actual)
|
||||
*
|
||||
* Executes an expression and check expectations. Throws AssertionError for reporting if test doesn't pass.
|
||||
*
|
||||
* @name assert
|
||||
* @param {Philosophical} expression to be tested
|
||||
* @param {String} message to display if fails
|
||||
* @param {String} negatedMessage to display if negated expression fails
|
||||
* @param {Mixed} expected value (remember to check for negation)
|
||||
* @param {Mixed} actual (optional) will default to `this.obj`
|
||||
* @api private
|
||||
*/
|
||||
|
||||
Assertion.prototype.assert = function (expr, msg, negateMsg, expected, _actual, showDiff) {
|
||||
var ok = util.test(this, arguments);
|
||||
if (true !== showDiff) showDiff = false;
|
||||
if (true !== Assertion.showDiff) showDiff = false;
|
||||
|
||||
if (!ok) {
|
||||
var msg = util.getMessage(this, arguments)
|
||||
, actual = util.getActual(this, arguments);
|
||||
throw new AssertionError(msg, {
|
||||
actual: actual
|
||||
, expected: expected
|
||||
, showDiff: showDiff
|
||||
}, (Assertion.includeStack) ? this.assert : flag(this, 'ssfi'));
|
||||
}
|
||||
};
|
||||
|
||||
/*!
|
||||
* ### ._obj
|
||||
*
|
||||
* Quick reference to stored `actual` value for plugin developers.
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
|
||||
Object.defineProperty(Assertion.prototype, '_obj',
|
||||
{ get: function () {
|
||||
return flag(this, 'object');
|
||||
}
|
||||
, set: function (val) {
|
||||
flag(this, 'object', val);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
/*!
|
||||
* ### ._obj
|
||||
*
|
||||
* Quick reference to stored `actual` value for plugin developers.
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
|
||||
Object.defineProperty(Assertion.prototype, '_obj',
|
||||
{ get: function () {
|
||||
return flag(this, 'object');
|
||||
}
|
||||
, set: function (val) {
|
||||
flag(this, 'object', val);
|
||||
}
|
||||
});
|
||||
|
|
|
@ -664,10 +664,11 @@ module.exports = function (chai, util) {
|
|||
} else if ('string' === typeof exp) {
|
||||
obj.to.contain.string(inc);
|
||||
} else {
|
||||
throw new chai.AssertionError({
|
||||
message: 'expected an array or string'
|
||||
, stackStartFunction: assert.include
|
||||
});
|
||||
throw new chai.AssertionError(
|
||||
'expected an array or string'
|
||||
, null
|
||||
, assert.include
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -695,10 +696,11 @@ module.exports = function (chai, util) {
|
|||
} else if ('string' === typeof exp) {
|
||||
obj.to.not.contain.string(inc);
|
||||
} else {
|
||||
throw new chai.AssertionError({
|
||||
message: 'expected an array or string'
|
||||
, stackStartFunction: assert.include
|
||||
});
|
||||
throw new chai.AssertionError(
|
||||
'expected an array or string'
|
||||
, null
|
||||
, assert.notInclude
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
@ -40,9 +40,4 @@ suite('configuration', function () {
|
|||
}
|
||||
}
|
||||
});
|
||||
|
||||
test('AssertionError Properties', function () {
|
||||
var err = new chai.AssertionError({ message: 'Chai!' });
|
||||
assert.equal(err.toString(), 'Chai!');
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue