| 1 |
"use strict"; |
| 2 |
|
| 3 |
var call = Function.prototype.call |
| 4 |
, partial = require("es5-ext/function/#/partial") |
| 5 |
, last = require("es5-ext/array/#/last") |
| 6 |
, indent = call.bind(partial.call(require("es5-ext/string/#/indent"), " ", 17)) |
| 7 |
, format = call.bind(partial.call(require("es5-ext/date/#/format"), "%H:%M:%S.%L ")) |
| 8 |
, dpad = partial.call(require("es5-ext/string/#/pad"), " ", 12) |
| 9 |
, duration = require("duration") |
| 10 |
, inspect = require("util").inspect |
| 11 |
, clc = require("cli-color") |
| 12 |
, ctrim = require("cli-color/strip") |
| 13 |
, cthrobber = require("cli-color/throbber"); |
| 14 |
|
| 15 |
var write = process.stdout.write.bind(process.stdout) |
| 16 |
, lerror = clc.magenta |
| 17 |
, lfail = clc.red |
| 18 |
, lpass = clc.green |
| 19 |
, lsummary = clc.cyan |
| 20 |
, handler; |
| 21 |
|
| 22 |
handler = { |
| 23 |
init: function (options) { |
| 24 |
if (options.a) this.mode = 2; |
| 25 |
else this.mode = options.m ? 0 : 1; |
| 26 |
this.passed = 0; |
| 27 |
this.failed = 0; |
| 28 |
this.errored = 0; |
| 29 |
this.started = new Date(); |
| 30 |
this.writeLog = []; |
| 31 |
this.write = function (ignored) { |
| 32 |
this.writeLog.push(arguments); |
| 33 |
return write.apply(this, arguments); |
| 34 |
}; |
| 35 |
this.progress = cthrobber(write, 200); |
| 36 |
return this; |
| 37 |
}, |
| 38 |
atNewLine: function () { |
| 39 |
return !this.writeLog.length || last.call(ctrim(last.call(this.writeLog)[0])) === "\n"; |
| 40 |
}, |
| 41 |
break: function () { |
| 42 |
this.progress.restart(); |
| 43 |
if (!this.atNewLine() && this.mode) { |
| 44 |
this.write("\n"); |
| 45 |
} |
| 46 |
}, |
| 47 |
pass: function (path, name) { |
| 48 |
++this.passed; |
| 49 |
this.progress.restart(); |
| 50 |
if (this.mode === 2) { |
| 51 |
this.write(lpass(format(new Date()) + " ✓ " + name + "\n")); |
| 52 |
} else { |
| 53 |
this.write( |
| 54 |
lpass( |
| 55 |
this.atNewLine() |
| 56 |
? format(new Date()) + " ✓ " + (path && this.mode ? path + " " : "") + "." |
| 57 |
: "." |
| 58 |
) |
| 59 |
); |
| 60 |
} |
| 61 |
}, |
| 62 |
fail: function (path, name, e) { |
| 63 |
var message; |
| 64 |
++this.failed; |
| 65 |
this.progress.restart(); |
| 66 |
if (!this.atNewLine()) { |
| 67 |
this.write("\n"); |
| 68 |
} |
| 69 |
message = ""; |
| 70 |
if (e.operator) { |
| 71 |
if (hasOwnProperty.call(e, "expected")) { |
| 72 |
message += "Expected: " + inspect(e.expected, false, 1) + "\n"; |
| 73 |
} |
| 74 |
if (hasOwnProperty.call(e, "actual")) { |
| 75 |
message += "Actual: " + inspect(e.actual, false, 1) + "\n"; |
| 76 |
} |
| 77 |
message += "Operator: " + e.operator + "\n"; |
| 78 |
} else { |
| 79 |
message += (e.stack || e) + "\n"; |
| 80 |
} |
| 81 |
this.write(lfail(format(new Date()) + " ✗ " + name + "\n" + indent(message))); |
| 82 |
}, |
| 83 |
error: function (path, name, e) { |
| 84 |
var message, eStr, index; |
| 85 |
++this.errored; |
| 86 |
this.progress.restart(); |
| 87 |
if (!this.atNewLine()) { |
| 88 |
this.write("\n"); |
| 89 |
} |
| 90 |
name = name || path; |
| 91 |
message = format(new Date()) + " - "; |
| 92 |
eStr = String(e.stack || e); |
| 93 |
if (name) { |
| 94 |
message += name + "\n" + indent(eStr); |
| 95 |
} else { |
| 96 |
index = eStr.indexOf("\n") + 1; |
| 97 |
message += index ? eStr.slice(0, index) + indent(eStr.slice(index)) : eStr; |
| 98 |
} |
| 99 |
this.write(lerror(message + "\n")); |
| 100 |
}, |
| 101 |
end: function () { |
| 102 |
var message, all = this.passed + this.failed + this.errored; |
| 103 |
this.progress.stop(); |
| 104 |
if (!this.atNewLine()) { |
| 105 |
this.write("\n"); |
| 106 |
} |
| 107 |
this.write("\n"); |
| 108 |
if (all) { |
| 109 |
this.write( |
| 110 |
lsummary(dpad.call(duration(this.started, new Date()).toString()) + " ") |
| 111 |
); |
| 112 |
message = []; |
| 113 |
message.push( |
| 114 |
clc.green(this.passed + " Ok [" + ((this.passed / all) * 100).toFixed(2) + "%]") |
| 115 |
); |
| 116 |
if (this.failed) { |
| 117 |
message.push(clc.red(this.failed + " Failed")); |
| 118 |
} |
| 119 |
if (this.errored) { |
| 120 |
message.push(clc.magenta(this.errored + " Errors")); |
| 121 |
} |
| 122 |
this.write(message.join(" ") + "\n\n"); |
| 123 |
} else { |
| 124 |
this.write(lsummary("No tests run\n\n")); |
| 125 |
} |
| 126 |
} |
| 127 |
}; |
| 128 |
|
| 129 |
module.exports = function (options) { return Object.create(handler).init(options || {}); }; |