forked from tj/should.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First attempt to split library on smaller peaces
- Loading branch information
Showing
13 changed files
with
401 additions
and
435 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}); | ||
} | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
Oops, something went wrong.