Comparing version 0.1.0 to 0.1.1
499
chai.js
@@ -61,2 +61,30 @@ | ||
/** | ||
* ### BDD Style Introduction | ||
* | ||
* The BDD style is exposed through `expect` or `should` interfaces. In both | ||
* scenarios, you chain together natural language assertions. | ||
* | ||
* // expect | ||
* var expect = require('chai').expect; | ||
* expect(foo).to.equal('bar'); | ||
* | ||
* // should | ||
* var should = require('chai').should(); | ||
* foo.should.equal('bar'); | ||
* | ||
* #### Differences | ||
* | ||
* The `expect` interface provides a function as a starting point for chaining | ||
* your language assertions. It works on both node.js and in the browser. | ||
* | ||
* The `should` interface extends `Object.prototype` to provide a single getter as | ||
* the starting point for your language assertions. Most browser don't like | ||
* extensions to `Object.prototype` so it is not recommended for browser use. | ||
*/ | ||
/*! | ||
* Module dependencies. | ||
*/ | ||
var AssertionError = require('./error') | ||
@@ -66,4 +94,16 @@ , eql = require('./utils/eql') | ||
/*! | ||
* Module export. | ||
*/ | ||
module.exports = Assertion; | ||
/*! | ||
* # Assertion Constructor | ||
* | ||
* Creates object for chaining. | ||
* | ||
* @api private | ||
*/ | ||
function Assertion (obj, msg) { | ||
@@ -74,2 +114,15 @@ this.obj = obj; | ||
/*! | ||
* # .assert(expression, message, negateMessage) | ||
* | ||
* Executes an expression and check expectations. | ||
* Throws AssertionError for reporting. | ||
* | ||
* @name assert | ||
* @param {Philosophical} expression to be tested | ||
* @param {String} message to display if fails | ||
* @param {String} negatedMessage to display if negated expression fails | ||
* @api privage | ||
*/ | ||
Assertion.prototype.assert = function (expr, msg, negateMsg) { | ||
@@ -87,2 +140,11 @@ var msg = (this.msg ? this.msg + ': ' : '') + (this.negate ? negateMsg : msg) | ||
/*! | ||
* # inpsect | ||
* | ||
* Returns the current object stringified. | ||
* | ||
* @name inspect | ||
* @api private | ||
*/ | ||
Assertion.prototype.__defineGetter__('inspect', function () { | ||
@@ -92,11 +154,11 @@ return inspect(this.obj); | ||
Assertion.prototype.__defineGetter__('arguments', function () { | ||
this.assert( | ||
'[object Arguments]' == Object.prototype.toString.call(this.obj) | ||
, 'expected ' + this.inspect + ' to be arguments' | ||
, 'expected ' + this.inspect + ' to not be arguments'); | ||
/** | ||
* # to | ||
* | ||
* Language chain. | ||
* | ||
* @name to | ||
* @api public | ||
*/ | ||
return this; | ||
}); | ||
Assertion.prototype.__defineGetter__('to', function () { | ||
@@ -106,2 +168,11 @@ return this; | ||
/** | ||
* # be | ||
* | ||
* Language chain. | ||
* | ||
* @name be | ||
* @api public | ||
*/ | ||
Assertion.prototype.__defineGetter__('be', function () { | ||
@@ -111,2 +182,11 @@ return this; | ||
/** | ||
* # an | ||
* | ||
* Language chain. | ||
* | ||
* @name an | ||
* @api public | ||
*/ | ||
Assertion.prototype.__defineGetter__('an', function () { | ||
@@ -116,2 +196,11 @@ return this; | ||
/** | ||
* # is | ||
* | ||
* Language chain. | ||
* | ||
* @name is | ||
* @api public | ||
*/ | ||
Assertion.prototype.__defineGetter__('is', function () { | ||
@@ -121,2 +210,11 @@ return this; | ||
/** | ||
* # and | ||
* | ||
* Language chain. | ||
* | ||
* @name and | ||
* @api public | ||
*/ | ||
Assertion.prototype.__defineGetter__('and', function () { | ||
@@ -126,2 +224,11 @@ return this; | ||
/** | ||
* # have | ||
* | ||
* Language chain. | ||
* | ||
* @name have | ||
* @api public | ||
*/ | ||
Assertion.prototype.__defineGetter__('have', function () { | ||
@@ -131,6 +238,10 @@ return this; | ||
Assertion.prototype.__defineGetter__('include', function () { | ||
this.includes = true; | ||
return this; | ||
}); | ||
/** | ||
* # with | ||
* | ||
* Language chain. | ||
* | ||
* @name with | ||
* @api public | ||
*/ | ||
@@ -141,2 +252,11 @@ Assertion.prototype.__defineGetter__('with', function () { | ||
/** | ||
* # .not | ||
* | ||
* Negates any of assertions following in the chain. | ||
* | ||
* @name not | ||
* @api public | ||
*/ | ||
Assertion.prototype.__defineGetter__('not', function () { | ||
@@ -147,2 +267,16 @@ this.negate = true; | ||
/** | ||
* # .ok | ||
* | ||
* Assert object truthiness. | ||
* | ||
* expect('everthing').to.be.ok; | ||
* expect(false).to.not.be.ok; | ||
* expect(undefined).to.not.be.ok; | ||
* expect(null).to.not.be.ok; | ||
* | ||
* @name ok | ||
* @api public | ||
*/ | ||
Assertion.prototype.__defineGetter__('ok', function () { | ||
@@ -157,2 +291,11 @@ this.assert( | ||
/** | ||
* # .true | ||
* | ||
* Assert object is true | ||
* | ||
* @name true | ||
* @api public | ||
*/ | ||
Assertion.prototype.__defineGetter__('true', function () { | ||
@@ -167,2 +310,11 @@ this.assert( | ||
/** | ||
* # .false | ||
* | ||
* Assert object is false | ||
* | ||
* @name false | ||
* @api public | ||
*/ | ||
Assertion.prototype.__defineGetter__('false', function () { | ||
@@ -177,2 +329,16 @@ this.assert( | ||
/** | ||
* # .exist | ||
* | ||
* Assert object exists (null). | ||
* | ||
* var foo = 'hi' | ||
* , bar; | ||
* expect(foo).to.exist; | ||
* expect(bar).to.not.exist; | ||
* | ||
* @name exist | ||
* @api public | ||
*/ | ||
Assertion.prototype.__defineGetter__('exist', function () { | ||
@@ -187,2 +353,13 @@ this.assert( | ||
/** | ||
* # .empty | ||
* | ||
* Assert object's length to be 0. | ||
* | ||
* expect([]).to.be.empty; | ||
* | ||
* @name empty | ||
* @api public | ||
*/ | ||
Assertion.prototype.__defineGetter__('empty', function () { | ||
@@ -199,2 +376,36 @@ new Assertion(this.obj).to.have.property('length'); | ||
/** | ||
* # .arguments | ||
* | ||
* Assert object is an instanceof arguments. | ||
* | ||
* function test () { | ||
* expect(arguments).to.be.arguments; | ||
* } | ||
* | ||
* @name arguments | ||
* @api public | ||
*/ | ||
Assertion.prototype.__defineGetter__('arguments', function () { | ||
this.assert( | ||
'[object Arguments]' == Object.prototype.toString.call(this.obj) | ||
, 'expected ' + this.inspect + ' to be arguments' | ||
, 'expected ' + this.inspect + ' to not be arguments'); | ||
return this; | ||
}); | ||
/** | ||
* # .equal(value) | ||
* | ||
* Assert strict equality. | ||
* | ||
* expect('hello').to.equal('hello'); | ||
* | ||
* @name equal | ||
* @param {*} value | ||
* @api public | ||
*/ | ||
Assertion.prototype.equal = function (val) { | ||
@@ -209,2 +420,14 @@ this.assert( | ||
/** | ||
* # .eql(value) | ||
* | ||
* Assert deep equality. | ||
* | ||
* expect({ foo: 'bar' }).to.eql({ foo: 'bar' }); | ||
* | ||
* @name eql | ||
* @param {*} value | ||
* @api public | ||
*/ | ||
Assertion.prototype.eql = function (obj) { | ||
@@ -218,2 +441,14 @@ this.assert( | ||
/** | ||
* # .above(value) | ||
* | ||
* Assert greater than `value`. | ||
* | ||
* expect(10).to.be.above(5); | ||
* | ||
* @name above | ||
* @param {Number} value | ||
* @api public | ||
*/ | ||
Assertion.prototype.above = function (val) { | ||
@@ -228,2 +463,14 @@ this.assert( | ||
/** | ||
* # .below(value) | ||
* | ||
* Assert less than `value`. | ||
* | ||
* expect(5).to.be.below(10); | ||
* | ||
* @name below | ||
* @param {Number} value | ||
* @api public | ||
*/ | ||
Assertion.prototype.below = function (val) { | ||
@@ -238,2 +485,38 @@ this.assert( | ||
/** | ||
* # .within(start, finish) | ||
* | ||
* Assert that a number is within a range. | ||
* | ||
* expect(7).to.be.within(5,10); | ||
* | ||
* @name within | ||
* @param {Number} start lowerbound inclusive | ||
* @param {Number} finish upperbound inclusive | ||
* @api public | ||
*/ | ||
Assertion.prototype.within = function (start, finish) { | ||
var range = start + '..' + finish; | ||
this.assert( | ||
this.obj >= start && this.obj <= finish | ||
, 'expected ' + this.inspect + ' to be within ' + range | ||
, 'expected ' + this.inspect + ' to not be within ' + range); | ||
return this; | ||
}; | ||
/** | ||
* # .a(type) | ||
* | ||
* Assert typeof. | ||
* | ||
* expect('test').to.be.a('string'); | ||
* | ||
* @name a | ||
* @param {String} type | ||
* @api public | ||
*/ | ||
Assertion.prototype.a = function (type) { | ||
@@ -248,2 +531,15 @@ this.assert( | ||
/** | ||
* # .instanceOf(constructor) | ||
* | ||
* Assert instanceof. | ||
* | ||
* expect(42).to.be.instanceof(Number); | ||
* expect([4,2]).to.be.instanceof(Array); | ||
* | ||
* @name instanceOf | ||
* @param {Constructor} | ||
* @api public | ||
*/ | ||
Assertion.prototype.instanceof = function (constructor) { | ||
@@ -259,2 +555,15 @@ var name = constructor.name; | ||
/** | ||
* # respondTo(method) | ||
* | ||
* Assert that `method` is a function. | ||
* | ||
* var res = { send: function () {} }; | ||
* expect(res).to.respondTo('send'); | ||
* | ||
* @name respondTo | ||
* @param {String} method name | ||
* @api public | ||
*/ | ||
Assertion.prototype.respondTo = function (method) { | ||
@@ -269,2 +578,20 @@ this.assert( | ||
/** | ||
* # .property(name, [value]) | ||
* | ||
* Assert that property of `name` exists, | ||
* optionally with `value`. | ||
* | ||
* var obj = { foo: 'bar' } | ||
* expect(obj).to.have.property('foo'); | ||
* expect(obj).to.have.property('foo', 'bar'); | ||
* expect(obj).to.have.property('foo').to.be.a('string'); | ||
* | ||
* @name property | ||
* @param {String} name | ||
* @param {*} value (optional) | ||
* @returns value of property for chaining | ||
* @api public | ||
*/ | ||
Assertion.prototype.property = function (name, val) { | ||
@@ -294,2 +621,15 @@ if (this.negate && undefined !== val) { | ||
/** | ||
* # .ownProperty(name) | ||
* | ||
* Assert that has own property by `name`. | ||
* | ||
* expect('test').to.have.ownProperty('length'); | ||
* | ||
* @name ownProperty | ||
* @alias haveOwnProperty | ||
* @param {String} name | ||
* @api public | ||
*/ | ||
Assertion.prototype.ownProperty = function (name) { | ||
@@ -303,2 +643,16 @@ this.assert( | ||
/** | ||
* # .length(val) | ||
* | ||
* Assert that object has expected length. | ||
* | ||
* expect([1,2,3]).to.have.length(3); | ||
* expect('foobar').to.have.length(6); | ||
* | ||
* @name length | ||
* @alias lengthOf | ||
* @param {Number} length | ||
* @api public | ||
*/ | ||
Assertion.prototype.length = function (n) { | ||
@@ -316,2 +670,14 @@ new Assertion(this.obj).to.have.property('length'); | ||
/** | ||
* # .match(regexp) | ||
* | ||
* Assert that matches regular expression. | ||
* | ||
* expect('foobar').to.match(/^foo/); | ||
* | ||
* @name match | ||
* @param {RegExp} RegularExpression | ||
* @api public | ||
*/ | ||
Assertion.prototype.match = function (re) { | ||
@@ -326,2 +692,14 @@ this.assert( | ||
/** | ||
* # .contain(obj) | ||
* | ||
* Assert the inclusion of an object in an Array | ||
* | ||
* expect([1,2,3]).to.contain(2); | ||
* | ||
* @name contain | ||
* @param {Object|String|Number} obj | ||
* @api public | ||
*/ | ||
Assertion.prototype.contain = function (obj) { | ||
@@ -338,22 +716,14 @@ new Assertion(this.obj).to.be.an.instanceof(Array); | ||
Assertion.prototype.within = function (start, finish) { | ||
var range = start + '..' + finish; | ||
/** | ||
* # .string(string) | ||
* | ||
* Assert inclusion of string in string. | ||
* | ||
* expect('foobar').to.include.string('bar'); | ||
* | ||
* @name string | ||
* @param {String} string | ||
* @api public | ||
*/ | ||
this.assert( | ||
this.obj >= start && this.obj <= finish | ||
, 'expected ' + this.inspect + ' to be within ' + range | ||
, 'expected ' + this.inspect + ' to not be within ' + range); | ||
return this; | ||
}; | ||
Assertion.prototype.greaterThan = function (val) { | ||
this.assert( | ||
this.obj > val | ||
, 'expected ' + this.inspect + ' to be greater than ' + inspect(val) | ||
, 'expected ' + this.inspect + ' to not be greater than ' + inspect(val)); | ||
return this; | ||
}; | ||
Assertion.prototype.string = function (str) { | ||
@@ -370,2 +740,15 @@ new Assertion(this.obj).is.a('string'); | ||
/** | ||
* # .object(object) | ||
* | ||
* Assert inclusion of object in object. | ||
* | ||
* var obj = {foo: 'bar', baz: {baaz: 42}, qux: 13}; | ||
* expect(obj).to.include.object({foo: 'bar'}); | ||
* | ||
* @name object | ||
* @param {Object} object | ||
* @api public | ||
*/ | ||
Assertion.prototype.object = function(obj){ | ||
@@ -391,2 +774,31 @@ new Assertion(this.obj).is.a('object'); | ||
/** | ||
* # include | ||
* | ||
* Language chain that lags #keys to test for inclusion testing. | ||
* | ||
* @name include | ||
* @api public | ||
*/ | ||
Assertion.prototype.__defineGetter__('include', function () { | ||
this.includes = true; | ||
return this; | ||
}); | ||
/** | ||
* # .keys(key1, [key2], [...]) | ||
* | ||
* Assert exact keys or the inclusing of keys using | ||
* the include modifier. | ||
* | ||
* expect({ foo: 1, bar: 2 }).to.have.keys(['foo', 'bar']); | ||
* expect({ foo: 1, bar: 2, baz: 3 }).to.include.keys('foo', 'bar'); | ||
* | ||
* @name keys | ||
* @alias key | ||
* @param {String|Array} Keys | ||
* @api public | ||
*/ | ||
Assertion.prototype.keys = function(keys) { | ||
@@ -441,2 +853,18 @@ var str | ||
/** | ||
* # .throw(constructor) | ||
* | ||
* Assert that a function will throw a specific | ||
* type of error. | ||
* | ||
* var fn = function () { throw new ReferenceError(''); } | ||
* expect(fn).to.throw(ReferenceError); | ||
* | ||
* @name throw | ||
* @alias throws | ||
* @param {ErrorConstructor} constructor | ||
* @see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Error#Error_types | ||
* @api public | ||
*/ | ||
Assertion.prototype.throw = function (constructor) { | ||
@@ -466,3 +894,3 @@ new Assertion(this.obj).is.a('function'); | ||
/** | ||
/*! | ||
* Aliases. | ||
@@ -479,3 +907,4 @@ */ | ||
('above', 'greaterThan') | ||
('below', 'lessThan'); | ||
('below', 'lessThan') | ||
('throw', 'throws'); | ||
}); // module: assertion.js | ||
@@ -492,3 +921,3 @@ | ||
exports.version = '0.1.0'; | ||
exports.version = '0.1.1'; | ||
@@ -511,3 +940,3 @@ exports.expect = require('./interface/expect'); | ||
}); | ||
} | ||
}; | ||
}); // module: chai.js | ||
@@ -514,0 +943,0 @@ |
0.1.1 / 2011-12-16 | ||
================== | ||
* docs ready for upcoming 0.1.1 | ||
* readme image fixed [ci skip] | ||
* more readme tweaks [ci skip] | ||
* réadmet image fixed [ci skip] | ||
* documentation | ||
* codex locked in version 0.0.5 | ||
* more comments to assertions for docs | ||
* assertions fully commented, browser library updated | ||
* adding codex as doc dependancy | ||
* prepping for docs | ||
* assertion component completely commented for documentation | ||
* added exist test | ||
* var expect outside of browser if check | ||
* added keywords to package.json | ||
0.1.0 / 2011-12-15 | ||
@@ -3,0 +21,0 @@ ================== |
@@ -12,2 +12,30 @@ /*! | ||
/** | ||
* ### BDD Style Introduction | ||
* | ||
* The BDD style is exposed through `expect` or `should` interfaces. In both | ||
* scenarios, you chain together natural language assertions. | ||
* | ||
* // expect | ||
* var expect = require('chai').expect; | ||
* expect(foo).to.equal('bar'); | ||
* | ||
* // should | ||
* var should = require('chai').should(); | ||
* foo.should.equal('bar'); | ||
* | ||
* #### Differences | ||
* | ||
* The `expect` interface provides a function as a starting point for chaining | ||
* your language assertions. It works on both node.js and in the browser. | ||
* | ||
* The `should` interface extends `Object.prototype` to provide a single getter as | ||
* the starting point for your language assertions. Most browser don't like | ||
* extensions to `Object.prototype` so it is not recommended for browser use. | ||
*/ | ||
/*! | ||
* Module dependencies. | ||
*/ | ||
var AssertionError = require('./error') | ||
@@ -17,4 +45,16 @@ , eql = require('./utils/eql') | ||
/*! | ||
* Module export. | ||
*/ | ||
module.exports = Assertion; | ||
/*! | ||
* # Assertion Constructor | ||
* | ||
* Creates object for chaining. | ||
* | ||
* @api private | ||
*/ | ||
function Assertion (obj, msg) { | ||
@@ -25,2 +65,15 @@ this.obj = obj; | ||
/*! | ||
* # .assert(expression, message, negateMessage) | ||
* | ||
* Executes an expression and check expectations. | ||
* Throws AssertionError for reporting. | ||
* | ||
* @name assert | ||
* @param {Philosophical} expression to be tested | ||
* @param {String} message to display if fails | ||
* @param {String} negatedMessage to display if negated expression fails | ||
* @api privage | ||
*/ | ||
Assertion.prototype.assert = function (expr, msg, negateMsg) { | ||
@@ -38,2 +91,11 @@ var msg = (this.msg ? this.msg + ': ' : '') + (this.negate ? negateMsg : msg) | ||
/*! | ||
* # inpsect | ||
* | ||
* Returns the current object stringified. | ||
* | ||
* @name inspect | ||
* @api private | ||
*/ | ||
Assertion.prototype.__defineGetter__('inspect', function () { | ||
@@ -43,11 +105,11 @@ return inspect(this.obj); | ||
Assertion.prototype.__defineGetter__('arguments', function () { | ||
this.assert( | ||
'[object Arguments]' == Object.prototype.toString.call(this.obj) | ||
, 'expected ' + this.inspect + ' to be arguments' | ||
, 'expected ' + this.inspect + ' to not be arguments'); | ||
/** | ||
* # to | ||
* | ||
* Language chain. | ||
* | ||
* @name to | ||
* @api public | ||
*/ | ||
return this; | ||
}); | ||
Assertion.prototype.__defineGetter__('to', function () { | ||
@@ -57,2 +119,11 @@ return this; | ||
/** | ||
* # be | ||
* | ||
* Language chain. | ||
* | ||
* @name be | ||
* @api public | ||
*/ | ||
Assertion.prototype.__defineGetter__('be', function () { | ||
@@ -62,2 +133,11 @@ return this; | ||
/** | ||
* # an | ||
* | ||
* Language chain. | ||
* | ||
* @name an | ||
* @api public | ||
*/ | ||
Assertion.prototype.__defineGetter__('an', function () { | ||
@@ -67,2 +147,11 @@ return this; | ||
/** | ||
* # is | ||
* | ||
* Language chain. | ||
* | ||
* @name is | ||
* @api public | ||
*/ | ||
Assertion.prototype.__defineGetter__('is', function () { | ||
@@ -72,2 +161,11 @@ return this; | ||
/** | ||
* # and | ||
* | ||
* Language chain. | ||
* | ||
* @name and | ||
* @api public | ||
*/ | ||
Assertion.prototype.__defineGetter__('and', function () { | ||
@@ -77,2 +175,11 @@ return this; | ||
/** | ||
* # have | ||
* | ||
* Language chain. | ||
* | ||
* @name have | ||
* @api public | ||
*/ | ||
Assertion.prototype.__defineGetter__('have', function () { | ||
@@ -82,6 +189,10 @@ return this; | ||
Assertion.prototype.__defineGetter__('include', function () { | ||
this.includes = true; | ||
return this; | ||
}); | ||
/** | ||
* # with | ||
* | ||
* Language chain. | ||
* | ||
* @name with | ||
* @api public | ||
*/ | ||
@@ -92,2 +203,11 @@ Assertion.prototype.__defineGetter__('with', function () { | ||
/** | ||
* # .not | ||
* | ||
* Negates any of assertions following in the chain. | ||
* | ||
* @name not | ||
* @api public | ||
*/ | ||
Assertion.prototype.__defineGetter__('not', function () { | ||
@@ -98,2 +218,16 @@ this.negate = true; | ||
/** | ||
* # .ok | ||
* | ||
* Assert object truthiness. | ||
* | ||
* expect('everthing').to.be.ok; | ||
* expect(false).to.not.be.ok; | ||
* expect(undefined).to.not.be.ok; | ||
* expect(null).to.not.be.ok; | ||
* | ||
* @name ok | ||
* @api public | ||
*/ | ||
Assertion.prototype.__defineGetter__('ok', function () { | ||
@@ -108,2 +242,11 @@ this.assert( | ||
/** | ||
* # .true | ||
* | ||
* Assert object is true | ||
* | ||
* @name true | ||
* @api public | ||
*/ | ||
Assertion.prototype.__defineGetter__('true', function () { | ||
@@ -118,2 +261,11 @@ this.assert( | ||
/** | ||
* # .false | ||
* | ||
* Assert object is false | ||
* | ||
* @name false | ||
* @api public | ||
*/ | ||
Assertion.prototype.__defineGetter__('false', function () { | ||
@@ -128,2 +280,16 @@ this.assert( | ||
/** | ||
* # .exist | ||
* | ||
* Assert object exists (null). | ||
* | ||
* var foo = 'hi' | ||
* , bar; | ||
* expect(foo).to.exist; | ||
* expect(bar).to.not.exist; | ||
* | ||
* @name exist | ||
* @api public | ||
*/ | ||
Assertion.prototype.__defineGetter__('exist', function () { | ||
@@ -138,2 +304,13 @@ this.assert( | ||
/** | ||
* # .empty | ||
* | ||
* Assert object's length to be 0. | ||
* | ||
* expect([]).to.be.empty; | ||
* | ||
* @name empty | ||
* @api public | ||
*/ | ||
Assertion.prototype.__defineGetter__('empty', function () { | ||
@@ -150,2 +327,36 @@ new Assertion(this.obj).to.have.property('length'); | ||
/** | ||
* # .arguments | ||
* | ||
* Assert object is an instanceof arguments. | ||
* | ||
* function test () { | ||
* expect(arguments).to.be.arguments; | ||
* } | ||
* | ||
* @name arguments | ||
* @api public | ||
*/ | ||
Assertion.prototype.__defineGetter__('arguments', function () { | ||
this.assert( | ||
'[object Arguments]' == Object.prototype.toString.call(this.obj) | ||
, 'expected ' + this.inspect + ' to be arguments' | ||
, 'expected ' + this.inspect + ' to not be arguments'); | ||
return this; | ||
}); | ||
/** | ||
* # .equal(value) | ||
* | ||
* Assert strict equality. | ||
* | ||
* expect('hello').to.equal('hello'); | ||
* | ||
* @name equal | ||
* @param {*} value | ||
* @api public | ||
*/ | ||
Assertion.prototype.equal = function (val) { | ||
@@ -160,2 +371,14 @@ this.assert( | ||
/** | ||
* # .eql(value) | ||
* | ||
* Assert deep equality. | ||
* | ||
* expect({ foo: 'bar' }).to.eql({ foo: 'bar' }); | ||
* | ||
* @name eql | ||
* @param {*} value | ||
* @api public | ||
*/ | ||
Assertion.prototype.eql = function (obj) { | ||
@@ -169,2 +392,14 @@ this.assert( | ||
/** | ||
* # .above(value) | ||
* | ||
* Assert greater than `value`. | ||
* | ||
* expect(10).to.be.above(5); | ||
* | ||
* @name above | ||
* @param {Number} value | ||
* @api public | ||
*/ | ||
Assertion.prototype.above = function (val) { | ||
@@ -179,2 +414,14 @@ this.assert( | ||
/** | ||
* # .below(value) | ||
* | ||
* Assert less than `value`. | ||
* | ||
* expect(5).to.be.below(10); | ||
* | ||
* @name below | ||
* @param {Number} value | ||
* @api public | ||
*/ | ||
Assertion.prototype.below = function (val) { | ||
@@ -189,2 +436,38 @@ this.assert( | ||
/** | ||
* # .within(start, finish) | ||
* | ||
* Assert that a number is within a range. | ||
* | ||
* expect(7).to.be.within(5,10); | ||
* | ||
* @name within | ||
* @param {Number} start lowerbound inclusive | ||
* @param {Number} finish upperbound inclusive | ||
* @api public | ||
*/ | ||
Assertion.prototype.within = function (start, finish) { | ||
var range = start + '..' + finish; | ||
this.assert( | ||
this.obj >= start && this.obj <= finish | ||
, 'expected ' + this.inspect + ' to be within ' + range | ||
, 'expected ' + this.inspect + ' to not be within ' + range); | ||
return this; | ||
}; | ||
/** | ||
* # .a(type) | ||
* | ||
* Assert typeof. | ||
* | ||
* expect('test').to.be.a('string'); | ||
* | ||
* @name a | ||
* @param {String} type | ||
* @api public | ||
*/ | ||
Assertion.prototype.a = function (type) { | ||
@@ -199,2 +482,15 @@ this.assert( | ||
/** | ||
* # .instanceOf(constructor) | ||
* | ||
* Assert instanceof. | ||
* | ||
* expect(42).to.be.instanceof(Number); | ||
* expect([4,2]).to.be.instanceof(Array); | ||
* | ||
* @name instanceOf | ||
* @param {Constructor} | ||
* @api public | ||
*/ | ||
Assertion.prototype.instanceof = function (constructor) { | ||
@@ -210,2 +506,15 @@ var name = constructor.name; | ||
/** | ||
* # respondTo(method) | ||
* | ||
* Assert that `method` is a function. | ||
* | ||
* var res = { send: function () {} }; | ||
* expect(res).to.respondTo('send'); | ||
* | ||
* @name respondTo | ||
* @param {String} method name | ||
* @api public | ||
*/ | ||
Assertion.prototype.respondTo = function (method) { | ||
@@ -220,2 +529,20 @@ this.assert( | ||
/** | ||
* # .property(name, [value]) | ||
* | ||
* Assert that property of `name` exists, | ||
* optionally with `value`. | ||
* | ||
* var obj = { foo: 'bar' } | ||
* expect(obj).to.have.property('foo'); | ||
* expect(obj).to.have.property('foo', 'bar'); | ||
* expect(obj).to.have.property('foo').to.be.a('string'); | ||
* | ||
* @name property | ||
* @param {String} name | ||
* @param {*} value (optional) | ||
* @returns value of property for chaining | ||
* @api public | ||
*/ | ||
Assertion.prototype.property = function (name, val) { | ||
@@ -245,2 +572,15 @@ if (this.negate && undefined !== val) { | ||
/** | ||
* # .ownProperty(name) | ||
* | ||
* Assert that has own property by `name`. | ||
* | ||
* expect('test').to.have.ownProperty('length'); | ||
* | ||
* @name ownProperty | ||
* @alias haveOwnProperty | ||
* @param {String} name | ||
* @api public | ||
*/ | ||
Assertion.prototype.ownProperty = function (name) { | ||
@@ -254,2 +594,16 @@ this.assert( | ||
/** | ||
* # .length(val) | ||
* | ||
* Assert that object has expected length. | ||
* | ||
* expect([1,2,3]).to.have.length(3); | ||
* expect('foobar').to.have.length(6); | ||
* | ||
* @name length | ||
* @alias lengthOf | ||
* @param {Number} length | ||
* @api public | ||
*/ | ||
Assertion.prototype.length = function (n) { | ||
@@ -267,2 +621,14 @@ new Assertion(this.obj).to.have.property('length'); | ||
/** | ||
* # .match(regexp) | ||
* | ||
* Assert that matches regular expression. | ||
* | ||
* expect('foobar').to.match(/^foo/); | ||
* | ||
* @name match | ||
* @param {RegExp} RegularExpression | ||
* @api public | ||
*/ | ||
Assertion.prototype.match = function (re) { | ||
@@ -277,2 +643,14 @@ this.assert( | ||
/** | ||
* # .contain(obj) | ||
* | ||
* Assert the inclusion of an object in an Array | ||
* | ||
* expect([1,2,3]).to.contain(2); | ||
* | ||
* @name contain | ||
* @param {Object|String|Number} obj | ||
* @api public | ||
*/ | ||
Assertion.prototype.contain = function (obj) { | ||
@@ -289,22 +667,14 @@ new Assertion(this.obj).to.be.an.instanceof(Array); | ||
Assertion.prototype.within = function (start, finish) { | ||
var range = start + '..' + finish; | ||
/** | ||
* # .string(string) | ||
* | ||
* Assert inclusion of string in string. | ||
* | ||
* expect('foobar').to.include.string('bar'); | ||
* | ||
* @name string | ||
* @param {String} string | ||
* @api public | ||
*/ | ||
this.assert( | ||
this.obj >= start && this.obj <= finish | ||
, 'expected ' + this.inspect + ' to be within ' + range | ||
, 'expected ' + this.inspect + ' to not be within ' + range); | ||
return this; | ||
}; | ||
Assertion.prototype.greaterThan = function (val) { | ||
this.assert( | ||
this.obj > val | ||
, 'expected ' + this.inspect + ' to be greater than ' + inspect(val) | ||
, 'expected ' + this.inspect + ' to not be greater than ' + inspect(val)); | ||
return this; | ||
}; | ||
Assertion.prototype.string = function (str) { | ||
@@ -321,2 +691,15 @@ new Assertion(this.obj).is.a('string'); | ||
/** | ||
* # .object(object) | ||
* | ||
* Assert inclusion of object in object. | ||
* | ||
* var obj = {foo: 'bar', baz: {baaz: 42}, qux: 13}; | ||
* expect(obj).to.include.object({foo: 'bar'}); | ||
* | ||
* @name object | ||
* @param {Object} object | ||
* @api public | ||
*/ | ||
Assertion.prototype.object = function(obj){ | ||
@@ -342,2 +725,31 @@ new Assertion(this.obj).is.a('object'); | ||
/** | ||
* # include | ||
* | ||
* Language chain that lags #keys to test for inclusion testing. | ||
* | ||
* @name include | ||
* @api public | ||
*/ | ||
Assertion.prototype.__defineGetter__('include', function () { | ||
this.includes = true; | ||
return this; | ||
}); | ||
/** | ||
* # .keys(key1, [key2], [...]) | ||
* | ||
* Assert exact keys or the inclusing of keys using | ||
* the include modifier. | ||
* | ||
* expect({ foo: 1, bar: 2 }).to.have.keys(['foo', 'bar']); | ||
* expect({ foo: 1, bar: 2, baz: 3 }).to.include.keys('foo', 'bar'); | ||
* | ||
* @name keys | ||
* @alias key | ||
* @param {String|Array} Keys | ||
* @api public | ||
*/ | ||
Assertion.prototype.keys = function(keys) { | ||
@@ -392,2 +804,18 @@ var str | ||
/** | ||
* # .throw(constructor) | ||
* | ||
* Assert that a function will throw a specific | ||
* type of error. | ||
* | ||
* var fn = function () { throw new ReferenceError(''); } | ||
* expect(fn).to.throw(ReferenceError); | ||
* | ||
* @name throw | ||
* @alias throws | ||
* @param {ErrorConstructor} constructor | ||
* @see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Error#Error_types | ||
* @api public | ||
*/ | ||
Assertion.prototype.throw = function (constructor) { | ||
@@ -417,3 +845,3 @@ new Assertion(this.obj).is.a('function'); | ||
/** | ||
/*! | ||
* Aliases. | ||
@@ -430,2 +858,3 @@ */ | ||
('above', 'greaterThan') | ||
('below', 'lessThan'); | ||
('below', 'lessThan') | ||
('throw', 'throws'); |
@@ -9,3 +9,3 @@ /*! | ||
exports.version = '0.1.0'; | ||
exports.version = '0.1.1'; | ||
@@ -28,2 +28,2 @@ exports.expect = require('./interface/expect'); | ||
}); | ||
} | ||
}; |
{ | ||
"author": "Jake Luer <jake@alogicalparadox.com>", | ||
"name": "chai", | ||
"description": "BDD/TDD assertion framework for node.js and the browser.", | ||
"description": "BDD/TDD assertion library for node.js and the browser. Test framework agnostic.", | ||
"keywords": [ "test", "assertion", "assert", "testing" ], | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"repository": { | ||
@@ -23,4 +23,5 @@ "type": "git", | ||
"devDependencies": { | ||
"mocha": "*" | ||
"mocha": "*", | ||
"codex": "0.0.5" | ||
} | ||
} |
@@ -1,11 +0,10 @@ | ||
# Chai [![Build Status](https://secure.travis-ci.org/logicalparadox/chai.png)](http://travis-ci.org/logicalparadox/chai) | ||
[![Build Status](https://secure.travis-ci.org/logicalparadox/chai.png)](http://travis-ci.org/logicalparadox/chai) | ||
Chai is a multi-style assert library for [node](http://nodejs.org) and the browser. | ||
It is based on [@visionmedia's awesome should.js](https://github.com/visionmedia/should.js) | ||
assert library and is completely API compatable. | ||
[![Chai Documentation](https://github.com/logicalparadox/chai/raw/master/docs/template/assets/img/chai-logo.png)](http://chaijs.com) | ||
This library was developed because, as awesome as `should.js` is, it doesn't work in the browser. | ||
As the lines blur (and sometimes disappear) between modules that run on either side, I needed | ||
an assert package that allowed the same tests to run on both sides. | ||
Chai is a BDD / TDD assertion library for [node](http://nodejs.org) and the browser that | ||
can be delightfully paired with any javascript testing framework. | ||
For more information view the [documentation](http://chaijs.com). | ||
## Installation | ||
@@ -29,55 +28,5 @@ | ||
Chai tests itself in the browser using mocha [mocha](https://github.com/visionmedia/mocha). | ||
Chai tests itself in the browser using [mocha](https://github.com/visionmedia/mocha). | ||
Have a look at the `test/browser` folder for an example. | ||
## Styles | ||
### Expect | ||
The `expect` style is server/browser BDD style assert language. | ||
```js | ||
var expect = require('chai').expect; | ||
var foo = 'bar'; | ||
expect(foo).to.be.a('string'); | ||
expect(foo).to.equal('bar'); | ||
expect(foo).to.have.length(3); | ||
``` | ||
### Should | ||
The `should` style allows for chai to be a replacement for [should.js](https://github.com/visionmedia/should.js) | ||
if the need arises. | ||
```js | ||
var should = require('chai').should(); //actually call the the function | ||
var foo = 'bar'; | ||
foo.should.be.a('string'); | ||
foo.should.equal('bar'); | ||
foo.should.have.length(3); | ||
``` | ||
*Should tests do not run in the browser.* | ||
Notice that the `expect` require is just a reference to the `expect` function, whereas | ||
with the `should` require, the function is being executed. | ||
### Assert | ||
The `assert` style is like the node.js included assert utility with few extras. | ||
```js | ||
var assert = require('chai').assert; | ||
var foo = 'bar'; | ||
assert.typeOf(foo, 'string'); | ||
assert.equal(foo, 'bar'); | ||
assert.length(foo, 3); | ||
``` | ||
## License | ||
@@ -84,0 +33,0 @@ |
@@ -7,6 +7,7 @@ /** | ||
if (!chai) { | ||
var chai = require('../') | ||
, expect = chai.expect; | ||
var chai = require('../'); | ||
} | ||
var expect = chai.expect; | ||
function err(fn, msg) { | ||
@@ -71,2 +72,9 @@ try { | ||
'text exist': function(){ | ||
var foo = 'bar' | ||
, bar; | ||
expect(foo).to.exist; | ||
expect(bar).to.not.exist; | ||
}, | ||
'test arguments': function(){ | ||
@@ -73,0 +81,0 @@ var args = (function(){ return arguments; })(1,2,3); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
109271
3135
0
2
53