From 2794ceb76f1d7c37fc1657b331a14c0d8b45170e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1r=20=C3=96rlygsson?= Date: Tue, 22 May 2018 16:19:54 +0000 Subject: [PATCH] [ospec] Make the default reporting nicer looking (#2147) * feat(ospec): Add spacing before report results ...sometimes tested code emits console.log() messages which then blend in with ospec's report * feat(ospec): Reword and prettify the report messages * feat(ospec): Make console.errors more compact, yet more readable * docs(ospec): Update change-log.md * fix(ospec): Fix grammar when number of assertions is 1 * feat(ospec): Make "all passed" message bright green * refactor(ospec): define `cStyle()` helper for browser styling * feat(ospec): Use en-dashes for the horizontal divider * feat(ospec): Revert stacktrace coloring, make context bright red ...and add extra newline above each error - for readability in commandline (node.js) mode * feat(ospec): Improve the only-test-passed message "1 assertion passed" --> "The 1 assertion passed" * docs: Update LOC count --- ospec/README.md | 2 +- ospec/change-log.md | 3 +-- ospec/ospec.js | 37 ++++++++++++++++++++++++++++++------- 3 files changed, 32 insertions(+), 10 deletions(-) diff --git a/ospec/README.md b/ospec/README.md index ce42ecf5..38aae7b4 100644 --- a/ospec/README.md +++ b/ospec/README.md @@ -7,7 +7,7 @@ Noiseless testing framework ## About -- ~330 LOC including the CLI runner +- ~360 LOC including the CLI runner - terser and faster test code than with mocha, jasmine or tape - test code reads like bullet points - assertion code follows [SVO](https://en.wikipedia.org/wiki/Subject–verb–object) structure in present tense for terseness and readability diff --git a/ospec/change-log.md b/ospec/change-log.md index f0950dd1..bb7102a6 100644 --- a/ospec/change-log.md +++ b/ospec/change-log.md @@ -2,9 +2,8 @@ ## Upcoming... -_2018-xx-xx_ -- ... +- Improved wording, spacing and color-coding of report messages and errors ([#2147](https://github.com/MithrilJS/mithril.js/pull/2147), [@maranomynet](https://github.com/maranomynet)) ## 2.0.0 diff --git a/ospec/ospec.js b/ospec/ospec.js index f13b1ed0..5674b499 100644 --- a/ospec/ospec.js +++ b/ospec/ospec.js @@ -231,7 +231,7 @@ else window.o = m() function define(name, verb, compare) { Assert.prototype[name] = function assert(value) { if (compare(this.value, value)) record(null) - else record(serialize(this.value) + "\n" + verb + "\n" + serialize(value)) + else record(serialize(this.value) + "\n " + verb + "\n" + serialize(value)) return function(message) { var result = results[results.length - 1] result.message = message + "\n\n" + result.message @@ -258,8 +258,17 @@ else window.o = m() else if (typeof value === "function") return value.name || "" try {return JSON.stringify(value)} catch (e) {return String(value)} } - function highlight(message) { - return hasProcess ? (process.stdout.isTTY ? "\x1b[31m" + message + "\x1b[0m" : message) : "%c" + message + "%c " + var colorCodes = { + red: "31m", + red2: "31;1m", + green: "32;1m" + } + function highlight(message, color) { + var code = colorCodes[color] || colorCodes.red; + return hasProcess ? (process.stdout.isTTY ? "\x1b[" + code + message + "\x1b[0m" : message) : "%c" + message + "%c " + } + function cStyle(color, bold) { + return hasProcess||!color ? "" : "color:"+color+(bold ? ";font-weight:bold" : "") } o.report = function (results) { @@ -267,14 +276,28 @@ else window.o = m() for (var i = 0, r; r = results[i]; i++) { if (!r.pass) { var stackTrace = o.cleanStackTrace(r.error) - console.error(r.context + ":\n" + highlight(r.message) + (stackTrace ? "\n\n" + stackTrace + "\n\n" : ""), hasProcess ? "" : "color:red", hasProcess ? "" : "color:black") + console.error( + (hasProcess ? "\n" : "") + + highlight(r.context + ":", "red2") + "\n" + + highlight(r.message, "red") + + (stackTrace ? "\n" + stackTrace + "\n" : ""), + + cStyle("black", true), "", // reset to default + cStyle("red"), cStyle("black") + ) errCount++ } } + var pl = results.length === 1 ? "" : "s" + var resultSummary = (errCount === 0) ? + highlight((pl ? "All " : "The ") + results.length + " assertion" + pl + " passed", "green"): + highlight(errCount + " out of " + results.length + " assertion" + pl + " failed", "red2") + var runningTime = " in " + Math.round(Date.now() - start) + "ms" + console.log( - (name ? name + ": " : "") + - results.length + " assertions completed in " + Math.round(new Date - start) + "ms, " + - "of which " + results.filter(function(result){return result.error}).length + " failed" + (hasProcess ? "––––––\n" : "") + + (name ? name + ": " : "") + resultSummary + runningTime, + cStyle((errCount === 0 ? "green" : "red"), true), "" ) return errCount }