mirror of
https://github.com/chaijs/chai
synced 2024-11-15 08:17:14 +00:00
574 lines
13 KiB
JavaScript
574 lines
13 KiB
JavaScript
/*!
|
|
* chai
|
|
* Copyright(c) 2011 Jake Luer <jake@alogicalparadox.com>
|
|
* MIT Licensed
|
|
*/
|
|
|
|
/**
|
|
* ### TDD Style Introduction
|
|
*
|
|
* The TDD style is exposed through `assert` interfaces. This provides
|
|
* the classic assert.`test` notation, similiar to that packaged with
|
|
* node.js. This assert module, however, provides several additional
|
|
* tests and is browser compatible.
|
|
*
|
|
* // assert
|
|
* var assert = require('chai').assert;
|
|
* , foo = 'bar';
|
|
*
|
|
* assert.typeOf(foo, 'string');
|
|
* assert.equal(foo, 'bar');
|
|
*
|
|
* #### Configuration
|
|
*
|
|
* By default, Chai does not show stack traces upon an AssertionError. This can
|
|
* be changed by modifying the `includeStack` parameter for chai.Assertion. For example:
|
|
*
|
|
* var chai = require('chai');
|
|
* chai.Assertion.includeStack = true; // defaults to false
|
|
*/
|
|
|
|
module.exports = function (chai) {
|
|
/*!
|
|
* Chai dependencies.
|
|
*/
|
|
var Assertion = chai.Assertion
|
|
, inspect = chai.inspect;
|
|
|
|
/*!
|
|
* Module export.
|
|
*/
|
|
|
|
var assert = chai.assert = {};
|
|
|
|
/**
|
|
* # .ok(object, [message])
|
|
*
|
|
* Assert object is truthy.
|
|
*
|
|
* assert.ok('everthing', 'everything is ok');
|
|
* assert.ok(false, 'this will fail');
|
|
*
|
|
* @name ok
|
|
* @param {*} object to test
|
|
* @param {String} message
|
|
* @api public
|
|
*/
|
|
|
|
assert.ok = function (val, msg) {
|
|
new Assertion(val, msg).is.ok;
|
|
};
|
|
|
|
/**
|
|
* # .equal(actual, expected, [message])
|
|
*
|
|
* Assert strict equality.
|
|
*
|
|
* assert.equal(3, 3, 'these numbers are equal');
|
|
*
|
|
* @name equal
|
|
* @param {*} actual
|
|
* @param {*} expected
|
|
* @param {String} message
|
|
* @api public
|
|
*/
|
|
|
|
assert.equal = function (act, exp, msg) {
|
|
var test = new Assertion(act, msg);
|
|
|
|
test.assert(
|
|
exp == test.obj
|
|
, 'expected ' + test.inspect + ' to equal ' + inspect(exp)
|
|
, 'expected ' + test.inspect + ' to not equal ' + inspect(exp));
|
|
};
|
|
|
|
/**
|
|
* # .notEqual(actual, expected, [message])
|
|
*
|
|
* Assert not equal.
|
|
*
|
|
* assert.notEqual(3, 4, 'these numbers are not equal');
|
|
*
|
|
* @name notEqual
|
|
* @param {*} actual
|
|
* @param {*} expected
|
|
* @param {String} message
|
|
* @api public
|
|
*/
|
|
|
|
assert.notEqual = function (act, exp, msg) {
|
|
var test = new Assertion(act, msg);
|
|
|
|
test.assert(
|
|
exp != test.obj
|
|
, 'expected ' + test.inspect + ' to equal ' + inspect(exp)
|
|
, 'expected ' + test.inspect + ' to not equal ' + inspect(exp));
|
|
};
|
|
|
|
/**
|
|
* # .strictEqual(actual, expected, [message])
|
|
*
|
|
* Assert strict equality.
|
|
*
|
|
* assert.strictEqual(true, true, 'these booleans are strictly equal');
|
|
*
|
|
* @name strictEqual
|
|
* @param {*} actual
|
|
* @param {*} expected
|
|
* @param {String} message
|
|
* @api public
|
|
*/
|
|
|
|
assert.strictEqual = function (act, exp, msg) {
|
|
new Assertion(act, msg).to.equal(exp);
|
|
};
|
|
|
|
/**
|
|
* # .notStrictEqual(actual, expected, [message])
|
|
*
|
|
* Assert strict equality.
|
|
*
|
|
* assert.notStrictEqual(1, true, 'these booleans are not strictly equal');
|
|
*
|
|
* @name notStrictEqual
|
|
* @param {*} actual
|
|
* @param {*} expected
|
|
* @param {String} message
|
|
* @api public
|
|
*/
|
|
|
|
assert.notStrictEqual = function (act, exp, msg) {
|
|
new Assertion(act, msg).to.not.equal(exp);
|
|
};
|
|
|
|
/**
|
|
* # .deepEqual(actual, expected, [message])
|
|
*
|
|
* Assert not deep equality.
|
|
*
|
|
* assert.deepEqual({ tea: 'green' }, { tea: 'green' });
|
|
*
|
|
* @name deepEqual
|
|
* @param {*} actual
|
|
* @param {*} expected
|
|
* @param {String} message
|
|
* @api public
|
|
*/
|
|
|
|
assert.deepEqual = function (act, exp, msg) {
|
|
new Assertion(act, msg).to.eql(exp);
|
|
};
|
|
|
|
/**
|
|
* # .notDeepEqual(actual, expected, [message])
|
|
*
|
|
* Assert not deep equality.
|
|
*
|
|
* assert.notDeepEqual({ tea: 'green' }, { tea: 'jasmine' });
|
|
*
|
|
* @name notDeepEqual
|
|
* @param {*} actual
|
|
* @param {*} expected
|
|
* @param {String} message
|
|
* @api public
|
|
*/
|
|
|
|
assert.notDeepEqual = function (act, exp, msg) {
|
|
new Assertion(act, msg).to.not.eql(exp);
|
|
};
|
|
|
|
/**
|
|
* # .isTrue(value, [message])
|
|
*
|
|
* Assert `value` is true.
|
|
*
|
|
* var tea_served = true;
|
|
* assert.isTrue(tea_served, 'the tea has been served');
|
|
*
|
|
* @name isTrue
|
|
* @param {Boolean} value
|
|
* @param {String} message
|
|
* @api public
|
|
*/
|
|
|
|
assert.isTrue = function (val, msg) {
|
|
new Assertion(val, msg).is.true;
|
|
};
|
|
|
|
/**
|
|
* # .isFalse(value, [message])
|
|
*
|
|
* Assert `value` is false.
|
|
*
|
|
* var tea_served = false;
|
|
* assert.isFalse(tea_served, 'no tea yet? hmm...');
|
|
*
|
|
* @name isFalse
|
|
* @param {Boolean} value
|
|
* @param {String} message
|
|
* @api public
|
|
*/
|
|
|
|
assert.isFalse = function (val, msg) {
|
|
new Assertion(val, msg).is.false;
|
|
};
|
|
|
|
/**
|
|
* # .isNull(value, [message])
|
|
*
|
|
* Assert `value` is null.
|
|
*
|
|
* assert.isNull(err, 'no errors');
|
|
*
|
|
* @name isNull
|
|
* @param {*} value
|
|
* @param {String} message
|
|
* @api public
|
|
*/
|
|
|
|
assert.isNull = function (val, msg) {
|
|
new Assertion(val, msg).to.equal(null);
|
|
};
|
|
|
|
/**
|
|
* # .isNotNull(value, [message])
|
|
*
|
|
* Assert `value` is not null.
|
|
*
|
|
* var tea = 'tasty chai';
|
|
* assert.isNotNull(tea, 'great, time for tea!');
|
|
*
|
|
* @name isNotNull
|
|
* @param {*} value
|
|
* @param {String} message
|
|
* @api public
|
|
*/
|
|
|
|
assert.isNotNull = function (val, msg) {
|
|
new Assertion(val, msg).to.not.equal(null);
|
|
};
|
|
|
|
/**
|
|
* # .isUndefined(value, [message])
|
|
*
|
|
* Assert `value` is undefined.
|
|
*
|
|
* assert.isUndefined(tea, 'no tea defined');
|
|
*
|
|
* @name isUndefined
|
|
* @param {*} value
|
|
* @param {String} message
|
|
* @api public
|
|
*/
|
|
|
|
assert.isUndefined = function (val, msg) {
|
|
new Assertion(val, msg).to.equal(undefined);
|
|
};
|
|
|
|
/**
|
|
* # .isFunction(value, [message])
|
|
*
|
|
* Assert `value` is a function.
|
|
*
|
|
* var serve_tea = function () { return 'cup of tea'; };
|
|
* assert.isFunction(serve_tea, 'great, we can have tea now');
|
|
*
|
|
* @name isFunction
|
|
* @param {Function} value
|
|
* @param {String} message
|
|
* @api public
|
|
*/
|
|
|
|
assert.isFunction = function (val, msg) {
|
|
new Assertion(val, msg).to.be.a('function');
|
|
};
|
|
|
|
/**
|
|
* # .isObject(value, [message])
|
|
*
|
|
* Assert `value` is an object.
|
|
*
|
|
* var selection = { name: 'Chai', serve: 'with spices' };
|
|
* assert.isObject(selection, 'tea selection is an object');
|
|
*
|
|
* @name isObject
|
|
* @param {Object} value
|
|
* @param {String} message
|
|
* @api public
|
|
*/
|
|
|
|
assert.isObject = function (val, msg) {
|
|
new Assertion(val, msg).to.be.a('object');
|
|
};
|
|
|
|
/**
|
|
* # .isArray(value, [message])
|
|
*
|
|
* Assert `value` is an instance of Array.
|
|
*
|
|
* var menu = [ 'green', 'chai', 'oolong' ];
|
|
* assert.isArray(menu, 'what kind of tea do we want?');
|
|
*
|
|
* @name isArray
|
|
* @param {*} value
|
|
* @param {String} message
|
|
* @api public
|
|
*/
|
|
|
|
assert.isArray = function (val, msg) {
|
|
new Assertion(val, msg).to.be.instanceof(Array);
|
|
};
|
|
|
|
/**
|
|
* # .isString(value, [message])
|
|
*
|
|
* Assert `value` is a string.
|
|
*
|
|
* var teaorder = 'chai';
|
|
* assert.isString(tea_order, 'order placed');
|
|
*
|
|
* @name isString
|
|
* @param {String} value
|
|
* @param {String} message
|
|
* @api public
|
|
*/
|
|
|
|
assert.isString = function (val, msg) {
|
|
new Assertion(val, msg).to.be.a('string');
|
|
};
|
|
|
|
/**
|
|
* # .isNumber(value, [message])
|
|
*
|
|
* Assert `value` is a number
|
|
*
|
|
* var cups = 2;
|
|
* assert.isNumber(cups, 'how many cups');
|
|
*
|
|
* @name isNumber
|
|
* @param {Number} value
|
|
* @param {String} message
|
|
* @api public
|
|
*/
|
|
|
|
assert.isNumber = function (val, msg) {
|
|
new Assertion(val, msg).to.be.a('number');
|
|
};
|
|
|
|
/**
|
|
* # .isBoolean(value, [message])
|
|
*
|
|
* Assert `value` is a boolean
|
|
*
|
|
* var teaready = true
|
|
* , teaserved = false;
|
|
*
|
|
* assert.isBoolean(tea_ready, 'is the tea ready');
|
|
* assert.isBoolean(tea_served, 'has tea been served');
|
|
*
|
|
* @name isBoolean
|
|
* @param {*} value
|
|
* @param {String} message
|
|
* @api public
|
|
*/
|
|
|
|
assert.isBoolean = function (val, msg) {
|
|
new Assertion(val, msg).to.be.a('boolean');
|
|
};
|
|
|
|
/**
|
|
* # .typeOf(value, name, [message])
|
|
*
|
|
* Assert typeof `value` is `name`.
|
|
*
|
|
* assert.typeOf('tea', 'string', 'we have a string');
|
|
*
|
|
* @name typeOf
|
|
* @param {*} value
|
|
* @param {String} typeof name
|
|
* @param {String} message
|
|
* @api public
|
|
*/
|
|
|
|
assert.typeOf = function (val, type, msg) {
|
|
new Assertion(val, msg).to.be.a(type);
|
|
};
|
|
|
|
/**
|
|
* # .instanceOf(object, constructor, [message])
|
|
*
|
|
* Assert `value` is instanceof `constructor`.
|
|
*
|
|
* var Tea = function (name) { this.name = name; }
|
|
* , Chai = new Tea('chai');
|
|
*
|
|
* assert.instanceOf(Chai, Tea, 'chai is an instance of tea');
|
|
*
|
|
* @name instanceOf
|
|
* @param {Object} object
|
|
* @param {Constructor} constructor
|
|
* @param {String} message
|
|
* @api public
|
|
*/
|
|
|
|
assert.instanceOf = function (val, type, msg) {
|
|
new Assertion(val, msg).to.be.instanceof(type);
|
|
};
|
|
|
|
/**
|
|
* # .include(value, includes, [message])
|
|
*
|
|
* Assert the inclusion of an object in another. Works
|
|
* for strings and arrays.
|
|
*
|
|
* assert.include('foobar', 'bar', 'foobar contains string `var`);
|
|
* assert.include([ 1, 2, 3], 3, 'array contains value);
|
|
*
|
|
* @name include
|
|
* @param {Array|String} value
|
|
* @param {*} includes
|
|
* @param {String} message
|
|
* @api public
|
|
*/
|
|
|
|
assert.include = function (exp, inc, msg) {
|
|
var obj = new Assertion(exp, msg);
|
|
|
|
if (Array.isArray(exp)) {
|
|
obj.to.include(inc);
|
|
} else if ('string' === typeof exp) {
|
|
obj.to.contain.string(inc);
|
|
}
|
|
};
|
|
|
|
/**
|
|
* # .match(value, regex, [message])
|
|
*
|
|
* Assert that `value` matches regular expression.
|
|
*
|
|
* assert.match('foobar', /^foo/, 'Regexp matches');
|
|
*
|
|
* @name match
|
|
* @param {*} value
|
|
* @param {RegExp} RegularExpression
|
|
* @param {String} message
|
|
* @api public
|
|
*/
|
|
|
|
assert.match = function (exp, re, msg) {
|
|
new Assertion(exp, msg).to.match(re);
|
|
};
|
|
|
|
/**
|
|
* # .length(value, constructor, [message])
|
|
*
|
|
* Assert that object has expected length.
|
|
*
|
|
* assert.length([1,2,3], 3, 'Array has length of 3');
|
|
* assert.length('foobar', 5, 'String has length of 6');
|
|
*
|
|
* @name length
|
|
* @param {*} value
|
|
* @param {Number} length
|
|
* @param {String} message
|
|
* @api public
|
|
*/
|
|
|
|
assert.length = function (exp, len, msg) {
|
|
new Assertion(exp, msg).to.have.length(len);
|
|
};
|
|
|
|
/**
|
|
* # .throws(function, [constructor/regexp], [message])
|
|
*
|
|
* Assert that a function will throw a specific
|
|
* type of error.
|
|
*
|
|
* assert.throw(fn, ReferenceError, 'function throw reference error');
|
|
*
|
|
* @name throws
|
|
* @alias throw
|
|
* @param {Function} function to test
|
|
* @param {ErrorConstructor} constructor
|
|
* @param {String} message
|
|
* @see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Error#Error_types
|
|
* @api public
|
|
*/
|
|
|
|
assert.throws = function (fn, type, msg) {
|
|
if ('string' === typeof type) {
|
|
msg = type;
|
|
type = null;
|
|
}
|
|
|
|
new Assertion(fn, msg).to.throw(type);
|
|
};
|
|
|
|
/**
|
|
* # .doesNotThrow(function, [constructor/regexp], [message])
|
|
*
|
|
* Assert that a function will throw a specific
|
|
* type of error.
|
|
*
|
|
* var fn = function (err) { if (err) throw Error(err) };
|
|
* assert.doesNotThrow(fn, Error, 'function throw reference error');
|
|
*
|
|
* @name doesNotThrow
|
|
* @param {Function} function to test
|
|
* @param {ErrorConstructor} constructor
|
|
* @param {String} message
|
|
* @see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Error#Error_types
|
|
* @api public
|
|
*/
|
|
|
|
assert.doesNotThrow = function (fn, type, msg) {
|
|
if ('string' === typeof type) {
|
|
msg = type;
|
|
type = null;
|
|
}
|
|
|
|
new Assertion(fn, msg).to.not.throw(type);
|
|
};
|
|
|
|
/**
|
|
* # .operator(val, operator, val2, [message])
|
|
*
|
|
* Compare two values using operator.
|
|
*
|
|
* assert.operator(1, '<', 2, 'everything is ok');
|
|
* assert.operator(1, '>', 2, 'this will fail');
|
|
*
|
|
* @name operator
|
|
* @param {*} object to test
|
|
* @param {String} operator
|
|
* @param {*} second object
|
|
* @param {String} message
|
|
* @api public
|
|
*/
|
|
|
|
assert.operator = function (val, operator, val2, msg) {
|
|
if (!~['==', '===', '>', '>=', '<', '<=', '!=', '!=='].indexOf(operator)) {
|
|
throw new Error('Invalid operator "' + operator + '"');
|
|
}
|
|
new Assertion(eval(val + operator + val2), msg).to.be.true;
|
|
};
|
|
|
|
/*!
|
|
* Undocumented / untested
|
|
*/
|
|
|
|
assert.ifError = function (val, msg) {
|
|
new Assertion(val, msg).to.not.be.ok;
|
|
};
|
|
|
|
/*!
|
|
* Aliases.
|
|
*/
|
|
|
|
(function alias(name, as){
|
|
assert[as] = assert[name];
|
|
return alias;
|
|
})
|
|
('length', 'lengthOf')
|
|
('throws', 'throw');
|
|
};
|