|
| 1 | +module.exports = test |
| 2 | + |
| 3 | +var tests = module.exports.tests = []; |
| 4 | +var ran = false |
| 5 | +var id = 0 |
| 6 | +var fail = 0 |
| 7 | +var pass = 0 |
| 8 | + |
| 9 | +function test(name, fn) { |
| 10 | + tests.push([name, fn]); |
| 11 | + if (ran) return; |
| 12 | + ran = true |
| 13 | + process.nextTick(run) |
| 14 | +} |
| 15 | + |
| 16 | +var assert = require('assert'); |
| 17 | +Error.captureStackTrace = function(){}; |
| 18 | + |
| 19 | +test.count = function (n) { |
| 20 | + console.log('1..' + Number(n)); |
| 21 | +} |
| 22 | + |
| 23 | +var t = Object.keys(assert).map(function (k) { |
| 24 | + if (typeof assert[k] !== 'function') return; |
| 25 | + return [k, function () { |
| 26 | + var s = null |
| 27 | + id++ |
| 28 | + try { |
| 29 | + assert[k].apply(assert, arguments) |
| 30 | + pass ++ |
| 31 | + console.log('ok', id, k) |
| 32 | + } catch (e) { |
| 33 | + |
| 34 | + fail ++ |
| 35 | + // ignore everything up to the run() function |
| 36 | + // Comment out until captureStackTrace is implemented |
| 37 | + // Error.captureStackTrace(e, t[k]) |
| 38 | + s = e.stack |
| 39 | + if (s) { |
| 40 | + s = s.trim().split(/\n/) |
| 41 | + // bottom two frames are nextTick and this file |
| 42 | + s.pop() |
| 43 | + s.pop() |
| 44 | + } |
| 45 | + |
| 46 | + if (s && !e.message) |
| 47 | + e.message = s[0] |
| 48 | + console.log('') |
| 49 | + console.log('not ok', id, s ? s.shift() : e.message) |
| 50 | + if (s && s.length) { |
| 51 | + s = s.map(function(s) { |
| 52 | + return s.trim() + '\n' |
| 53 | + }) |
| 54 | + console.log('#' + s.join('# ')) |
| 55 | + } |
| 56 | + console.log('') |
| 57 | + } |
| 58 | + }] |
| 59 | +}).reduce(function (set, kv) { |
| 60 | + set[kv[0]] = kv[1] |
| 61 | + return set |
| 62 | +}, {}) |
| 63 | + |
| 64 | +t.pass = function (m) { |
| 65 | + assert(true, m) |
| 66 | +} |
| 67 | + |
| 68 | +t.fail = function (m) { |
| 69 | + assert(false, m) |
| 70 | +} |
| 71 | + |
| 72 | +t.comment = function (m) { |
| 73 | + console.log('#', m.replace(/^#\s*/, ''), "\n") |
| 74 | +} |
| 75 | + |
| 76 | +t.end = run |
| 77 | + |
| 78 | +var children = [] |
| 79 | +t.test = function(name, fn) { |
| 80 | + children.push([name, fn]) |
| 81 | +} |
| 82 | + |
| 83 | +function run () { |
| 84 | + if (children.length) { |
| 85 | + tests.unshift.apply(tests, children) |
| 86 | + children.length = 0 |
| 87 | + } |
| 88 | + |
| 89 | + var next = tests.shift(); |
| 90 | + if (!next) { |
| 91 | + console.log('# pass', pass, pass + fail) |
| 92 | + console.log('# fail', fail, pass + fail) |
| 93 | + process.exit(fail) |
| 94 | + return |
| 95 | + } |
| 96 | + |
| 97 | + var name = next[0]; |
| 98 | + var fn = next[1]; |
| 99 | + console.log('#', name); |
| 100 | + process.nextTick(function() { |
| 101 | + fn(t); |
| 102 | + }) |
| 103 | +} |
0 commit comments