-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Christopher Rogers <chrissrogers@gmail.com>
- Loading branch information
1 parent
163588f
commit b9e0c15
Showing
34 changed files
with
6,992 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
build | ||
components | ||
node_modules |
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,42 @@ | ||
|
||
0.5.1 / 2015-08-19 | ||
================== | ||
|
||
* bump yields/fmt | ||
|
||
0.5.0 / 2014-10-07 | ||
================== | ||
|
||
* index: Fix .throws() and .doesNotThrow() | ||
* add mocha diff | ||
|
||
0.4.0 / 2014-06-30 | ||
================== | ||
|
||
* fix default assertion messages | ||
* add testing server | ||
|
||
0.3.1 / 2014-06-03 | ||
================== | ||
|
||
* fix custom assertion messages being ignored | ||
|
||
0.3.0 / 2013-12-10 | ||
================== | ||
|
||
* add node assert methods | ||
|
||
0.2.0 / 2013-08-15 | ||
================== | ||
|
||
* update to work with latest component/stack. | ||
|
||
0.1.1 / 2012-10-09 | ||
================== | ||
|
||
* fix paren balancing with greedy capture | ||
|
||
0.1.0 / 2012-10-09 | ||
================== | ||
|
||
* add callsite support for auto-generated messages |
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,20 @@ | ||
|
||
build: components index.js | ||
@component build --dev | ||
|
||
components: component.json | ||
@component install --dev | ||
|
||
clean: | ||
@rm -fr build components | ||
|
||
node_modules: package.json | ||
@npm install | ||
|
||
server: node_modules build | ||
@node test/server.js | ||
|
||
test: build | ||
@open http://localhost:7575 | ||
|
||
.PHONY: clean server test |
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,25 @@ | ||
|
||
# assert | ||
|
||
C-style assertion lib. | ||
|
||
## Example | ||
|
||
With custom assertion message: | ||
|
||
```js | ||
var assert = require('assert'); | ||
assert(expr, 'oh no it broke'); | ||
``` | ||
|
||
Or auto-generated assertion message in | ||
browsers that support `Error.captureStackTrace()`: | ||
|
||
```js | ||
var assert = require('assert'); | ||
assert(user.name == 'Tobi'); | ||
``` | ||
|
||
## License | ||
|
||
MIT |
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,18 @@ | ||
{ | ||
"name": "assert", | ||
"repo": "component/assert", | ||
"description": "Assertion lib", | ||
"version": "0.5.1", | ||
"keywords": [ | ||
"assert", | ||
"test" | ||
], | ||
"dependencies": { | ||
"component/stack": "*", | ||
"jkroso/equals": "*", | ||
"yields/fmt": "0.1.0" | ||
}, | ||
"scripts": [ | ||
"index.js" | ||
] | ||
} |
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,6 @@ | ||
<html> | ||
<body> | ||
<script src="build/build.js"></script> | ||
<script src="example.js"></script> | ||
</body> | ||
</html> |
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,13 @@ | ||
|
||
/** | ||
* Module dependencies. | ||
*/ | ||
|
||
var assert = require('assert'); | ||
|
||
function test() { | ||
var user = { name: 'Tobi' }; | ||
assert(user.name == 'tobi'); | ||
} | ||
|
||
test(); |
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,203 @@ | ||
|
||
/** | ||
* Module dependencies. | ||
*/ | ||
|
||
var equals = require('equals'); | ||
var fmt = require('fmt'); | ||
var stack = require('stack'); | ||
|
||
/** | ||
* Assert `expr` with optional failure `msg`. | ||
* | ||
* @param {Mixed} expr | ||
* @param {String} [msg] | ||
* @api public | ||
*/ | ||
|
||
module.exports = exports = function (expr, msg) { | ||
if (expr) return; | ||
throw error(msg || message()); | ||
}; | ||
|
||
/** | ||
* Assert `actual` is weak equal to `expected`. | ||
* | ||
* @param {Mixed} actual | ||
* @param {Mixed} expected | ||
* @param {String} [msg] | ||
* @api public | ||
*/ | ||
|
||
exports.equal = function (actual, expected, msg) { | ||
if (actual == expected) return; | ||
throw error(msg || fmt('Expected %o to equal %o.', actual, expected), actual, expected); | ||
}; | ||
|
||
/** | ||
* Assert `actual` is not weak equal to `expected`. | ||
* | ||
* @param {Mixed} actual | ||
* @param {Mixed} expected | ||
* @param {String} [msg] | ||
* @api public | ||
*/ | ||
|
||
exports.notEqual = function (actual, expected, msg) { | ||
if (actual != expected) return; | ||
throw error(msg || fmt('Expected %o not to equal %o.', actual, expected)); | ||
}; | ||
|
||
/** | ||
* Assert `actual` is deep equal to `expected`. | ||
* | ||
* @param {Mixed} actual | ||
* @param {Mixed} expected | ||
* @param {String} [msg] | ||
* @api public | ||
*/ | ||
|
||
exports.deepEqual = function (actual, expected, msg) { | ||
if (equals(actual, expected)) return; | ||
throw error(msg || fmt('Expected %o to deeply equal %o.', actual, expected), actual, expected); | ||
}; | ||
|
||
/** | ||
* Assert `actual` is not deep equal to `expected`. | ||
* | ||
* @param {Mixed} actual | ||
* @param {Mixed} expected | ||
* @param {String} [msg] | ||
* @api public | ||
*/ | ||
|
||
exports.notDeepEqual = function (actual, expected, msg) { | ||
if (!equals(actual, expected)) return; | ||
throw error(msg || fmt('Expected %o not to deeply equal %o.', actual, expected)); | ||
}; | ||
|
||
/** | ||
* Assert `actual` is strict equal to `expected`. | ||
* | ||
* @param {Mixed} actual | ||
* @param {Mixed} expected | ||
* @param {String} [msg] | ||
* @api public | ||
*/ | ||
|
||
exports.strictEqual = function (actual, expected, msg) { | ||
if (actual === expected) return; | ||
throw error(msg || fmt('Expected %o to strictly equal %o.', actual, expected), actual, expected); | ||
}; | ||
|
||
/** | ||
* Assert `actual` is not strict equal to `expected`. | ||
* | ||
* @param {Mixed} actual | ||
* @param {Mixed} expected | ||
* @param {String} [msg] | ||
* @api public | ||
*/ | ||
|
||
exports.notStrictEqual = function (actual, expected, msg) { | ||
if (actual !== expected) return; | ||
throw error(msg || fmt('Expected %o not to strictly equal %o.', actual, expected)); | ||
}; | ||
|
||
/** | ||
* Assert `block` throws an `error`. | ||
* | ||
* @param {Function} block | ||
* @param {Function} [error] | ||
* @param {String} [msg] | ||
* @api public | ||
*/ | ||
|
||
exports.throws = function (block, err, msg) { | ||
var threw; | ||
try { | ||
block(); | ||
} catch (e) { | ||
threw = e; | ||
} | ||
|
||
if (!threw) throw error(msg || fmt('Expected %s to throw an error.', block.toString())); | ||
if (err && !(threw instanceof err)) { | ||
throw error(msg || fmt('Expected %s to throw an %o.', block.toString(), err)); | ||
} | ||
}; | ||
|
||
/** | ||
* Assert `block` doesn't throw an `error`. | ||
* | ||
* @param {Function} block | ||
* @param {Function} [error] | ||
* @param {String} [msg] | ||
* @api public | ||
*/ | ||
|
||
exports.doesNotThrow = function (block, err, msg) { | ||
var threw; | ||
try { | ||
block(); | ||
} catch (e) { | ||
threw = e; | ||
} | ||
|
||
if (threw) throw error(msg || fmt('Expected %s not to throw an error.', block.toString())); | ||
if (err && (threw instanceof err)) { | ||
throw error(msg || fmt('Expected %s not to throw an %o.', block.toString(), err)); | ||
} | ||
}; | ||
|
||
/** | ||
* Create a message from the call stack. | ||
* | ||
* @return {String} | ||
* @api private | ||
*/ | ||
|
||
function message() { | ||
if (!Error.captureStackTrace) return 'assertion failed'; | ||
var callsite = stack()[2]; | ||
var fn = callsite.getFunctionName(); | ||
var file = callsite.getFileName(); | ||
var line = callsite.getLineNumber() - 1; | ||
var col = callsite.getColumnNumber() - 1; | ||
var src = get(file); | ||
line = src.split('\n')[line].slice(col); | ||
var m = line.match(/assert\((.*)\)/); | ||
return m && m[1].trim(); | ||
} | ||
|
||
/** | ||
* Load contents of `script`. | ||
* | ||
* @param {String} script | ||
* @return {String} | ||
* @api private | ||
*/ | ||
|
||
function get(script) { | ||
var xhr = new XMLHttpRequest; | ||
xhr.open('GET', script, false); | ||
xhr.send(null); | ||
return xhr.responseText; | ||
} | ||
|
||
/** | ||
* Error with `msg`, `actual` and `expected`. | ||
* | ||
* @param {String} msg | ||
* @param {Mixed} actual | ||
* @param {Mixed} expected | ||
* @return {Error} | ||
*/ | ||
|
||
function error(msg, actual, expected){ | ||
var err = new Error(msg); | ||
err.showDiff = 3 == arguments.length; | ||
err.actual = actual; | ||
err.expected = expected; | ||
return err; | ||
} |
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,8 @@ | ||
{ | ||
"name": "assert", | ||
"private": true, | ||
"dependencies": { | ||
"express": "^4.4.5", | ||
"hbs": "^2.7.0" | ||
} | ||
} |
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,14 @@ | ||
<html> | ||
<head> | ||
<title>assert tests</title> | ||
<link rel="stylesheet" href="test/mocha.css" /> | ||
</head> | ||
<body> | ||
<div id="mocha"></div> | ||
<script src="test/mocha.js"></script> | ||
<script>mocha.setup({ui: 'bdd'})</script> | ||
<script src="build/build.js"></script> | ||
<script src="test/index.js"></script> | ||
<script>mocha.run();</script> | ||
</body> | ||
</html> |
Oops, something went wrong.