Skip to content

Commit

Permalink
Merge pull request #7 from ianstormtaylor/add/node
Browse files Browse the repository at this point in the history
add node assert methods and tests
  • Loading branch information
tj committed Dec 11, 2013
2 parents 9a9c409 + 0bcd31a commit cd0a4d8
Show file tree
Hide file tree
Showing 7 changed files with 5,919 additions and 35 deletions.
13 changes: 8 additions & 5 deletions Makefile
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
3 changes: 2 additions & 1 deletion component.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"version": "0.2.0",
"keywords": ["assert", "test"],
"dependencies": {
"component/stack": "*"
"component/stack": "*",
"jkroso/equals": "*"
},
"scripts": [
"index.js"
Expand Down
187 changes: 158 additions & 29 deletions index.js
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;
}
14 changes: 14 additions & 0 deletions test/index.html
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>
Loading

0 comments on commit cd0a4d8

Please sign in to comment.