Skip to content

Commit

Permalink
Make should a function and expose it to world. Assert module merged t…
Browse files Browse the repository at this point in the history
…o it. Closes tj#43.
  • Loading branch information
btd committed Sep 13, 2013
1 parent 2ed049c commit 0328be8
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 21 deletions.
1 change: 1 addition & 0 deletions History.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

* expose should to exports, to do not require Object.prototype only
* fixed usage of valueOf only for standard wrappers
* updated eql.js to current version from assert.js
* fixed object comparison in eql.js [dirtyrottenscoundrel]
Expand Down
6 changes: 5 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ _should_ is an expressive, readable, test framework agnostic, assertion library

It extends the Object prototype with a single non-enumerable getter that allows you to express how that object should behave.

_should_ literally extends node's _assert_ module, in fact, it is node's assert module, for example `should.equal(str, 'foo')` will work, just as `assert.equal(str, 'foo')` would, and `should.AssertionError` **is** `assert.AssertionError`, meaning any test framework supporting this constructor will function properly with _should_.
_should_ literally extends node's _assert_ module, for example `should.equal(str, 'foo')` will work, just as `assert.equal(str, 'foo')` would, and `should.AssertionError` **is** `assert.AssertionError`, meaning any test framework supporting this constructor will function properly with _should_.

## Example
```javascript
Expand All @@ -14,6 +14,10 @@ var user = {
user.should.have.property('name', 'tj');
user.should.have.property('pets').with.lengthOf(4);

// or without Object.prototype, for guys how did Object.create(null)

should(user).have.property('name', 'tj');

someAsyncTask(foo, function(err, result){
should.not.exist(err);
should.exist(result);
Expand Down
46 changes: 27 additions & 19 deletions lib/should.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,25 @@ var util = require('./util')
, i = require('util').inspect;

/**
* Expose assert as should.
* Our function should
* @param obj
* @returns {Assertion}
*/
var should = function(obj) {
return new Assertion(util.isWrapperType(obj) ? obj.valueOf(): obj);
};

/**
* Expose assert to should
*
* This allows you to do things like below
* without require()ing the assert module.
*
* should.equal(foo.bar, undefined);
*
*/
util.merge(should, assert);

exports = module.exports = assert;

/**
* Assert _obj_ exists, with optional message.
Expand All @@ -35,12 +44,11 @@ exports = module.exports = assert;
* @param {String} [msg]
* @api public
*/

exports.exist = exports.exists = function(obj, msg){
if (null == obj) {
should.exist = should.exists = function(obj, msg) {
if(null == obj) {
throw new AssertionError({
message: msg || ('expected ' + i(obj) + ' to exist')
, stackStartFunction: exports.exist
message: msg || ('expected ' + i(obj) + ' to exist')
, stackStartFunction: should.exist
});
}
};
Expand All @@ -53,16 +61,22 @@ exports.exist = exports.exists = function(obj, msg){
* @api public
*/

exports.not = {};
exports.not.exist = exports.not.exists = function(obj, msg){
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: exports.not.exist
message: msg || ('expected ' + i(obj) + ' to not exist')
, stackStartFunction: should.not.exist
});
}
};

/**
* Expose should to external world.
*/
exports = module.exports = should;


/**
* Expose api via `Object#should`.
*
Expand All @@ -72,7 +86,7 @@ exports.not.exist = exports.not.exists = function(obj, msg){
Object.defineProperty(Object.prototype, 'should', {
set: function(){},
get: function(){
return new Assertion(util.isWrapperType(this) ? this.valueOf() : this);
return should(this);
},
configurable: true
});
Expand All @@ -84,7 +98,7 @@ Object.defineProperty(Object.prototype, 'should', {
* @api private
*/

var Assertion = exports.Assertion = function Assertion(obj) {
var Assertion = should.Assertion = function Assertion(obj) {
this.obj = obj;
};

Expand All @@ -94,12 +108,6 @@ var Assertion = exports.Assertion = function Assertion(obj) {

Assertion.prototype = {

/**
* HACK: prevents double require() from failing.
*/

exports: exports,

/**
* Assert _expr_ with the given _msg_ and _negatedMsg_.
*
Expand Down
26 changes: 25 additions & 1 deletion lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,28 @@
*/
exports.isWrapperType = function(obj) {
return obj instanceof Number || obj instanceof String || obj instanceof Boolean;
}
}

/**
* Merge object b with object a.
*
* var a = { foo: 'bar' }
* , b = { bar: 'baz' };
*
* utils.merge(a, b);
* // => { foo: 'bar', bar: 'baz' }
*
* @param {Object} a
* @param {Object} b
* @return {Object}
* @api private
*/

exports.merge = function(a, b){
if (a && b) {
for (var key in b) {
a[key] = b[key];
}
}
return a;
};

0 comments on commit 0328be8

Please sign in to comment.