| 1 |
/* eslint max-lines: "off" */ |
| 2 |
|
| 3 |
// Runs tests |
| 4 |
|
| 5 |
"use strict"; |
| 6 |
|
| 7 |
var isError = require("es5-ext/error/is-error") |
| 8 |
, isFunction = require("es5-ext/function/is-function") |
| 9 |
, noop = require("es5-ext/function/noop") |
| 10 |
, toArray = require("es5-ext/object/to-array") |
| 11 |
, hforEach = require("es5-ext/object/for-each") |
| 12 |
, isValue = require("es5-ext/object/is-value") |
| 13 |
, isThenable = require("es5-ext/object/is-thenable") |
| 14 |
, deferred = require("deferred") |
| 15 |
, createLogger = require("./logger") |
| 16 |
, createAssert = require("./assert"); |
| 17 |
|
| 18 |
var nextTick = process.nextTick |
| 19 |
, pattern = /^\s*(?:async\s*)?(?:[^\s()]+\s*)?\(\s*([tad])(?:\s*,\s*([tad]))?\s*\)/ |
| 20 |
, run; |
| 21 |
|
| 22 |
run = function self(testee, tests, assert, logger) { |
| 23 |
if (isFunction(tests)) { |
| 24 |
tests = { "": tests }; |
| 25 |
} |
| 26 |
|
| 27 |
hforEach(tests, function (t) { |
| 28 |
var conf, match; |
| 29 |
if (isFunction(t)) { |
| 30 |
conf = t.conf = { t: true, a: true, d: false }; |
| 31 |
if (t.length > 2) { |
| 32 |
conf.d = true; |
| 33 |
} else if ((match = t.toString().match(pattern))) { |
| 34 |
conf.t = conf.a = false; |
| 35 |
conf[match[1]] = true; |
| 36 |
if (match[2]) { |
| 37 |
conf[match[2]] = true; |
| 38 |
} |
| 39 |
} |
| 40 |
} |
| 41 |
}); |
| 42 |
|
| 43 |
return deferred.reduce( |
| 44 |
toArray(tests), |
| 45 |
// eslint-disable-next-line max-statements |
| 46 |
function (ignore, data) { |
| 47 |
var testResult, d, finish, done, name, testFunction; |
| 48 |
name = data[0]; |
| 49 |
testFunction = data[1]; |
| 50 |
d = deferred(); |
| 51 |
finish = function () { |
| 52 |
logger.out(true); |
| 53 |
d.resolve(); |
| 54 |
}; |
| 55 |
logger.in(name, true); |
| 56 |
if (isFunction(testFunction)) { |
| 57 |
try { |
| 58 |
if (testFunction.conf.d) { |
| 59 |
done = function (asyncTestResult) { |
| 60 |
if (asyncTestResult) { |
| 61 |
if (isError(asyncTestResult)) { |
| 62 |
assert.fail(asyncTestResult); |
| 63 |
finish(); |
| 64 |
} else { |
| 65 |
self(testee, asyncTestResult, assert, logger)(finish).done(); |
| 66 |
} |
| 67 |
} else { |
| 68 |
finish(); |
| 69 |
} |
| 70 |
}; |
| 71 |
if (testFunction.conf.t) { |
| 72 |
// eslint-disable-next-line max-depth |
| 73 |
if (testFunction.conf.a) { |
| 74 |
testFunction(testee, assert, done); |
| 75 |
} else { |
| 76 |
testFunction(testee, done); |
| 77 |
} |
| 78 |
} else if (testFunction.conf.a) { |
| 79 |
testFunction(assert, done); |
| 80 |
} else { |
| 81 |
testFunction(done); |
| 82 |
} |
| 83 |
} else { |
| 84 |
if (testFunction.conf.t) { |
| 85 |
testResult = testFunction(testee, assert); |
| 86 |
} else { |
| 87 |
testResult = testFunction(assert); |
| 88 |
} |
| 89 |
if (isThenable(testResult)) { |
| 90 |
testResult.then( |
| 91 |
function (thenableTestResult) { |
| 92 |
if (thenableTestResult) { |
| 93 |
self( |
| 94 |
testee, thenableTestResult, assert, logger |
| 95 |
)(finish).done(); |
| 96 |
} else { |
| 97 |
finish(); |
| 98 |
} |
| 99 |
}, |
| 100 |
function (error) { |
| 101 |
assert.fail(error); |
| 102 |
finish(); |
| 103 |
} |
| 104 |
); |
| 105 |
} else if (testResult) { |
| 106 |
self(testee, testResult, assert, logger)(finish).done(); |
| 107 |
} else { |
| 108 |
finish(); |
| 109 |
} |
| 110 |
} |
| 111 |
} catch (e) { |
| 112 |
logger.error(e); |
| 113 |
finish(); |
| 114 |
} |
| 115 |
} else if (isValue(testFunction)) { |
| 116 |
self(testee, testFunction, assert, logger)(finish).done(); |
| 117 |
} else { |
| 118 |
assert.fail(new Error("No tests found at '" + name + "' property")); |
| 119 |
finish(); |
| 120 |
} |
| 121 |
return d.promise; |
| 122 |
}, |
| 123 |
null |
| 124 |
)(noop); |
| 125 |
}; |
| 126 |
|
| 127 |
module.exports = function (testee, test, assert, logger) { |
| 128 |
var runResult; |
| 129 |
logger = logger || createLogger(); |
| 130 |
assert = assert || createAssert(logger); |
| 131 |
nextTick(function () { |
| 132 |
runResult = run(testee, test, assert, logger); |
| 133 |
if (logger.end) { |
| 134 |
runResult = runResult(logger.end.bind(logger)); |
| 135 |
} |
| 136 |
runResult.done(); |
| 137 |
}); |
| 138 |
return logger; |
| 139 |
}; |