diff --git a/package.json b/package.json index afb32be..7089839 100644 --- a/package.json +++ b/package.json @@ -1,17 +1,17 @@ { "name": "tinytap", - "version": "0.1.0", - "description": "Tiny tap parser and tools.", - "main": "index.js", + "version": "0.2.0", + "description": "Tiny TAP tester and test runner.", + "main": "./src/tester.js", "bin": { - "tinytap": "./tinytap.js" + "tinytap": "./src/runner.js" }, "dependencies": { "glob": "^4.0.0", "shell-quote": "^1.4.1" }, "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "test": "cd test; ./test.sh" }, "author": "", "license": "MIT", diff --git a/index.js b/src/parser.js similarity index 98% rename from index.js rename to src/parser.js index 2471aab..9b25027 100644 --- a/index.js +++ b/src/parser.js @@ -2,8 +2,6 @@ var fs = require('fs'); var Duplex = require('stream').Duplex; var util = require('util'); -// console.log('tap tiny'); - function state (machine) { var key = 'start'; return function caller () { diff --git a/tinytap.js b/src/runner.js similarity index 98% rename from tinytap.js rename to src/runner.js index 79aec18..45a1196 100755 --- a/tinytap.js +++ b/src/runner.js @@ -4,9 +4,10 @@ var spawn = require('child_process').spawn; var parse = require('shell-quote').parse; var quote = require('shell-quote').quote; var stream = require('stream'); -var tinytap = require('./'); var glob = require('glob'); +var tinytap = require('../src/parser'); + var args = process.argv.slice(2); var list = [[null]]; diff --git a/src/tester.js b/src/tester.js new file mode 100644 index 0000000..a91546d --- /dev/null +++ b/src/tester.js @@ -0,0 +1,103 @@ +module.exports = test + +var tests = module.exports.tests = []; +var ran = false +var id = 0 +var fail = 0 +var pass = 0 + +function test(name, fn) { + tests.push([name, fn]); + if (ran) return; + ran = true + process.nextTick(run) +} + +var assert = require('assert'); +Error.captureStackTrace = function(){}; + +test.count = function (n) { + console.log('1..' + Number(n)); +} + +var t = Object.keys(assert).map(function (k) { + if (typeof assert[k] !== 'function') return; + return [k, function () { + var s = null + id++ + try { + assert[k].apply(assert, arguments) + pass ++ + console.log('ok', id, k) + } catch (e) { + + fail ++ + // ignore everything up to the run() function + // Comment out until captureStackTrace is implemented + // Error.captureStackTrace(e, t[k]) + s = e.stack + if (s) { + s = s.trim().split(/\n/) + // bottom two frames are nextTick and this file + s.pop() + s.pop() + } + + if (s && !e.message) + e.message = s[0] + console.log('') + console.log('not ok', id, s ? s.shift() : e.message) + if (s && s.length) { + s = s.map(function(s) { + return s.trim() + '\n' + }) + console.log('#' + s.join('# ')) + } + console.log('') + } + }] +}).reduce(function (set, kv) { + set[kv[0]] = kv[1] + return set +}, {}) + +t.pass = function (m) { + assert(true, m) +} + +t.fail = function (m) { + assert(false, m) +} + +t.comment = function (m) { + console.log('#', m.replace(/^#\s*/, ''), "\n") +} + +t.end = run + +var children = [] +t.test = function(name, fn) { + children.push([name, fn]) +} + +function run () { + if (children.length) { + tests.unshift.apply(tests, children) + children.length = 0 + } + + var next = tests.shift(); + if (!next) { + console.log('# pass', pass, pass + fail) + console.log('# fail', fail, pass + fail) + process.exit(fail) + return + } + + var name = next[0]; + var fn = next[1]; + console.log('#', name); + process.nextTick(function() { + fn(t); + }) +} diff --git a/test.sh b/test.sh deleted file mode 100755 index dceeebb..0000000 --- a/test.sh +++ /dev/null @@ -1,5 +0,0 @@ -cat test/colony.tap | node index.js - > out.tap -cat out.tap | node index.js - > out2.tap -diff out.tap out2.tap -rm out.tap -rm out2.tap diff --git a/test/test.sh b/test/test.sh new file mode 100755 index 0000000..68fd0b5 --- /dev/null +++ b/test/test.sh @@ -0,0 +1,5 @@ +cat colony.tap | node ../src/runner.js - > out.tap +cat out.tap | node ../src/runner.js - > out2.tap +diff out.tap out2.tap +rm out.tap +rm out2.tap