Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

first commit

  • Loading branch information...
commit d93f0280a6746611a9a68eecee68107fcd4b46d1 0 parents
@caolan authored
0  README.md
No changes.
3  index.js
@@ -0,0 +1,3 @@
+// This file is just added for convenience so this repository can be
+// directly checked out into a project's deps folder
+module.exports = require('./lib/nodeunit');
101 lib/nodeunit.js
@@ -0,0 +1,101 @@
+var assert = require('assert');
+
+
+var Assertion = function(method, message, error){
+ return {
+ method: method,
+ message: message || '',
+ error: error,
+ passed: function(){return !error;},
+ failed: function(){return Boolean(error);}
+ };
+};
+
+var TestEnv = function(options){
+ var expecting;
+ var assertions = [];
+ options.log = options.log || function(){}; // log callback optional
+
+ var wrapAssert = function(new_method, assert_method){
+ return function(){
+ try {
+ assert[assert_method].apply(global, arguments);
+ var message = arguments[arguments.length-1];
+ var assertion = new Assertion(new_method, message);
+ }
+ catch (e){
+ var assertion = new Assertion('', e.message || '', e);
+ }
+ assertions.push(assertion);
+ process.nextTick(function(){options.log(assertion)});
+ };
+ };
+
+ return {
+ done: function(){
+ if(expecting !== undefined && expecting != assertions.length){
+ var err = new Error(
+ 'Expected ' + expecting + ' assertions, ' +
+ assertions.length + ' ran'
+ );
+ var assertion = new Assertion('expect', err.message, err);
+ assertions.push(assertion);
+ process.nextTick(function(){options.log(assertion);});
+ }
+ var failures = assertions.reduce(function(a,x){
+ return x.failed() ? a+1 : a;
+ }, 0);
+ process.nextTick(function(){
+ options.testDone(failures, assertions.length);
+ });
+ },
+ ok: wrapAssert('ok', 'ok'),
+ same: wrapAssert('same', 'deepEqual'),
+ equals: wrapAssert('equals', 'equal'),
+ expect: function(num){
+ expecting = num;
+ }
+ };
+};
+
+
+exports.runTest = function(fn, options){
+ options.log = options.log || function(){}; // log callback optional
+ try {
+ fn(new TestEnv(options));
+ }
+ catch (e){
+ var assertion = new Assertion('', e.message || '', e);
+ process.nextTick(function(){options.log(assertion)});
+ process.nextTick(function(){options.testDone(1, 1)});
+ }
+};
+
+
+exports.runModule = function(mod, options){
+ var m_failures = 0;
+ var m_total = 0;
+ var i = 0;
+ var tests = Object.keys(mod).map(function(k){return mod[k];});
+ var _fn = function(test){
+ (options.testStart || function(){})(test);
+ exports.runTest(test, {
+ log: options.log,
+ testDone: function(failures, total){
+
+ m_failures += failures;
+ m_total += total;
+ (options.testDone || function(){})(failures, total);
+
+ i++;
+ if(i < tests.length){
+ _fn(tests[i]);
+ }
+ else {
+ (options.moduleDone || function(){})(m_failures, m_total);
+ }
+ }
+ });
+ };
+ _fn(tests[0]);
+};
14 test.js
@@ -0,0 +1,14 @@
+#!/usr/local/bin/node
+
+var path = require('path'),
+ fs = require('fs');
+
+require.paths.push(process.cwd());
+require.paths.push(path.join(process.cwd(), 'deps'));
+require.paths.push(path.join(process.cwd(), 'lib'));
+
+fs.readdirSync('test').filter(function(filename){
+ return /\.js$/.exec(filename);
+}).forEach(function(filename){
+ require('test/' + filename.replace(/\.js$/,''));
+});
52 test/test-runmodule.js
@@ -0,0 +1,52 @@
+var assert = require('assert'),
+ sys = require('sys'),
+ nodeunit = require('nodeunit');
+
+
+
+var call_order = [];
+var testmodule = {
+ test1: function(test){
+ call_order.push('test1');
+ test.ok(true, 'ok true');
+ test.done();
+ },
+ test2: function(test){
+ call_order.push('test2');
+ test.ok(false, 'ok false');
+ test.ok(false, 'ok false');
+ test.done();
+ },
+ test3: function(test){
+ call_order.push('test3');
+ test.done();
+ }
+};
+nodeunit.runModule(testmodule, {
+ log: function(assertion){
+ call_order.push('log');
+ },
+ testStart: function(name){
+ call_order.push('testStart');
+ },
+ testDone: function(failures, total){
+ call_order.push('testDone');
+ },
+ moduleDone: function(failures, total){
+ assert.equal(failures, 2);
+ assert.equal(total, 3);
+ call_order.push('moduleDone');
+ }
+});
+
+
+// callbacks are async, so test call order after callbacks have executed
+setTimeout(function(){
+ assert.deepEqual(call_order, [
+ 'testStart', 'test1', 'log', 'testDone',
+ 'testStart', 'test2', 'log', 'log', 'testDone',
+ 'testStart', 'test3', 'testDone',
+ 'moduleDone'
+ ]);
+ sys.puts('test-runmodule OK');
+}, 100);
284 test/test-runtest.js
@@ -0,0 +1,284 @@
+var assert = require('assert'),
+ sys = require('sys'),
+ nodeunit = require('nodeunit');
+
+
+// this is mean to bubble to the top, and will be ignored for the purposes
+// of testing:
+var ignored_error = new Error('ignore this callback error');
+process.addListener('uncaughtException', function(err){
+ if(err && err.message != ignored_error.message){
+ throw err;
+ }
+});
+
+var call_order1 = [];
+var test1 = function(test){
+ call_order1.push('test1');
+ assert.ok(test.expect instanceof Function, 'test.expect');
+ assert.ok(test.done instanceof Function, 'test.done');
+ assert.ok(test.ok instanceof Function, 'test.ok');
+ assert.ok(test.same instanceof Function, 'test.same');
+ assert.ok(test.equals instanceof Function, 'test.equals');
+ test.done();
+};
+nodeunit.runTest(test1, {
+ log: function(assertion){
+ throw assertion.error;
+ },
+ testDone: function(failures, total){
+ assert.equal(failures, 0, 'failures');
+ assert.equal(total, 0, 'total');
+ call_order1.push('testDone');
+ }
+});
+
+
+var err = new Error('test');
+var call_order2 = [];
+var test2 = function(test){
+ call_order2.push('test2');
+ throw err;
+};
+nodeunit.runTest(test2, {
+ log: function(assertion){
+ call_order2.push('log');
+ assert.deepEqual(assertion.error, err, 'assertion.error');
+ },
+ testDone: function(failures, total){
+ assert.equal(failures, 1);
+ assert.equal(total, 1);
+ call_order2.push('testDone');
+ }
+});
+
+
+var call_order3 = [];
+var test3 = function(test){
+ call_order3.push('test3');
+ test.ok(true, 'test.ok');
+ test.done();
+};
+nodeunit.runTest(test3, {
+ log: function(assertion){
+ call_order3.push('log ' + assertion.message);
+ },
+ testDone: function(failures, total){
+ assert.equal(failures, 0, 'failures');
+ assert.equal(total, 1, 'total');
+ call_order3.push('testDone');
+ }
+});
+
+
+// A failing callback should not affect the test outcome
+var call_order4 = [];
+var test4 = function(test){
+ call_order4.push('test4');
+ test.ok(true, 'test.ok');
+ test.done();
+};
+nodeunit.runTest(test4, {
+ log: function(assertion){
+ call_order4.push('log');
+ assert.ok(true, 'log called');
+ throw ignored_error;
+ },
+ testDone: function(failures, total){
+ assert.equal(failures, 0, 'failures');
+ assert.equal(total, 1, 'total');
+ call_order4.push('testDone');
+ }
+});
+
+
+// A failing callback should not affect the test outcome
+var call_order5 = [];
+var test5 = function(test){
+ call_order5.push('test5');
+ test.done();
+};
+nodeunit.runTest(test5, {
+ log: function(assertion){
+ call_order5.push('log');
+ assert.ok(false, 'log should not be called');
+ },
+ testDone: function(failures, total){
+ assert.equal(failures, 0, 'failures');
+ assert.equal(total, 0, 'total');
+ call_order5.push('testDone');
+ throw ignored_error;
+ }
+});
+
+// test6 removed
+
+// using expect
+var call_order7 = [];
+var test7 = function(test){
+ test.expect(2);
+ call_order7.push('test7');
+ test.done();
+};
+nodeunit.runTest(test7, {
+ log: function(assertion){
+ assert.equal(
+ '' + assertion.message,
+ 'Expected 2 assertions, 0 ran'
+ );
+ call_order7.push('log');
+ },
+ testDone: function(failures, total){
+ call_order7.push('testDone');
+ assert.equal(failures, 1, 'failures');
+ assert.equal(total, 1, 'total');
+ }
+});
+
+var call_order8 = [];
+var test8 = function(test){
+ test.expect(1);
+ call_order8.push('test8');
+ test.ok(true, 'test.ok');
+ test.done();
+};
+nodeunit.runTest(test8, {
+ log: function(assertion){
+ call_order8.push('log');
+ },
+ testDone: function(failures, total){
+ call_order8.push('testDone');
+ assert.equal(failures, 0, 'failures');
+ assert.equal(total, 1, 'total');
+ }
+});
+
+
+// test.same - pass
+var call_order9 = [];
+var test9 = function(test){
+ call_order9.push('test9');
+ test.same({test:'test'}, {test:'test'}, 'test.same');
+ test.done();
+};
+nodeunit.runTest(test9, {
+ log: function(assertion){
+ call_order9.push('log');
+ assert.ok(assertion.passed(), 'assertion.passed');
+ assert.equal(assertion.method, 'same', 'assertion.method');
+ },
+ testDone: function(){
+ call_order9.push('testDone');
+ }
+});
+
+
+// test.same - fail
+var call_order10 = [];
+var test10 = function(test){
+ call_order10.push('test10');
+ test.same({test:'test'}, {test2:'test2'}, 'test.same');
+ test.done();
+};
+nodeunit.runTest(test10, {
+ log: function(assertion){
+ call_order10.push('log');
+ assert.ok(assertion.failed(), 'failed');
+ },
+ testDone: function(){
+ call_order10.push('testDone');
+ }
+});
+
+
+// test.equals - pass
+var call_order11 = [];
+var test11 = function(test){
+ call_order11.push('test11');
+ test.equals('test', 'test', 'test.equals');
+ test.done();
+};
+nodeunit.runTest(test11, {
+ log: function(assertion){
+ call_order11.push('log');
+ assert.ok(assertion.passed(), 'passed');
+ },
+ testDone: function(){
+ call_order11.push('testDone');
+ }
+});
+
+
+// test.same - fail
+var call_order12 = [];
+var test12 = function(test){
+ call_order12.push('test12');
+ test.equals('test', 'test2', 'test.equals');
+ test.done();
+};
+nodeunit.runTest(test12, {
+ log: function(assertion){
+ call_order12.push('log');
+ assert.ok(assertion.failed(), 'failed');
+ },
+ testDone: function(){
+ call_order12.push('testDone');
+ }
+});
+
+
+// test assertion object
+var call_order13 = [];
+var test13 = function(test){
+ call_order13.push('test13');
+ test.ok(true, 'ok true');
+ test.done();
+};
+nodeunit.runTest(test13, {
+ log: function(assertion){
+ call_order13.push('log');
+ assert.strictEqual(assertion.passed(), true, 'assertion.passed');
+ assert.strictEqual(assertion.failed(), false, 'assertion.failed');
+ },
+ testDone: function(failures, total){
+ call_order13.push('testDone');
+ assert.equal(failures, 0, 'failures');
+ assert.equal(total, 1, 'total');
+ }
+});
+
+
+// log callback is optional
+var call_order14 = [];
+var test14 = function(test){
+ call_order14.push('test14');
+ test.ok(true, 'ok true');
+ test.done();
+};
+nodeunit.runTest(test14, {
+ testDone: function(failures, total){
+ call_order14.push('testDone');
+ assert.equal(failures, 0, 'failures');
+ assert.equal(total, 1, 'total');
+ }
+});
+
+
+// callbacks are async, so test call order after callbacks have executed
+setTimeout(function(){
+ assert.deepEqual(call_order1, ['test1', 'testDone']);
+ assert.deepEqual(call_order2, ['test2', 'log', 'testDone']);
+ assert.deepEqual(call_order3, ['test3', 'log test.ok', 'testDone']);
+ assert.deepEqual(call_order4, ['test4', 'log', 'testDone']);
+ assert.deepEqual(call_order5, ['test5', 'testDone']);
+ //assert.deepEqual(call_order6, ['test6', 'log', 'testDone']);
+ assert.deepEqual(call_order7, ['test7', 'log', 'testDone']);
+ assert.deepEqual(call_order8, ['test8', 'log', 'testDone']);
+ assert.deepEqual(call_order9, ['test9', 'log', 'testDone']);
+ assert.deepEqual(call_order10, ['test10', 'log', 'testDone']);
+ assert.deepEqual(call_order11, ['test11', 'log', 'testDone']);
+ assert.deepEqual(call_order12, ['test12', 'log', 'testDone']);
+ assert.deepEqual(call_order13, ['test13', 'log', 'testDone']);
+ assert.deepEqual(call_order14, ['test14', 'testDone']);
+ sys.puts('test-runtest OK');
+}, 100);
Please sign in to comment.
Something went wrong with that request. Please try again.