|
| 1 | +var assert = require('assert'); |
| 2 | + |
| 3 | + |
| 4 | +var Assertion = function(method, message, error){ |
| 5 | + return { |
| 6 | + method: method, |
| 7 | + message: message || '', |
| 8 | + error: error, |
| 9 | + passed: function(){return !error;}, |
| 10 | + failed: function(){return Boolean(error);} |
| 11 | + }; |
| 12 | +}; |
| 13 | + |
| 14 | +var TestEnv = function(options){ |
| 15 | + var expecting; |
| 16 | + var assertions = []; |
| 17 | + options.log = options.log || function(){}; // log callback optional |
| 18 | + |
| 19 | + var wrapAssert = function(new_method, assert_method){ |
| 20 | + return function(){ |
| 21 | + try { |
| 22 | + assert[assert_method].apply(global, arguments); |
| 23 | + var message = arguments[arguments.length-1]; |
| 24 | + var assertion = new Assertion(new_method, message); |
| 25 | + } |
| 26 | + catch (e){ |
| 27 | + var assertion = new Assertion('', e.message || '', e); |
| 28 | + } |
| 29 | + assertions.push(assertion); |
| 30 | + process.nextTick(function(){options.log(assertion)}); |
| 31 | + }; |
| 32 | + }; |
| 33 | + |
| 34 | + return { |
| 35 | + done: function(){ |
| 36 | + if(expecting !== undefined && expecting != assertions.length){ |
| 37 | + var err = new Error( |
| 38 | + 'Expected ' + expecting + ' assertions, ' + |
| 39 | + assertions.length + ' ran' |
| 40 | + ); |
| 41 | + var assertion = new Assertion('expect', err.message, err); |
| 42 | + assertions.push(assertion); |
| 43 | + process.nextTick(function(){options.log(assertion);}); |
| 44 | + } |
| 45 | + var failures = assertions.reduce(function(a,x){ |
| 46 | + return x.failed() ? a+1 : a; |
| 47 | + }, 0); |
| 48 | + process.nextTick(function(){ |
| 49 | + options.testDone(failures, assertions.length); |
| 50 | + }); |
| 51 | + }, |
| 52 | + ok: wrapAssert('ok', 'ok'), |
| 53 | + same: wrapAssert('same', 'deepEqual'), |
| 54 | + equals: wrapAssert('equals', 'equal'), |
| 55 | + expect: function(num){ |
| 56 | + expecting = num; |
| 57 | + } |
| 58 | + }; |
| 59 | +}; |
| 60 | + |
| 61 | + |
| 62 | +exports.runTest = function(fn, options){ |
| 63 | + options.log = options.log || function(){}; // log callback optional |
| 64 | + try { |
| 65 | + fn(new TestEnv(options)); |
| 66 | + } |
| 67 | + catch (e){ |
| 68 | + var assertion = new Assertion('', e.message || '', e); |
| 69 | + process.nextTick(function(){options.log(assertion)}); |
| 70 | + process.nextTick(function(){options.testDone(1, 1)}); |
| 71 | + } |
| 72 | +}; |
| 73 | + |
| 74 | + |
| 75 | +exports.runModule = function(mod, options){ |
| 76 | + var m_failures = 0; |
| 77 | + var m_total = 0; |
| 78 | + var i = 0; |
| 79 | + var tests = Object.keys(mod).map(function(k){return mod[k];}); |
| 80 | + var _fn = function(test){ |
| 81 | + (options.testStart || function(){})(test); |
| 82 | + exports.runTest(test, { |
| 83 | + log: options.log, |
| 84 | + testDone: function(failures, total){ |
| 85 | + |
| 86 | + m_failures += failures; |
| 87 | + m_total += total; |
| 88 | + (options.testDone || function(){})(failures, total); |
| 89 | + |
| 90 | + i++; |
| 91 | + if(i < tests.length){ |
| 92 | + _fn(tests[i]); |
| 93 | + } |
| 94 | + else { |
| 95 | + (options.moduleDone || function(){})(m_failures, m_total); |
| 96 | + } |
| 97 | + } |
| 98 | + }); |
| 99 | + }; |
| 100 | + _fn(tests[0]); |
| 101 | +}; |
0 commit comments