chai/lib/interface/should.js
Jake Luer 1082fc6544 cleaner should interface export.
- also aliased `chai.Should()` as the original would be overwritten after the first call (helps when using watch mode for test runners)
2012-05-04 17:55:11 -04:00

62 lines
1.4 KiB
JavaScript

/*!
* chai
* Copyright(c) 2011-2012 Jake Luer <jake@alogicalparadox.com>
* MIT Licensed
*/
module.exports = function (chai, util) {
var Assertion = chai.Assertion;
function loadShould () {
// modify Object.prototype to have `should`
Object.defineProperty(Object.prototype, 'should',
{ set: function () {}
, get: function(){
if (this instanceof String || this instanceof Number) {
return new Assertion(this.constructor(this));
} else if (this instanceof Boolean) {
return new Assertion(this == true);
}
return new Assertion(this);
}
, configurable: true
});
var should = {};
should.equal = function (val1, val2) {
new Assertion(val1).to.equal(val2);
};
should.Throw = function (fn, errt, errs) {
new Assertion(fn).to.Throw(errt, errs);
};
should.exist = function (val) {
new Assertion(val).to.exist;
}
// negation
should.not = {}
should.not.equal = function (val1, val2) {
new Assertion(val1).to.not.equal(val2);
};
should.not.Throw = function (fn, errt, errs) {
new Assertion(fn).to.not.Throw(errt, errs);
};
should.not.exist = function (val) {
new Assertion(val).to.not.exist;
}
should['throw'] = should['Throw'];
should.not['throw'] = should.not['Throw'];
return should;
};
chai.should = loadShould;
chai.Should = loadShould;
};