-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from ianstormtaylor/add/node
add node assert methods and tests
- Loading branch information
Showing
7 changed files
with
5,919 additions
and
35 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 |
---|---|---|
@@ -1,11 +1,14 @@ | ||
|
||
build: components index.js | ||
@component build | ||
@component build --dev | ||
|
||
components: | ||
@Component install | ||
components: component.json | ||
@component install --dev | ||
|
||
clean: | ||
rm -fr build components | ||
@rm -fr build components | ||
|
||
.PHONY: clean | ||
test: build | ||
@open test/index.html | ||
|
||
.PHONY: clean 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
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 |
---|---|---|
@@ -1,49 +1,178 @@ | ||
|
||
/** | ||
* Module dependencies. | ||
*/ | ||
|
||
var stack = require('stack'); | ||
var equals = require('equals'); | ||
|
||
/** | ||
* Load contents of `script`. | ||
* Assert `expr` with optional failure `msg`. | ||
* | ||
* @param {String} script | ||
* @return {String} | ||
* @api private | ||
* @param {Mixed} expr | ||
* @param {String} [msg] | ||
* @api public | ||
*/ | ||
|
||
function getScript(script) { | ||
var xhr = new XMLHttpRequest; | ||
xhr.open('GET', script, false); | ||
xhr.send(null); | ||
return xhr.responseText; | ||
} | ||
module.exports = exports = function (expr, msg) { | ||
if (expr) return; | ||
throw new Error(message()); | ||
}; | ||
|
||
/** | ||
* Assert `expr` with optional failure `msg`. | ||
* Assert `actual` is weak equal to `expected`. | ||
* | ||
* @param {Mixed} expr | ||
* @param {Mixed} actual | ||
* @param {Mixed} expected | ||
* @param {String} [msg] | ||
* @api public | ||
*/ | ||
|
||
module.exports = function(expr, msg){ | ||
if (expr) return; | ||
if (!msg) { | ||
if (Error.captureStackTrace) { | ||
var callsite = stack()[1]; | ||
var fn = callsite.getFunctionName(); | ||
var file = callsite.getFileName(); | ||
var line = callsite.getLineNumber() - 1; | ||
var col = callsite.getColumnNumber() - 1; | ||
var src = getScript(file); | ||
line = src.split('\n')[line].slice(col); | ||
expr = line.match(/assert\((.*)\)/)[1].trim(); | ||
msg = expr; | ||
} else { | ||
msg = 'assertion failed'; | ||
} | ||
exports.equal = function (actual, expected, msg) { | ||
if (actual == expected) return; | ||
throw new Error(message()); | ||
}; | ||
|
||
/** | ||
* 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 new Error(message()); | ||
}; | ||
|
||
/** | ||
* 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 new Error(message()); | ||
}; | ||
|
||
/** | ||
* 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 new Error(message()); | ||
}; | ||
|
||
/** | ||
* 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 new Error(message()); | ||
}; | ||
|
||
/** | ||
* 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 new Error(message()); | ||
}; | ||
|
||
/** | ||
* Assert `block` throws an `error`. | ||
* | ||
* @param {Function} block | ||
* @param {Function} [error] | ||
* @param {String} [msg] | ||
* @api public | ||
*/ | ||
|
||
exports.throws = function (block, error, msg) { | ||
var err; | ||
try { | ||
block(); | ||
} catch (e) { | ||
err = e; | ||
} | ||
if (!err) throw new Error(message()); | ||
if (error && !(err instanceof error)) throw new Error(message()); | ||
}; | ||
|
||
throw new Error(msg); | ||
/** | ||
* Assert `block` doesn't throw an `error`. | ||
* | ||
* @param {Function} block | ||
* @param {Function} [error] | ||
* @param {String} [msg] | ||
* @api public | ||
*/ | ||
|
||
exports.doesNotThrow = function (block, error, msg) { | ||
var err; | ||
try { | ||
block(); | ||
} catch (e) { | ||
err = e; | ||
} | ||
if (error && (err instanceof error)) throw new Error(message()); | ||
if (err) throw new Error(message()); | ||
}; | ||
|
||
/** | ||
* 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 = getScript(file); | ||
line = src.split('\n')[line].slice(col); | ||
return line.match(/assert\((.*)\)/)[1].trim(); | ||
} | ||
|
||
/** | ||
* Load contents of `script`. | ||
* | ||
* @param {String} script | ||
* @return {String} | ||
* @api private | ||
*/ | ||
|
||
function getScript(script) { | ||
var xhr = new XMLHttpRequest; | ||
xhr.open('GET', script, false); | ||
xhr.send(null); | ||
return xhr.responseText; | ||
} |
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 test</title> | ||
<link rel="stylesheet" href="mocha.css" /> | ||
</head> | ||
<body> | ||
<div id="mocha"></div> | ||
<script src="mocha.js"></script> | ||
<script>mocha.setup({ui: 'bdd'})</script> | ||
<script src="../build/build.js"></script> | ||
<script src="index.js"></script> | ||
<script>mocha.run();</script> | ||
</body> | ||
</html> |
Oops, something went wrong.