Comparing version 2.1.1 to 3.0.0
@@ -0,1 +1,16 @@ | ||
3.0.0 / 2014-01-17 | ||
================== | ||
* Split everything to smaller files | ||
* Added huge extension to .match and .matchEach. Thanks @alsotang for initial code and idea. | ||
* Added .containDeep and .containEql | ||
* Separate build for browser and node.js | ||
* Basic plugin system | ||
* Breaking change: .Object now do not fail on arrays | ||
* Breaking change: Additional messages now replaces generated and do not added | ||
* Breaking change: .keys now check as is - no keys in args means no keys in object | ||
* Deprecated: assert extension | ||
* Deprecated: .include and .includeEql | ||
* Now all assertions define only positive cases, should.js take care about negations and chains | ||
2.1.1 / 2013-12-05 | ||
@@ -2,0 +17,0 @@ ================== |
module.exports = require('./lib/should'); | ||
module.exports = require('./lib/node.js'); |
@@ -0,1 +1,7 @@ | ||
/*! | ||
* Should | ||
* Copyright(c) 2010-2014 TJ Holowaychuk <tj@vision-media.ca> | ||
* MIT Licensed | ||
*/ | ||
// Taken from node's assert module, because it sucks | ||
@@ -2,0 +8,0 @@ // and exposes next to nothing useful. |
1010
lib/should.js
/*! | ||
* Should | ||
* Copyright(c) 2010-2012 TJ Holowaychuk <tj@vision-media.ca> | ||
* Copyright(c) 2010-2014 TJ Holowaychuk <tj@vision-media.ca> | ||
* MIT Licensed | ||
*/ | ||
/** | ||
* Module dependencies. | ||
*/ | ||
var util = require('./util') | ||
, assert = require('assert') | ||
, AssertionError = assert.AssertionError | ||
, statusCodes = require('./http').STATUS_CODES | ||
, eql = require('./eql') | ||
, inspect = require('util').inspect; | ||
, AssertionError = util.AssertionError | ||
, inspect = util.inspect; | ||
@@ -27,55 +21,76 @@ /** | ||
should.inspect = inspect; | ||
function i(value) { | ||
if(util.isDate(value) && typeof value.inspect !== 'function') return value.toISOString(); //show millis in dates | ||
return should.inspect(value); | ||
} | ||
/** | ||
* Expose assert to should | ||
* Initialize a new `Assertion` with the given _obj_. | ||
* | ||
* This allows you to do things like below | ||
* without require()ing the assert module. | ||
* | ||
* should.equal(foo.bar, undefined); | ||
* | ||
* @param {*} obj | ||
* @api private | ||
*/ | ||
util.merge(should, assert); | ||
var Assertion = should.Assertion = function Assertion(obj) { | ||
this.obj = obj; | ||
}; | ||
/** | ||
* Assert _obj_ exists, with optional message. | ||
* | ||
* @param {*} obj | ||
* @param {String} [msg] | ||
* @api public | ||
*/ | ||
should.exist = should.exists = function(obj, msg) { | ||
if(null == obj) { | ||
throw new AssertionError({ | ||
message: msg || ('expected ' + i(obj) + ' to exist') | ||
, stackStartFunction: should.exist | ||
}); | ||
} | ||
Way to extend Assertion function. It uses some logic | ||
to define only positive assertions and itself rule with negative assertion. | ||
All actions happen in subcontext and this method take care about negation. | ||
Potentially we can add some more modifiers that does not depends from state of assertion. | ||
*/ | ||
Assertion.add = function(name, f, isGetter) { | ||
var prop = {}; | ||
prop[isGetter ? 'get' : 'value'] = function() { | ||
var context = new Assertion(this.obj); | ||
context.copy = context.copyIfMissing; | ||
try { | ||
f.apply(context, arguments); | ||
} catch(e) { | ||
//copy data from sub context to this | ||
this.copy(context); | ||
//check for fail | ||
if(e instanceof should.AssertionError) { | ||
//negative fail | ||
if(this.negate) { | ||
this.obj = context.obj; | ||
this.negate = false; | ||
return this; | ||
} | ||
this.assert(false); | ||
} | ||
// throw if it is another exception | ||
throw e; | ||
} | ||
//copy data from sub context to this | ||
this.copy(context); | ||
if(this.negate) { | ||
this.assert(false); | ||
} | ||
this.obj = context.obj; | ||
this.negate = false; | ||
return this; | ||
}; | ||
Object.defineProperty(Assertion.prototype, name, prop); | ||
}; | ||
/** | ||
* Asserts _obj_ does not exist, with optional message. | ||
* | ||
* @param {*} obj | ||
* @param {String} [msg] | ||
* @api public | ||
*/ | ||
Assertion.alias = function(from, to) { | ||
Assertion.prototype[to] = Assertion.prototype[from] | ||
}; | ||
should.not = {}; | ||
should.not.exist = should.not.exists = function(obj, msg){ | ||
if (null != obj) { | ||
throw new AssertionError({ | ||
message: msg || ('expected ' + i(obj) + ' to not exist') | ||
, stackStartFunction: should.not.exist | ||
}); | ||
} | ||
should.AssertionError = AssertionError; | ||
var i = should.format = function i(value) { | ||
if(util.isDate(value) && typeof value.inspect !== 'function') return value.toISOString(); //show millis in dates | ||
return inspect(value, { depth: null }); | ||
}; | ||
should.use = function(f) { | ||
f(this, Assertion); | ||
return this; | ||
}; | ||
/** | ||
@@ -101,51 +116,27 @@ * Expose should to external world. | ||
/** | ||
* Initialize a new `Assertion` with the given _obj_. | ||
* | ||
* @param {*} obj | ||
* @api private | ||
*/ | ||
var Assertion = should.Assertion = function Assertion(obj) { | ||
this.obj = obj; | ||
}; | ||
var hasOwnProperty = Object.prototype.hasOwnProperty; | ||
/** | ||
* Prototype. | ||
*/ | ||
Assertion.prototype = { | ||
constructor: Assertion, | ||
/** | ||
* Assert _expr_ with the given _msg_ and _negatedMsg_. | ||
* | ||
* @param {Boolean} expr | ||
* @param {function} msg | ||
* @param {function} negatedMsg | ||
* @param {Object} [expected] | ||
* @param {Boolean} [showDiff] | ||
* @param {String} [description] | ||
* @api private | ||
*/ | ||
assert: function(expr) { | ||
if(expr) return; | ||
assert: function(expr, msg, negatedMsg, expected, showDiff, description){ | ||
msg = this.negate ? negatedMsg : msg | ||
var params = this.params; | ||
var ok = this.negate ? !expr : expr | ||
, obj = this.obj; | ||
var msg = params.message, generatedMessage = false; | ||
if(!msg) { | ||
msg = this.getMessage(); | ||
generatedMessage = true; | ||
} | ||
if (ok) return; | ||
var err = new AssertionError({ | ||
message: msg.call(this) | ||
, actual: obj | ||
, expected: expected | ||
message: msg | ||
, actual: this.obj | ||
, expected: params.expected | ||
, stackStartFunction: this.assert | ||
, negated: this.negate | ||
}); | ||
err.showDiff = showDiff; | ||
err.description = description | ||
err.showDiff = params.showDiff; | ||
err.operator = params.operator; | ||
err.generatedMessage = generatedMessage; | ||
@@ -155,73 +146,17 @@ throw err; | ||
/** | ||
* Dummy getter. | ||
* | ||
* @api public | ||
*/ | ||
get an() { | ||
return this; | ||
getMessage: function() { | ||
return 'expected ' + i(this.obj) + (this.negate ? ' not ': ' ') + | ||
this.params.operator + ('expected' in this.params ? ' ' + i(this.params.expected) : ''); | ||
}, | ||
/** | ||
* Dummy getter. | ||
* | ||
* @api public | ||
*/ | ||
get of() { | ||
return this; | ||
copy: function(other) { | ||
this.params = other.params; | ||
}, | ||
/** | ||
* Dummy getter. | ||
* | ||
* @api public | ||
*/ | ||
get a() { | ||
return this; | ||
copyIfMissing: function(other) { | ||
if(!this.params) this.params = other.params; | ||
}, | ||
/** | ||
* Dummy getter. | ||
* | ||
* @api public | ||
*/ | ||
get and() { | ||
return this; | ||
}, | ||
/** | ||
* Dummy getter. | ||
* | ||
* @api public | ||
*/ | ||
get be() { | ||
return this; | ||
}, | ||
/** | ||
* Dummy getter. | ||
* | ||
* @api public | ||
*/ | ||
get have() { | ||
return this; | ||
}, | ||
/** | ||
* Dummy getter. | ||
* | ||
* @api public | ||
*/ | ||
get with() { | ||
return this; | ||
}, | ||
/** | ||
* Negation modifier. | ||
@@ -233,773 +168,6 @@ * | ||
get not() { | ||
this.negate = true; | ||
this.negate = !this.negate; | ||
return this; | ||
}, | ||
/** | ||
* Get object inspection string. | ||
* | ||
* @return {String} | ||
* @api private | ||
*/ | ||
get inspect() { | ||
return i(this.obj); | ||
}, | ||
/** | ||
* Assert instanceof `Arguments`. | ||
* | ||
* @api public | ||
*/ | ||
get arguments() { | ||
this.assert( | ||
util.isArguments(this.obj) | ||
, function(){ return 'expected ' + this.inspect + ' to be arguments' } | ||
, function(){ return 'expected ' + this.inspect + ' to not be arguments' }); | ||
return this; | ||
}, | ||
/** | ||
* Assert that object is empty. | ||
* | ||
* @api public | ||
*/ | ||
get empty() { | ||
var length = this.obj.length; | ||
if(util.isString(this.obj) || Array.isArray(this.obj) || util.isArguments(this.obj)) { | ||
this.assert( | ||
0 === length | ||
, function(){ return 'expected ' + this.inspect + ' to be empty' } | ||
, function(){ return 'expected ' + this.inspect + ' not to be empty' }); | ||
} else { | ||
var ok = true; | ||
for (var prop in this.obj) { | ||
if(hasOwnProperty.call(this.obj, prop)) { | ||
ok = false; | ||
break; | ||
} | ||
} | ||
this.assert( | ||
ok | ||
, function(){ return 'expected ' + this.inspect + ' to be empty' } | ||
, function(){ return 'expected ' + this.inspect + ' not to be empty' }); | ||
} | ||
return this; | ||
}, | ||
/** | ||
* Assert ok. | ||
* | ||
* @api public | ||
*/ | ||
get ok() { | ||
this.assert( | ||
this.obj | ||
, function(){ return 'expected ' + this.inspect + ' to be truthy' } | ||
, function(){ return 'expected ' + this.inspect + ' to be falsey' }); | ||
return this; | ||
}, | ||
/** | ||
* Assert true. | ||
* | ||
* @api public | ||
*/ | ||
get true() { | ||
this.assert( | ||
true === this.obj | ||
, function(){ return 'expected ' + this.inspect + ' to be true' } | ||
, function(){ return 'expected ' + this.inspect + ' not to be true' }); | ||
return this; | ||
}, | ||
/** | ||
* Assert false. | ||
* | ||
* @api public | ||
*/ | ||
get false() { | ||
this.assert( | ||
false === this.obj | ||
, function(){ return 'expected ' + this.inspect + ' to be false' } | ||
, function(){ return 'expected ' + this.inspect + ' not to be false' }); | ||
return this; | ||
}, | ||
/** | ||
* Assert NaN. | ||
* | ||
* @api public | ||
*/ | ||
get NaN() { | ||
this.assert( | ||
util.isNumber(this.obj) && isNaN(this.obj) | ||
, function(){ return 'expected ' + this.inspect + ' to be NaN' } | ||
, function(){ return 'expected ' + this.inspect + ' not to be NaN' }); | ||
return this; | ||
}, | ||
/** | ||
* Assert Infinity. | ||
* | ||
* @api public | ||
*/ | ||
get Infinity() { | ||
this.assert( | ||
util.isNumber(this.obj) && !isNaN(this.obj) && !isFinite(this.obj) | ||
, function(){ return 'expected ' + this.inspect + ' to be Infinity' } | ||
, function(){ return 'expected ' + this.inspect + ' not to be Infinity' }); | ||
return this; | ||
}, | ||
/** | ||
* Assert equal. | ||
* | ||
* @param {*} val | ||
* @param {String} description | ||
* @api public | ||
*/ | ||
eql: function(val, description){ | ||
this.assert( | ||
eql(val, this.obj) | ||
, function(){ return 'expected ' + this.inspect + ' to equal ' + i(val) + (description ? " | " + description : "") } | ||
, function(){ return 'expected ' + this.inspect + ' to not equal ' + i(val) + (description ? " | " + description : "") } | ||
, val | ||
, true | ||
, description); | ||
return this; | ||
}, | ||
/** | ||
* Assert strict equal. | ||
* | ||
* @param {*} val | ||
* @param {String} description | ||
* @api public | ||
*/ | ||
equal: function(val, description){ | ||
this.assert( | ||
val === this.obj | ||
, function(){ return 'expected ' + this.inspect + ' to equal ' + i(val) + (description ? " | " + description : "") } | ||
, function(){ return 'expected ' + this.inspect + ' to not equal ' + i(val) + (description ? " | " + description : "") } | ||
, val | ||
, void 0 | ||
, description); | ||
return this; | ||
}, | ||
/** | ||
* Assert within start to finish (inclusive). | ||
* | ||
* @param {Number} start | ||
* @param {Number} finish | ||
* @param {String} description | ||
* @api public | ||
*/ | ||
within: function(start, finish, description){ | ||
var range = start + '..' + finish; | ||
this.assert( | ||
this.obj >= start && this.obj <= finish | ||
, function(){ return 'expected ' + this.inspect + ' to be within ' + range + (description ? " | " + description : "") } | ||
, function(){ return 'expected ' + this.inspect + ' to not be within ' + range + (description ? " | " + description : "") } | ||
, void 0 | ||
, void 0 | ||
, description); | ||
return this; | ||
}, | ||
/** | ||
* Assert within value +- delta (inclusive). | ||
* | ||
* @param {Number} value | ||
* @param {Number} delta | ||
* @param {String} description | ||
* @api public | ||
*/ | ||
approximately: function(value, delta, description) { | ||
this.assert( | ||
Math.abs(this.obj - value) <= delta | ||
, function(){ return 'expected ' + this.inspect + ' to be approximately ' + value + " +- " + delta + (description ? " | " + description : "") } | ||
, function(){ return 'expected ' + this.inspect + ' to not be approximately ' + value + " +- " + delta + (description ? " | " + description : "") } | ||
, void 0 | ||
, void 0 | ||
, description); | ||
return this; | ||
}, | ||
/** | ||
* Assert typeof. | ||
* | ||
* @param {*} type | ||
* @param {String} description | ||
* @api public | ||
*/ | ||
type: function(type, description){ | ||
this.assert( | ||
type == typeof this.obj | ||
, function(){ return 'expected ' + this.inspect + ' to have type ' + type + (description ? " | " + description : "") } | ||
, function(){ return 'expected ' + this.inspect + ' not to have type ' + type + (description ? " | " + description : "") } | ||
, void 0 | ||
, void 0 | ||
, description); | ||
return this; | ||
}, | ||
/** | ||
* Assert instanceof. | ||
* | ||
* @param {Function} constructor | ||
* @param {String} description | ||
* @api public | ||
*/ | ||
instanceof: function(constructor, description){ | ||
var name = constructor.name; | ||
this.assert( | ||
this.obj instanceof constructor | ||
, function(){ return 'expected ' + this.inspect + ' to be an instance of ' + name + (description ? " | " + description : "") } | ||
, function(){ return 'expected ' + this.inspect + ' not to be an instance of ' + name + (description ? " | " + description : "") } | ||
, void 0 | ||
, void 0 | ||
, description); | ||
return this; | ||
}, | ||
/** | ||
* Assert if given object is a function. | ||
*/ | ||
get Function(){ | ||
this.assert( | ||
util.isFunction(this.obj) | ||
, function(){ return 'expected ' + this.inspect + ' to be a function' } | ||
, function(){ return 'expected ' + this.inspect + ' not to be a function' }); | ||
return this; | ||
}, | ||
/** | ||
* Assert given object is an object. | ||
*/ | ||
get Object(){ | ||
this.assert( | ||
util.isObject(this.obj) && !Array.isArray(this.obj) | ||
, function(){ return 'expected ' + this.inspect + ' to be an object' } | ||
, function(){ return 'expected ' + this.inspect + ' not to be an object' }); | ||
return this; | ||
}, | ||
/** | ||
* Assert given object is a string | ||
*/ | ||
get String(){ | ||
this.assert( | ||
util.isString(this.obj) | ||
, function(){ return 'expected ' + this.inspect + ' to be a string' } | ||
, function(){ return 'expected ' + this.inspect + ' not to be a string' }); | ||
return this; | ||
}, | ||
/** | ||
* Assert given object is an array | ||
*/ | ||
get Array(){ | ||
this.assert( | ||
Array.isArray(this.obj) | ||
, function(){ return 'expected ' + this.inspect + ' to be an array' } | ||
, function(){ return 'expected ' + this.inspect + ' not to be an array' }); | ||
return this; | ||
}, | ||
/** | ||
* Assert given object is a number. NaN and Infinity are not numbers. | ||
*/ | ||
get Number(){ | ||
this.assert( | ||
util.isNumber(this.obj) && isFinite(this.obj) && !isNaN(this.obj) | ||
, function(){ return 'expected ' + this.inspect + ' to be a number' } | ||
, function(){ return 'expected ' + this.inspect + ' not to be a number' }); | ||
return this; | ||
}, | ||
/** | ||
* Assert given object is a boolean | ||
*/ | ||
get Boolean(){ | ||
this.assert( | ||
util.isBoolean(this.obj) | ||
, function(){ return 'expected ' + this.inspect + ' to be a boolean' } | ||
, function(){ return 'expected ' + this.inspect + ' not to be a boolean' }); | ||
return this; | ||
}, | ||
/** | ||
* Assert given object is an error | ||
*/ | ||
get Error() { | ||
this.assert( | ||
util.isError(this.obj) | ||
, function(){ return 'expected ' + this.inspect + ' to be an error' } | ||
, function(){ return 'expected ' + this.inspect + ' not to be an error' }); | ||
return this; | ||
}, | ||
/** | ||
* Assert numeric value above _n_. | ||
* | ||
* @param {Number} n | ||
* @param {String} description | ||
* @api public | ||
*/ | ||
above: function(n, description){ | ||
this.assert( | ||
this.obj > n | ||
, function(){ return 'expected ' + this.inspect + ' to be above ' + n + (description ? " | " + description : "") } | ||
, function(){ return 'expected ' + this.inspect + ' to be below ' + n + (description ? " | " + description : "") } | ||
, void 0 | ||
, void 0 | ||
, description); | ||
return this; | ||
}, | ||
/** | ||
* Assert numeric value below _n_. | ||
* | ||
* @param {Number} n | ||
* @param {String} description | ||
* @api public | ||
*/ | ||
below: function(n, description){ | ||
this.assert( | ||
this.obj < n | ||
, function(){ return 'expected ' + this.inspect + ' to be below ' + n + (description ? " | " + description : "") } | ||
, function(){ return 'expected ' + this.inspect + ' to be above ' + n + (description ? " | " + description : "") } | ||
, void 0 | ||
, void 0 | ||
, description); | ||
return this; | ||
}, | ||
/** | ||
* Assert string value matches _regexp_. | ||
* | ||
* @param {RegExp} regexp | ||
* @param {String} description | ||
* @api public | ||
*/ | ||
match: function(regexp, description){ | ||
this.assert( | ||
regexp.exec(this.obj) | ||
, function(){ return 'expected ' + this.inspect + ' to match ' + regexp + (description ? " | " + description : "") } | ||
, function(){ return 'expected ' + this.inspect + ' not to match ' + regexp + (description ? " | " + description : "") } | ||
, void 0 | ||
, void 0 | ||
, description); | ||
return this; | ||
}, | ||
/** | ||
* Assert property "length" exists and has value of _n_. | ||
* | ||
* @param {Number} n | ||
* @param {String} description | ||
* @api public | ||
*/ | ||
length: function(n, description){ | ||
this.obj.should.have.property('length'); | ||
var len = this.obj.length; | ||
this.assert( | ||
n == len | ||
, function(){ return 'expected ' + this.inspect + ' to have a length of ' + n + ' but got ' + len + (description ? " | " + description : "") } | ||
, function(){ return 'expected ' + this.inspect + ' to not have a length of ' + len + (description ? " | " + description : "") } | ||
, void 0 | ||
, void 0 | ||
, description); | ||
return this; | ||
}, | ||
/** | ||
* Assert property _name_ exists, with optional _val_. | ||
* | ||
* @param {String} name | ||
* @param {*} [val] | ||
* @param {String} description | ||
* @api public | ||
*/ | ||
property: function(name, val, description){ | ||
if (this.negate && undefined !== val) { | ||
if (undefined === this.obj[name]) { | ||
throw new Error(this.inspect + ' has no property ' + i(name) + (description ? " | " + description : "")); | ||
} | ||
} else { | ||
this.assert( | ||
undefined !== this.obj[name] | ||
, function(){ return 'expected ' + this.inspect + ' to have a property ' + i(name) + (description ? " | " + description : "") } | ||
, function(){ return 'expected ' + this.inspect + ' to not have a property ' + i(name) + (description ? " | " + description : "") } | ||
, void 0 | ||
, void 0 | ||
, description); | ||
} | ||
if (undefined !== val) { | ||
this.assert( | ||
val === this.obj[name] | ||
, function(){ return 'expected ' + this.inspect + ' to have a property ' + i(name) | ||
+ ' of ' + i(val) + ', but got ' + i(this.obj[name]) + (description ? " | " + description : "") } | ||
, function(){ return 'expected ' + this.inspect + ' to not have a property ' + i(name) + ' of ' + i(val) + (description ? " | " + description : "") } | ||
, void 0 | ||
, void 0 | ||
, description); | ||
} | ||
this.obj = this.obj[name]; | ||
return this; | ||
}, | ||
/** | ||
* Asset have given properties | ||
* @param {Array|String ...} names | ||
* @api public | ||
*/ | ||
properties: function(names) { | ||
var str | ||
, ok = true; | ||
names = names instanceof Array | ||
? names | ||
: Array.prototype.slice.call(arguments); | ||
var len = names.length; | ||
if (!len) throw new Error('names required'); | ||
// make sure they're all present | ||
ok = names.every(function(name){ | ||
return this.obj[name] !== undefined; | ||
}, this); | ||
// key string | ||
if (len > 1) { | ||
names = names.map(function(name){ | ||
return i(name); | ||
}); | ||
var last = names.pop(); | ||
str = names.join(', ') + ', and ' + last; | ||
} else { | ||
str = i(names[0]); | ||
} | ||
// message | ||
str = 'have ' + (len > 1 ? 'properties ' : 'a property ') + str; | ||
this.assert( | ||
ok | ||
, function(){ return 'expected ' + this.inspect + ' to ' + str } | ||
, function(){ return 'expected ' + this.inspect + ' to not ' + str }); | ||
return this; | ||
}, | ||
/** | ||
* Assert own property _name_ exists. | ||
* | ||
* @param {String} name | ||
* @param {String} description | ||
* @api public | ||
*/ | ||
ownProperty: function(name, description){ | ||
this.assert( | ||
hasOwnProperty.call(this.obj, name) | ||
, function(){ return 'expected ' + this.inspect + ' to have own property ' + i(name) + (description ? " | " + description : "") } | ||
, function(){ return 'expected ' + this.inspect + ' to not have own property ' + i(name) + (description ? " | " + description : "") } | ||
, void 0 | ||
, void 0 | ||
, description); | ||
this.obj = this.obj[name]; | ||
return this; | ||
}, | ||
/** | ||
* Assert that string starts with `str`. | ||
* @param {String} str | ||
* @param {String} description | ||
* @api public | ||
*/ | ||
startWith: function(str, description) { | ||
this.assert(0 === this.obj.indexOf(str) | ||
, function() { return 'expected ' + this.inspect + ' to start with ' + i(str) + (description ? " | " + description : "") } | ||
, function() { return 'expected ' + this.inspect + ' to not start with ' + i(str) + (description ? " | " + description : "") } | ||
, void 0 | ||
, void 0 | ||
, description); | ||
return this; | ||
}, | ||
/** | ||
* Assert that string ends with `str`. | ||
* @param {String} str | ||
* @param {String} description | ||
* @api public | ||
*/ | ||
endWith: function(str, description) { | ||
this.assert(-1 !== this.obj.indexOf(str, this.obj.length - str.length) | ||
, function() { return 'expected ' + this.inspect + ' to end with ' + i(str) + (description ? " | " + description : "") } | ||
, function() { return 'expected ' + this.inspect + ' to not end with ' + i(str) + (description ? " | " + description : "") } | ||
, void 0 | ||
, void 0 | ||
, description); | ||
return this; | ||
}, | ||
/** | ||
* Assert that `obj` is present via `.indexOf()` or that `obj` contains some sub-object. | ||
* | ||
* @param {*} obj | ||
* @param {String} description | ||
* @api public | ||
*/ | ||
include: function(obj, description){ | ||
if (!Array.isArray(this.obj) && !util.isString(this.obj)){ | ||
var cmp = {}; | ||
for (var key in obj) cmp[key] = this.obj[key]; | ||
this.assert( | ||
eql(cmp, obj) | ||
, function(){ return 'expected ' + this.inspect + ' to include an object equal to ' + i(obj) + (description ? " | " + description : "") } | ||
, function(){ return 'expected ' + this.inspect + ' to not include an object equal to ' + i(obj) + (description ? " | " + description : "") } | ||
, void 0 | ||
, void 0 | ||
, description); | ||
} else { | ||
this.assert( | ||
~this.obj.indexOf(obj) | ||
, function(){ return 'expected ' + this.inspect + ' to include ' + i(obj) + (description ? " | " + description : "") } | ||
, function(){ return 'expected ' + this.inspect + ' to not include ' + i(obj) + (description ? " | " + description : "") } | ||
, void 0 | ||
, void 0 | ||
, description); | ||
} | ||
return this; | ||
}, | ||
/** | ||
* Assert that an object equal to `obj` is present. | ||
* | ||
* @param {Array} obj | ||
* @param {String} description | ||
* @api public | ||
*/ | ||
includeEql: function(obj, description){ | ||
this.assert( | ||
this.obj.some(function(item) { return eql(obj, item); }) | ||
, function(){ return 'expected ' + this.inspect + ' to include an object equal to ' + i(obj) + (description ? " | " + description : "") } | ||
, function(){ return 'expected ' + this.inspect + ' to not include an object equal to ' + i(obj) + (description ? " | " + description : "") } | ||
, void 0 | ||
, void 0 | ||
, description); | ||
return this; | ||
}, | ||
/** | ||
* Assert exact keys or inclusion of keys by using | ||
* the `.include` modifier. | ||
* | ||
* @param {Array|String ...} keys | ||
* @api public | ||
*/ | ||
keys: function(keys){ | ||
var str | ||
, ok = true; | ||
keys = keys instanceof Array | ||
? keys | ||
: Array.prototype.slice.call(arguments); | ||
if (!keys.length) throw new Error('keys required'); | ||
var actual = Object.keys(this.obj) | ||
, len = keys.length; | ||
// make sure they're all present | ||
ok = keys.every(function(key){ | ||
return ~actual.indexOf(key); | ||
}); | ||
// matching length | ||
ok = ok && keys.length == actual.length; | ||
// key string | ||
if (len > 1) { | ||
keys = keys.map(function(key){ | ||
return i(key); | ||
}); | ||
var last = keys.pop(); | ||
str = keys.join(', ') + ', and ' + last; | ||
} else { | ||
str = i(keys[0]); | ||
} | ||
// message | ||
str = 'have ' + (len > 1 ? 'keys ' : 'key ') + str; | ||
this.assert( | ||
ok | ||
, function(){ return 'expected ' + this.inspect + ' to ' + str } | ||
, function(){ return 'expected ' + this.inspect + ' to not ' + str }); | ||
return this; | ||
}, | ||
/** | ||
* Assert that header `field` has the given `val`. | ||
* | ||
* @param {String} field | ||
* @param {String} val | ||
* @return {Assertion} for chaining | ||
* @api public | ||
*/ | ||
header: function(field, val){ | ||
this.obj.should | ||
.have.property('headers').and | ||
.have.property(field.toLowerCase(), val); | ||
return this; | ||
}, | ||
/** | ||
* Assert `.statusCode` of `code`. | ||
* | ||
* @param {Number} code | ||
* @return {Assertion} for chaining | ||
* @api public | ||
*/ | ||
status: function(code){ | ||
this.obj.should.have.property('statusCode'); | ||
var status = this.obj.statusCode; | ||
this.assert( | ||
code == status | ||
, function(){ return 'expected response code of ' + code + ' ' + i(statusCodes[code]) | ||
+ ', but got ' + status + ' ' + i(statusCodes[status]) } | ||
, function(){ return 'expected to not respond with ' + code + ' ' + i(statusCodes[code]) }); | ||
return this; | ||
}, | ||
/** | ||
* Assert that this response has content-type: application/json. | ||
* | ||
* @return {Assertion} for chaining | ||
* @api public | ||
*/ | ||
get json() { | ||
this.obj.should.have.property('headers'); | ||
this.obj.headers.should.have.property('content-type'); | ||
this.obj.headers['content-type'].should.include('application/json'); | ||
return this; | ||
}, | ||
/** | ||
* Assert that this response has content-type: text/html. | ||
* | ||
* @return {Assertion} for chaining | ||
* @api public | ||
*/ | ||
get html() { | ||
this.obj.should.have.property('headers'); | ||
this.obj.headers.should.have.property('content-type'); | ||
this.obj.headers['content-type'].should.include('text/html'); | ||
return this; | ||
}, | ||
/** | ||
* Assert that this function will or will not | ||
* throw an exception. | ||
* | ||
* @return {Assertion} for chaining | ||
* @api public | ||
*/ | ||
throw: function(message){ | ||
var fn = this.obj | ||
, err = {} | ||
, errorInfo = '' | ||
, ok = true; | ||
try { | ||
fn(); | ||
ok = false; | ||
} catch (e) { | ||
err = e; | ||
} | ||
if (ok) { | ||
if ('string' == typeof message) { | ||
ok = message == err.message; | ||
} else if (message instanceof RegExp) { | ||
ok = message.test(err.message); | ||
} else if ('function' == typeof message) { | ||
ok = err instanceof message; | ||
} | ||
if (message && !ok) { | ||
if ('string' == typeof message) { | ||
errorInfo = " with a message matching '" + message + "', but got '" + err.message + "'"; | ||
} else if (message instanceof RegExp) { | ||
errorInfo = " with a message matching " + message + ", but got '" + err.message + "'"; | ||
} else if ('function' == typeof message) { | ||
errorInfo = " of type " + message.name + ", but got " + err.constructor.name; | ||
} | ||
} | ||
} | ||
this.assert( | ||
ok | ||
, function(){ return 'expected an exception to be thrown' + errorInfo } | ||
, function(){ return 'expected no exception to be thrown, got "' + err.message + '"' }); | ||
return this; | ||
} | ||
}; | ||
/** | ||
* Aliases. | ||
*/ | ||
(function alias(name, as){ | ||
Assertion.prototype[as] = Assertion.prototype[name]; | ||
return alias; | ||
}) | ||
('instanceof', 'instanceOf') | ||
('throw', 'throwError') | ||
('length', 'lengthOf') | ||
('keys', 'key') | ||
('ownProperty', 'haveOwnProperty') | ||
('above', 'greaterThan') | ||
('below', 'lessThan') | ||
('include', 'contain') | ||
('equal', 'exactly'); | ||
@@ -0,1 +1,7 @@ | ||
/*! | ||
* Should | ||
* Copyright(c) 2010-2014 TJ Holowaychuk <tj@vision-media.ca> | ||
* MIT Licensed | ||
*/ | ||
/** | ||
@@ -9,3 +15,3 @@ * Check if given obj just a primitive type wrapper | ||
return isNumber(obj) || isString(obj) || isBoolean(obj); | ||
} | ||
}; | ||
@@ -100,2 +106,16 @@ /** | ||
} | ||
exports.isError = isError; | ||
exports.isError = isError; | ||
exports.inspect = require('util').inspect; | ||
exports.AssertionError = require('assert').AssertionError; | ||
var hasOwnProperty = Object.prototype.hasOwnProperty; | ||
exports.forOwn = function(obj, f, context) { | ||
for(var prop in obj) { | ||
if(hasOwnProperty.call(obj, prop)) { | ||
f.call(context, obj[prop], prop); | ||
} | ||
} | ||
}; |
{ "name": "should" | ||
, "description": "test framework agnostic BDD-style assertions" | ||
, "version": "2.1.1" | ||
, "version": "3.0.0" | ||
, "author": "TJ Holowaychuk <tj@vision-media.ca>" | ||
, "repository": { "type": "git", "url": "git://github.com/visionmedia/should.js.git" } | ||
, "homepage": "https://github.com/visionmedia/should.js" | ||
, "contributors": [ "Aseem Kishore <aseem.kishore@gmail.com>" ] | ||
, "scripts": { "test": "make test" } | ||
@@ -13,4 +12,4 @@ , "devDependencies": { | ||
, "keywords": ["test", "bdd", "assert"] | ||
, "main": "./lib/should.js" | ||
, "main": "./lib/node.js" | ||
, "engines": { "node": ">= 0.2.0" } | ||
} |
602
Readme.md
# should.js | ||
_should_ is an expressive, readable, test framework agnostic, assertion library for [node](http://nodejs.org). | ||
_should_ is an expressive, readable, test framework agnostic, assertion library. Main goals of this library __to be expressive__ and __to be helpful__. It means test code should be clean, and error messages enough helpfull to understand error. | ||
It extends the Object prototype with a single non-enumerable getter that allows you to express how that object should behave. | ||
It extends the `Object.prototype` with a single non-enumerable getter that allows you to express how that object should behave, also it returns itself when required with `require`. | ||
@@ -19,3 +19,6 @@ ## Example | ||
// or without Object.prototype, for guys how did Object.create(null) | ||
// if the object was created with Object.create(null) | ||
// then it doesn't inherit `Object` and have the `should` getter | ||
// so you can do: | ||
should(user).have.property('name', 'tj'); | ||
@@ -30,9 +33,19 @@ should(true).ok; | ||
``` | ||
## Installation | ||
## To begin | ||
1. Install it: | ||
$ npm install should --save-dev | ||
2. Require it and use: | ||
```javascript | ||
var should = require('should'); | ||
(5).should.be.exactly(5).and.be.a.Number; | ||
``` | ||
## In browser | ||
If you want to use _should_ in browser, use version that is in root of repository. It is build with browserify (see [Makefile](https://github.com/visionmedia/should.js/blob/master/Makefile) about how it is build). To build fresh version: | ||
If you want to use _should_ in browser, use version that is in root of repository (or build it yourself). It is builded with browserify (see [Makefile](https://github.com/visionmedia/should.js/blob/master/Makefile)). To build fresh version: | ||
@@ -45,34 +58,34 @@ ```bash | ||
## chaining assertions | ||
This script exported to `window.Should`. It is the same as use `should` statically: | ||
Every assertion will return a should.js-wraped Object, so assertions can be chained. For example: | ||
```js | ||
Should(5).be.exactly(5) | ||
``` | ||
Also as for node.js case `Object.prototype` extended with `should` (that is why `window.Should` used): | ||
```js | ||
user.should.be.an.instanceOf(Object).and.have.property('name', 'tj'); | ||
user.pets.should.be.instanceof(Array).and.have.lengthOf(4); | ||
window.should.be.exactly(window); | ||
// the same | ||
// window is host object | ||
should.be.exactly(window); | ||
// you should not really care about it | ||
(5).should.be.exactly(5); | ||
``` | ||
All assertions return themselves but not `property` and `ownProperty`. They two would return the property value. | ||
*should.js* uses EcmaScript 5 very extensively so any browser that support ES5 is supported. (IE <=8 not supported). | ||
See [kangax's compat table](http://kangax.github.io/es5-compat-table) to know which exactly. | ||
for example if a property is volatile we can first assert property existence: | ||
```javascript | ||
// `user.should.have.property('pets')` returned `should(user.pets)` but not `should(user)` | ||
user.should.have.property('pets').with.lengthOf(4) | ||
You can easy install it with again npm or bower: | ||
``` | ||
which is essentially equivalent to below, however the property may not exist: | ||
```javascript | ||
user.pets.should.have.lengthOf(4) | ||
npm install should --save-dev | ||
# or | ||
bower install visionmedia/should.js | ||
``` | ||
our dummy getters such as _a_, _an_, _be_, _of_ and _have_ make tests more readable while the getters _and_ and _with_ helps express chaining: | ||
```javascript | ||
user.should.be.an.instanceOf(Object).and.have.property('name', 'tj') | ||
user.should.have.property('pets').with.a.lengthOf(4) | ||
``` | ||
Those getters don't have any actual effect, just for readability. | ||
## Static should and assert module | ||
## static | ||
For some rare cases should can be used statically, without `Object.prototype`. | ||
It can be replacement for node assert module (and it uses the same `AssertionError`): | ||
It can be replacement for node assert module: | ||
@@ -92,224 +105,385 @@ ```javascript | ||
``` | ||
## .not | ||
## ok | ||
`.not` negate current assertion. | ||
# Assertions | ||
## chaining assertions | ||
Every assertion will return a `should.js`-wrapped Object, so assertions can be chained. | ||
You can use this helpers to just chain: `.an`, `.of`, `.a`, `.and`, `.be`, `.have`, `.with`, `.is`, `.which`. Use them for better readability, they do nothing at all. | ||
For example: | ||
```js | ||
user.should.be.an.instanceOf(Object).and.have.property('name', 'tj'); | ||
user.pets.should.be.instanceof(Array).and.have.lengthOf(4); | ||
``` | ||
Almost all assertions return the same object - so you can easy chain them. But some move assertion object to property value. See feather, it will be mention if object chainged. | ||
## .ok | ||
Assert if asseted object is truthy in javascript meaning of truthy ('', null, undefined, 0 , NaN, Infinity - is falsy, so all others are truthy). | ||
Assert truthfulness: | ||
```javascript | ||
true.should.be.ok | ||
'yay'.should.be.ok | ||
(1).should.be.ok | ||
true.should.be.ok; | ||
'yay'.should.be.ok; | ||
(1).should.be.ok; | ||
({}).should.be.ok; | ||
``` | ||
or negated: | ||
```javascript | ||
false.should.not.be.ok | ||
''.should.not.be.ok | ||
(0).should.not.be.ok | ||
false.should.not.be.ok; | ||
''.should.not.be.ok; | ||
(0).should.not.be.ok; | ||
``` | ||
## true | ||
Assert === true: | ||
## .true | ||
Assert if asseted object === true: | ||
```javascript | ||
true.should.be.true | ||
'1'.should.not.be.true | ||
true.should.be.true; | ||
'1'.should.not.be.true; | ||
``` | ||
## false | ||
Assert === false: | ||
## .false | ||
Assert if asseted object === false: | ||
```javascript | ||
false.should.be.false | ||
(0).should.not.be.false | ||
false.should.be.false; | ||
(0).should.not.be.false; | ||
``` | ||
## arguments | ||
Assert `Arguments`: | ||
## .eql(otherValue) | ||
Assert if asserted object is *equal* to otherValue. This means that object compared by its actual content, not just reference equality. | ||
```javascript | ||
var args = (function(){ return arguments; })(1,2,3); | ||
args.should.be.arguments; | ||
[].should.not.be.arguments; | ||
({ foo: 'bar' }).should.eql({ foo: 'bar' }); | ||
[1,2,3].should.eql([1,2,3]); | ||
// see next example it is correct, even if it is different types, but actual content the same | ||
[1, 2, 3].should.eql({ '0': 1, '1': 2, '2': 3 }); | ||
``` | ||
## empty | ||
## .equal(otherValue) and .exactly(otherValue) | ||
Asserts that given object is empty | ||
Assert if asserted object strictly equal to `otherValue` (using `===` - no type conversion for primitive types and reference equivalence for reference types). | ||
```javascript | ||
[].should.be.empty | ||
''.should.be.empty | ||
({}).should.be.empty | ||
(function() { | ||
arguments.should.be.empty; | ||
})() | ||
(4).should.equal(4); | ||
'test'.should.equal('test'); | ||
[1,2,3].should.not.equal([1,2,3]); | ||
(4).should.be.exactly(4); | ||
``` | ||
## eql | ||
equality: | ||
## .startWith(str) | ||
Assert that string starts with `str`. | ||
```javascript | ||
({ foo: 'bar' }).should.eql({ foo: 'bar' }) | ||
[1,2,3].should.eql([1,2,3]) | ||
'foobar'.should.startWith('foo'); | ||
'foobar'.should.not.startWith('bar'); | ||
``` | ||
## equal and exactly | ||
## .endWith(str) | ||
strict equality: | ||
Assert that string ends with `str`. | ||
```javascript | ||
should.strictEqual(undefined, value) | ||
should.strictEqual(false, value) | ||
(4).should.equal(4) | ||
'test'.should.equal('test') | ||
[1,2,3].should.not.equal([1,2,3]) | ||
(4).should.be.exactly(4) | ||
'foobar'.should.endWith('bar'); | ||
'foobar'.should.not.endWith('foo'); | ||
``` | ||
## within | ||
Assert inclusive numeric range: | ||
## .within(from, to) | ||
Assert inclusive numeric range (`<= to` and `>= from`): | ||
```javascript | ||
user.age.should.be.within(5, 50) | ||
user.age.should.be.within(5, 50); | ||
(5).should.be.within(5, 10).and.within(5, 5); | ||
``` | ||
## approximately | ||
Assert floating point number: | ||
## .approximately(num, delta) | ||
Assert floating point number near `num` within `delta` margin: | ||
```javascript | ||
(99.99).should.be.approximately(100, 0.1); | ||
``` | ||
## type | ||
Assert __typeof__: | ||
## .above(num) and .greaterThan(num) | ||
Assert numeric value above the given value (`> num`): | ||
```javascript | ||
user.should.be.type('object') | ||
'test'.should.be.type('string') | ||
user.age.should.be.above(5); | ||
user.age.should.not.be.above(100); | ||
(5).should.be.above(0); | ||
(5).should.not.be.above(5); | ||
``` | ||
## instanceof and instanceOf | ||
Assert __instanceof__ or __instanceOf__: | ||
## .below(num) and .lessThan(num) | ||
Assert numeric value below the given value (`< num`): | ||
```javascript | ||
user.should.be.an.instanceof(User) | ||
[].should.be.an.instanceOf(Array) | ||
user.age.should.be.below(100); | ||
user.age.should.not.be.below(5); | ||
(5).should.be.below(6); | ||
(5).should.not.be.below(5); | ||
``` | ||
## above | ||
Assert numeric value above the given value: | ||
## .NaN | ||
Assert numeric value is NaN: | ||
```javascript | ||
user.age.should.be.above(5) | ||
user.age.should.not.be.above(100) | ||
(undefined + 0).should.be.NaN; | ||
``` | ||
## below | ||
Assert numeric value below the given value: | ||
## .Infinity | ||
Assert numeric value is Infinity: | ||
```javascript | ||
user.age.should.be.below(100) | ||
user.age.should.not.be.below(5) | ||
(1/0).should.be.Infinity; | ||
``` | ||
## NaN | ||
Assert numeric valus is NaN: | ||
## .type(str) | ||
Assert given value have such type (using __typeof__ operator): | ||
```javascript | ||
(undefined + 0).should.be.NaN; | ||
user.should.be.type('object'); | ||
'test'.should.be.type('string'); | ||
``` | ||
## Infinity | ||
Assert numeric valus is Infinity: | ||
## .instanceof(constructor) and .instanceOf(constructor) | ||
Assert given value is instance of `constructor` (using __instanceof__ operator): | ||
```javascript | ||
(1/0).should.be.Infinity; | ||
user.should.be.an.instanceof(User); | ||
[].should.be.an.instanceOf(Array); | ||
``` | ||
## match | ||
Assert regexp match: | ||
## .arguments | ||
Assert given object is an `Arguments`: | ||
```javascript | ||
username.should.match(/^\w+$/) | ||
var args = (function(){ return arguments; })(1,2,3); | ||
args.should.be.arguments; | ||
[].should.not.be.arguments; | ||
``` | ||
## length | ||
Assert _length_ property exists and has a value of the given number: | ||
## .Object, .Number, .Array, .Boolean, .Function, .String, .Error | ||
Assert given object is instance of such constructor (shortcut for `.instanceof` assertion). | ||
```javascript | ||
user.pets.should.have.length(5) | ||
user.pets.should.have.a.lengthOf(5) | ||
({ length: 10}).should.have.length(10); | ||
({}).should.be.an.Object; | ||
(1).should.be.a.Number; | ||
[].should.be.an.Array.and.an.Object; | ||
(true).should.be.a.Boolean; | ||
''.should.be.a.String; | ||
``` | ||
Aliases: _lengthOf_ | ||
## property | ||
## .property(name[, value]) | ||
Assert property exists and has optional value(compare using `===`): | ||
Assert property exists and has optional value(compare using `.eql`): | ||
```javascript | ||
user.should.have.property('name') | ||
user.should.have.property('age', 15) | ||
user.should.not.have.property('rawr') | ||
user.should.not.have.property('age', 0) | ||
user.should.have.property('name'); | ||
user.should.have.property('age', 15); | ||
user.should.not.have.property('rawr'); | ||
user.should.not.have.property('age', 0); | ||
[1, 2].should.have.property('0', 1); | ||
``` | ||
## properties | ||
__NB__ `.property` change object to actual property value! | ||
Assert given properties exists: | ||
## .properties(propName1, propName2, ...) or .properties([propName1, propName2, ...]) or .properties(obj) | ||
`obj` it is object that map properties to their actual values. | ||
Assert all given properties exists and have given values (compare using `.eql`): | ||
```javascript | ||
user.should.have.properties('name', 'age'); | ||
user.should.have.properties(['name', 'age']); | ||
user.should.have.properties({ | ||
name: 'denis', | ||
age: 24 | ||
}); | ||
``` | ||
## ownProperty | ||
Assert own property (on the immediate object): | ||
## .length(number) and .lengthOf(number) | ||
Assert _length_ property exists and has a value of the given number (shortcut for `.property('length', number)`): | ||
```javascript | ||
({ foo: 'bar' }).should.have.ownProperty('foo') | ||
user.pets.should.have.length(5); | ||
user.pets.should.have.a.lengthOf(5); | ||
({ length: 10}).should.have.length(10); | ||
``` | ||
## status(code) | ||
Asserts that `.statusCode` is `code`: | ||
__NB__ `.length` change object to actual property value! | ||
## .ownProperty(str) and .hasOwnProperty(str) | ||
Assert given object has own property (using `.hasOwnProperty`): | ||
```javascript | ||
res.should.have.status(200); | ||
({ foo: 'bar' }).should.have.ownProperty('foo').equal('bar'); | ||
``` | ||
## header(field[, value]) | ||
Asserts that a `.headers` object with `field` and optional `value` are present: | ||
__NB__ `.length` change object to actual property value! | ||
## .empty | ||
Assert given value is empty. It means for strings, arrays, arguments length == 0 and for object do not have own properties. | ||
```javascript | ||
res.should.have.header('content-length'); | ||
res.should.have.header('Content-Length', '123'); | ||
res.should.have.header('content-length', '123'); | ||
[].should.be.empty; | ||
''.should.be.empty; | ||
({}).should.be.empty; | ||
(function() { | ||
arguments.should.be.empty; | ||
})(); | ||
``` | ||
## json | ||
Assert that Content-Type is "application/json; charset=utf-8" | ||
## .keys([key1, key2, ...]) and .keys(key1, key2, ...) and .key(key) | ||
Assert own object keys, which must match _exactly_, | ||
and will fail if you omit a key or two: | ||
```javascript | ||
res.should.be.json | ||
var obj = { foo: 'bar', baz: 'raz' }; | ||
obj.should.have.keys('foo', 'baz'); | ||
obj.should.have.keys(['foo', 'baz']); | ||
({}).should.have.keys(); | ||
({}).should.have.keys('key'); //fail AssertionError: expected {} to have key 'key'missing keys: 'key' | ||
``` | ||
## html | ||
Assert that Content-Type is "text/html; charset=utf-8" | ||
## .containEql(otherValue) | ||
Assert given value to contain something *.eql* to otherValue. See examples to understand better: | ||
```javascript | ||
res.should.be.html | ||
'hello boy'.should.containEql('boy'); | ||
[1,2,3].should.containEql(3); | ||
[[1],[2],[3]].should.containEql([3]); | ||
[[1],[2],[3, 4]].should.not.containEql([3]); | ||
({ b: 10 }).should.containEql({ b: 10 }); | ||
([1, 2, { a: 10 }]).should.containEql({ a: 10 }); | ||
[1, 2, 3].should.not.containEql({ a: 1 }); | ||
[{a: 'a'}, {b: 'b', c: 'c'}].should.containEql({a: 'a'}); | ||
[{a: 'a'}, {b: 'b', c: 'c'}].should.not.containEql({b: 'b'}); | ||
``` | ||
## include(obj) or contain(obj) | ||
## .containDeep(otherValue) | ||
Assert that the given `obj` is present via `indexOf()`, so this works for strings, arrays, or custom objects implementing indexOf. Also it can assert if given object will have some sub-object. | ||
Assert given value to contain something *.eql* to otherValue within depth. | ||
Again see examples: | ||
Assert array value: | ||
```javascript | ||
[1,2,3].should.include(3) | ||
[1,2,3].should.include(2) | ||
[1,2,3].should.not.include(4) | ||
'hello boy'.should.containDeep('boy'); | ||
[1,2,3].should.containDeep([3]); | ||
[1,2,3].should.containDeep([1, 3]); | ||
//but not | ||
[1,2,3].should.containDeep([3, 1]); | ||
({ a: { b: 10 }, b: { c: 10, d: 11, a: { b: 10, c: 11} }}).should | ||
.containDeep({ a: { b: 10 }, b: { c: 10, a: { c: 11 }}}); | ||
[1, 2, 3, { a: { b: { d: 12 }}}].should.containDeep([{ a: { b: {d: 12}}}]); | ||
[[1],[2],[3]].should.containDeep([[3]]); | ||
[[1],[2],[3, 4]].should.containDeep([[3]]); | ||
[{a: 'a'}, {b: 'b', c: 'c'}].should.containDeep([{a: 'a'}]); | ||
[{a: 'a'}, {b: 'b', c: 'c'}].should.containDeep([{b: 'b'}]); | ||
``` | ||
Assert substring: | ||
It does not search somewhere in depth it check all pattern in depth. Object checked | ||
by properties key and value, arrays checked like sub sequences. Everyting compared using .eql. | ||
Main difference with `.containEql` is that this assertion require full type chain - | ||
if asserted value is an object, otherValue should be also an object (which is sub object of given). | ||
The same true for arrays, otherValue should be an array which compared to be subsequence of given object. | ||
## .match(otherValue) | ||
Assert given object to match `otherValue`. | ||
Given: String, otherValue: regexp. Uses `RegExp#exec(str)`: | ||
```javascript | ||
'foo bar baz'.should.include('foo') | ||
'foo bar baz'.should.include('bar') | ||
'foo bar baz'.should.include('baz') | ||
'foo bar baz'.should.not.include('FOO') | ||
username.should.match(/^\w+$/) | ||
``` | ||
Assert object includes another object: | ||
Given: Array, otherValue: regexp - assert each value match to regexp. | ||
```javascript | ||
var tobi = { name: 'Tobi', age: 1 }; | ||
var jane = { name: 'Jane', age: 5 }; | ||
var user = { name: 'TJ', pet: tobi }; | ||
['a', 'b', 'c'].should.match(/[a-z]/); | ||
['a', 'b', 'c'].should.not.match(/[d-z]/); | ||
``` | ||
user.should.include({ pet: tobi }); | ||
user.should.include({ pet: tobi, name: 'TJ' }); | ||
user.should.not.include({ pet: jane }); | ||
user.should.not.include({ name: 'Someone' }); | ||
Given: Object, otherValue: regexp - assert own property's values to match regexp. | ||
```javascript | ||
({ a: 'foo', c: 'barfoo' }).should.match(/foo$/); | ||
({ a: 'a' }).should.not.match(/^http/); | ||
``` | ||
## includeEql(obj) | ||
Assert that an object equal to the given `obj` is present in an Array: | ||
Given: Anything, otherValue: function - assert if given value matched to function. | ||
Function can use .should inside or return 'true' or 'false', in all other cases it do nothing. If you return value that return assertion, you will receive better error messages. | ||
```javascript | ||
[[1],[2],[3]].should.includeEql([3]) | ||
[[1],[2],[3]].should.includeEql([2]) | ||
[[1],[2],[3]].should.not.includeEql([4]) | ||
(5).should.match(function(n) { return n > 0; }); | ||
(5).should.not.match(function(n) { return n < 0; }); | ||
(5).should.not.match(function(it) { it.should.be.an.Array; }); | ||
(5).should.match(function(it) { return it.should.be.a.Number; }); | ||
``` | ||
## throw() | ||
Now compare messages: | ||
```javascript | ||
(5).should.not.match(function(it) { it.should.be.a.Number; }); | ||
//AssertionError: expected 5 not to match [Function] | ||
(5).should.not.match(function(it) { return it.should.be.a.Number; }); | ||
//AssertionError: expected 5 not to match [Function] | ||
// expected 5 to be a number | ||
``` | ||
Given: object, otherValue: another object - assert that object properties match to properties of another object in meaning that describe above cases. See examples: | ||
```javascript | ||
({ a: 10, b: 'abc', c: { d: 10 }, d: 0 }).should | ||
.match({ a: 10, b: /c$/, c: function(it) { return it.should.have.property('d', 10); }}); | ||
[10, 'abc', { d: 10 }, 0].should | ||
.match({ '0': 10, '1': /c$/, '2': function(it) { return it.should.have.property('d', 10); } }); | ||
[10, 'abc', { d: 10 }, 0].should | ||
.match([10, /c$/, function(it) { return it.should.have.property('d', 10); }]); | ||
``` | ||
## .matchEach(otherValue) | ||
Assert given property keys and values each match given check object. | ||
If `otherValue` is RegExp, then each property value checked to match it: | ||
```javascript | ||
(['a', 'b', 'c']).should.matchEach(/[a-c]/); | ||
``` | ||
If `otherValue` is Function, then check each property value and key matched it: | ||
```javascript | ||
[10, 11, 12].should.matchEach(function(it) { return it >= 10; }); | ||
[10, 11, 12].should.matchEach(function(it) { return it >= 10; }); | ||
``` | ||
In other cases it check that each property value is `.eql` to `otherValue`: | ||
```javascript | ||
[10, 10].should.matchEach(10); | ||
``` | ||
## .throw() and throwError() | ||
Assert an exception is thrown: | ||
@@ -346,49 +520,78 @@ | ||
## throwError() | ||
If you need to pass arguments and/or context to execute function use `Function#bind(context, arg1, ...)`: | ||
```js | ||
function isPositive(n) { | ||
if(n <= 0) throw new Error('Given number is not positive') | ||
} | ||
An alias of `throw`, its purpose is to be an option for those who run | ||
[jshint](https://github.com/jshint/node-jshint/) in strict mode. | ||
isPositive.bind(null, 10).should.not.throw(); | ||
isPositive.bind(null, -10).should.throw(); | ||
``` | ||
If you need to check something in asynchronous function it is required to do in 2 steps: | ||
```js | ||
(function(){ | ||
throw new Error('failed to baz'); | ||
}).should.throwError(/^fail.*/); | ||
// first we need to check that function is called | ||
var called = false; | ||
collection.findOne({ _id: 10 }, function(err, res) { | ||
called = true; | ||
//second we test what you want | ||
res.should.be.... | ||
}); | ||
called.should.be.true; | ||
``` | ||
## startWith(str) | ||
In case you are using something like `Mocha`, you should use asynchronous test and call `done()` in proper place to make sure that you asynchronous function is called before test is finished. | ||
```js | ||
collection.findOne({ _id: 10 }, function(err, res) { | ||
if(err) return done(err); | ||
//second we test what you want | ||
res.should.be.... | ||
done(); | ||
}); | ||
``` | ||
Assert that string starts with `str`. | ||
In general case if you need to check that something is executed you need such thing as `spies`, good example is an [sinon](http://sinonjs.org/). | ||
## .status(code) | ||
Asserts that `.statusCode` is `code`: | ||
```javascript | ||
'foobar'.should.startWith('foo') | ||
'foobar'.should.not.startWith('bar') | ||
res.should.have.status(200); | ||
``` | ||
## endWith(str) | ||
Assert that string ends with `str`. | ||
Not included in browser build. | ||
## .header(field[, value]) | ||
Asserts that a `.headers` object with `field` and optional `value` are present: | ||
```javascript | ||
'foobar'.should.endWith('bar') | ||
'foobar'.should.not.endWith('foo') | ||
res.should.have.header('content-length'); | ||
res.should.have.header('Content-Length', '123'); | ||
``` | ||
## keys | ||
Not included in browser build. | ||
Assert own object keys, which must match _exactly_, | ||
and will fail if you omit a key or two: | ||
## .json | ||
var obj = { foo: 'bar', baz: 'raz' }; | ||
obj.should.have.keys('foo', 'baz'); | ||
obj.should.have.keys(['foo', 'baz']); | ||
Assert that Content-Type is "application/json; charset=utf-8" | ||
## type assertions | ||
```javascript | ||
res.should.be.json | ||
``` | ||
Not included in browser build. | ||
## .html | ||
Assert that Content-Type is "text/html; charset=utf-8" | ||
```javascript | ||
({}).should.be.an.Object; | ||
(1).should.be.an.Number; | ||
[].should.be.an.Array; | ||
(true).should.be.a.Boolean; | ||
''.should.be.a.String; | ||
res.should.be.html | ||
``` | ||
Not included in browser build. | ||
## Optional Error description | ||
@@ -400,7 +603,7 @@ | ||
AssertionError: expected 1 to equal 0 | some useful description | ||
AssertionError: some useful description | ||
at Object.eql (/Users/swift/code/should.js/node_modules/should/lib/should.js:280:10) | ||
... | ||
The methods that support this optional description are: `eql`, `equal`, `within`, `a`, `instanceof`, `above`, `below`, `match`, `length`, `property`, `ownProperty`, `include`, and `includeEql`. | ||
The methods that support this optional description are: `eql`, `equal`, `within`, `instanceof`, `above`, `below`, `match`, `length`, `property`, `ownProperty`. | ||
@@ -423,8 +626,20 @@ ## Mocha example | ||
## Running tests | ||
## Contributions | ||
[Actual list of contributors](https://github.com/visionmedia/should.js/graphs/contributors) if you want to show it your friends. | ||
To run the tests for _should_ simply run: | ||
$ make test | ||
Before contribute something: | ||
1. Your code looks like all other code - all project should look like it was written by one man, always. | ||
2. If you want propose something - just create issue and describe your question with much description as you can. | ||
3. Please never send issues or pull requests about code style, jshint violations etc - i do not accept it (and you will spend your time for free). | ||
4. If you think you have some general receipt, consider create PR with it. | ||
5. If you not sure if you receipt enough general just create your own plugin for should.js. (see should.use and Assertion.add usage). | ||
6. If you add new code it should be covered by tests, no tests - no code. | ||
7. If you find bug (or at least you think it is a bug), create issue with library version and test case that I can run and see what are you talking about, or at least full steps that I can reproduce it. | ||
## OMG IT EXTENDS OBJECT???!?!@ | ||
@@ -436,25 +651,2 @@ | ||
(The MIT License) | ||
Copyright (c) 2010-2011 TJ Holowaychuk <tj@vision-media.ca> | ||
Copyright (c) 2011 Aseem Kishore <aseem.kishore@gmail.com> | ||
Permission is hereby granted, free of charge, to any person obtaining | ||
a copy of this software and associated documentation files (the | ||
'Software'), to deal in the Software without restriction, including | ||
without limitation the rights to use, copy, modify, merge, publish, | ||
distribute, sublicense, and/or sell copies of the Software, and to | ||
permit persons to whom the Software is furnished to do so, subject to | ||
the following conditions: | ||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | ||
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY | ||
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, | ||
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | ||
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
MIT |
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
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
177677
39
645
2733
1
6