Skip to content

Commit

Permalink
First attempt to split library on smaller peaces
Browse files Browse the repository at this point in the history
  • Loading branch information
btd committed Dec 27, 2013
1 parent 7805a50 commit 988c160
Show file tree
Hide file tree
Showing 13 changed files with 401 additions and 435 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
TESTS=$(shell find test -name *.test.js)

test:
@./node_modules/.bin/mocha \
--ui exports
@./node_modules/.bin/mocha --ui exports --recursive $(TESTS)

browser:
@browserify lib/should.js -s should --dg false -o should.js
Expand Down
53 changes: 53 additions & 0 deletions lib/ext/assert.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
var util = require('../util')
, assert = util.assert
, AssertionError = assert.AssertionError;

module.exports = function(should) {
var i = should.format;

/**
* 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);


/**
* 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
});
}
};

/**
* Asserts _obj_ does not exist, with optional message.
*
* @param {*} obj
* @param {String} [msg]
* @api public
*/

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
});
}
};
};
9 changes: 9 additions & 0 deletions lib/ext/bool.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = function(should, Assertion) {
Assertion.add('true', function() {
this.is.exactly(true)
}, true);

Assertion.add('false', function() {
this.is.exactly(false)
}, true);
};
12 changes: 12 additions & 0 deletions lib/ext/chain.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports = function(should, Assertion) {

function addLink(name) {
Object.defineProperty(Assertion.prototype, name, {
get: function() {
return this;
}
});
}

['an', 'of', 'a', 'and', 'be', 'have', 'with', 'is'].forEach(addLink);
};
17 changes: 17 additions & 0 deletions lib/ext/eql.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
var eql = require('../eql');

module.exports = function(should, Assertion) {
Assertion.add('eql', function(val, description) {
this.params = { operator: 'to equal', expected: val, showDiff: true, message: description };

this.assert2(eql(val, this.obj));
});

Assertion.add('equal', function(val, description) {
this.params = { operator: 'to be', expected: val, showDiff: true, message: description };

this.assert2(val === this.obj);
});

Assertion.alias('equal', 'exactly');
};
29 changes: 29 additions & 0 deletions lib/ext/number.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module.exports = function(should, Assertion) {
Assertion.add('NaN', function() {
this.params = { operator: 'to be NaN' };

this.is.a.Number
.and.assert2(isNaN(this.obj));
}, true);

Assertion.add('Infinity', function() {
this.params = { operator: 'to be Infinity' };

this.is.a.Number
.and.not.a.NaN
.and.assert2(!isFinite(this.obj));
}, true);

Assertion.add('within', function(start, finish, description){
this.params = { operator: 'to be within '+ start + '..' + finish, message: description };

this.assert2(this.obj >= start && this.obj <= finish);
});

Assertion.add('approximately', function(value, delta, description) {
this.params = { operator: 'to be approximately ' + value + " ±" + delta, message: description };

this.assert2(Math.abs(this.obj - value) <= delta);
});

};
9 changes: 9 additions & 0 deletions lib/ext/type.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var util = require('../util');

module.exports = function(should, Assertion) {
Assertion.add('Number', function() {
this.params = { operator: 'to be a number' };

this.assert2(util.isNumber(this.obj));
}, true);
};
Loading

0 comments on commit 988c160

Please sign in to comment.