From 1a51259a7615fe8ddf42ae3abdcfa5b706d0422e Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Fri, 23 Feb 2018 14:15:09 -0500 Subject: [PATCH] HtmlConsoleOutput: Add HTML-styled console output --- progtest/src/TestRunner.js | 4 +- progtest/src/env.js | 13 +- progtest/src/reporter/ConsoleTestReporter.js | 142 ++++++++++++-- progtest/src/reporter/HtmlConsoleOutput.js | 191 +++++++++++++++++++ progtest/test/TestRunnerTest.js | 2 + 5 files changed, 328 insertions(+), 24 deletions(-) create mode 100644 progtest/src/reporter/HtmlConsoleOutput.js diff --git a/progtest/src/TestRunner.js b/progtest/src/TestRunner.js index 504c1a16..b7685dc3 100644 --- a/progtest/src/TestRunner.js +++ b/progtest/src/TestRunner.js @@ -77,7 +77,7 @@ module.exports = Class( 'TestRunner', this._reporter.preRun( total ); return this._runAsync( dfns ).then( - results => this._reporter.done( results ) + results => ( this._reporter.done( results ), results ) ); }, @@ -157,6 +157,8 @@ module.exports = Class( 'TestRunner', i: test_i, total: cmp.length, failures: failures, + given: data, + expect: expect, }; this._reporter.testCaseResult( result_data, total ); diff --git a/progtest/src/env.js b/progtest/src/env.js index 7e858541..7072791c 100644 --- a/progtest/src/env.js +++ b/progtest/src/env.js @@ -34,16 +34,17 @@ const { }, reporter: { - ConsoleTestReporter + ConsoleTestReporter, + HtmlConsoleOutput, }, } = require( '../src' ); module.exports = { - console: ( program, stdout ) => + console: ( program, stdout, reporter ) => { const runner = TestRunner( - ConsoleTestReporter( stdout ), + ( reporter || ConsoleTestReporter( stdout ) ), program ); @@ -70,4 +71,10 @@ module.exports = { } } ); }, + + browser: ( program, stdout ) => module.exports.console( + program, + stdout, + ConsoleTestReporter.use( HtmlConsoleOutput )( stdout ) + ), }; diff --git a/progtest/src/reporter/ConsoleTestReporter.js b/progtest/src/reporter/ConsoleTestReporter.js index 2a16e038..45302bd3 100644 --- a/progtest/src/reporter/ConsoleTestReporter.js +++ b/progtest/src/reporter/ConsoleTestReporter.js @@ -30,6 +30,10 @@ const { Class } = require( 'easejs' ); * Test cases will be output in a block of dots (success) or 'F's (failure), * in a style similar to PHPUnit. If failures occur, they will be output to * in more detail after all tests have run. + * + * This class contains various virtual methods for overriding portions of + * the output. It is a bit of a mess, produced in a rush. If you find that + * it requires more extension in the future, it may be worth reconsidering. */ module.exports = Class( 'ConsoleTestReporter', { @@ -76,22 +80,67 @@ module.exports = Class( 'ConsoleTestReporter', * For the format of RESULT, see TestRunner. * * @param {Object} result test case result + * @param {number} total total number of test cases * * @return {undefined} */ 'public testCaseResult'( result, total ) { - const { i, failures } = result; + this._stdout.write( this.createTestCaseResult( result, total ) ); + }, - const ind = ( failures.length === 0 ) + + /** + * Produce string for test case result + * + * The result is the concatenation of result and progress indicators. + * + * @param {Object} result test case result + * @param {number} total total number of test cases + * + * @return {string} output string + */ + 'virtual protected createTestCaseResult'( result, total ) + { + return this.getInd( result, total ) + + this.createResultProgress( result, total ); + }, + + + /** + * Produce test case result indicator + * + * The indicator is a single `.` on success, and `F` on failure. + * + * @param {Object} result test case result + * @param {number} total total number of test cases + * + * @return {string} output string + */ + 'virtual protected getInd'( { i, failures }, total ) + { + return ( failures.length === 0 ) ? '.' : 'F'; + }, - const sep = ( i % 50 === 49 ) + + /** + * Produce progress indicator for test case result + * + * Progress will be reported numerically every 50 test cases, followed + * by a newline. + * + * @param {Object} result test case result + * @param {number} total total number of test cases + * + * @return {string} progress string + */ + 'virtual protected createResultProgress'( { i }, total ) + { + return ( i % 50 === 49 ) ? ` ${i+1}/${total}\n` : ''; - - this._stdout.write( ind + sep ); }, @@ -126,12 +175,24 @@ module.exports = Class( 'ConsoleTestReporter', */ 'private _outputFailureReport'( results ) { - const report = results + const parts = results .filter( ( { failures } ) => failures.length > 0 ) - .map( this._reportTestFailure.bind( this ) ) - .join( '\n' ) + .map( this._reportTestFailure.bind( this ) ); - this._stdout.write( "\n\n" + report ); + this._stdout.write( this.combineFailureResults( parts ) ); + }, + + + /** + * Compile failure result strings into a single report + * + * @param {Array} results failures + * + * @return {string} combined report + */ + 'virtual protected combineFailureResults'( results ) + { + return "\n\n" + results.join( "\n" ); }, @@ -144,12 +205,38 @@ module.exports = Class( 'ConsoleTestReporter', */ 'private _reportTestFailure'( { i, desc, failures } ) { - return `[#${i+1}] ${desc}\n` + - failures.map( ( { field, expect, result } ) => - ` ${field}:\n` + - ` expected: ` + JSON.stringify( expect ) + `\n` + - ` result: ` + JSON.stringify( result ) + `\n` - ).join( '' ); + return this.createFailureHeading( i, desc ) + + failures.map( this.createFailureDiff.bind( this ) ) + .join( '' ); + }, + + + /** + * Create heading for individual test case failure + * + * @param {number} i test case index + * @param {string} desc test case description + * + * @return {string} heading + */ + 'virtual protected createFailureHeading'( i, desc ) + { + return `[#${i+1}] ${desc}\n`; + }, + + + /** + * Create diff output for failed assertions + * + * @param {Object} result failure data + * + * @return {string} diff + */ + 'virtual protected createFailureDiff'( { field, expect, result } ) + { + return ` ${field}:\n` + + ` expected: ` + JSON.stringify( expect ) + `\n` + + ` result: ` + JSON.stringify( result ) + `\n`; }, @@ -173,11 +260,26 @@ module.exports = Class( 'ConsoleTestReporter', [ 0, 0, 0 ] ); - const test_total = results.length; + this._stdout.write( this.createSummaryLine( + results.length, failed, acount, afailed + ) ); + }, - this._stdout.write( - `\n${test_total} tests, ${failed} failed (` + - `${acount} assertions, ${afailed} failures)` - ); + + /** + * Output a line, preceded by an empty line, summarizing the number of + * tests, assertions, and failures for each + * + * @param {number} test_total total number of test cases run + * @param {number} failed number of failed test cases + * @param {number} acount total number of assertions + * @param {number} failed number of assertion failures + * + * @return {string} summary line + */ + 'virtual protected createSummaryLine'( test_total, failed, acount, afailed ) + { + return `\n${test_total} tests, ${failed} failed (` + + `${acount} assertions, ${afailed} failures)`; }, } ); diff --git a/progtest/src/reporter/HtmlConsoleOutput.js b/progtest/src/reporter/HtmlConsoleOutput.js new file mode 100644 index 00000000..47a66cfe --- /dev/null +++ b/progtest/src/reporter/HtmlConsoleOutput.js @@ -0,0 +1,191 @@ +/** + * Hyperlinks for console test reporter + * + * Copyright (C) 2018 R-T Specialty, LLC. + * + * This file is part of TAME. + * + * TAME is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +"use strict"; + +const { Trait } = require( 'easejs' ); +const ConsoleTestReporter = require( './ConsoleTestReporter' ); + + +/** + * Format console output as interactive HTML. + * + * Spacing and line breaks will be preserved. Test cases will be made + * clickable (both the indicators and failure output); it is up to the + * caller to hook the links to do something useful. + */ +module.exports = Trait( 'HtmlConsoleOutput' ) + .extend( ConsoleTestReporter, +{ + /** + * Produce string for test case result + * + * The result is the concatenation of result and progress indicators. + * + * @param {Object} result test case result + * @param {number} total total number of test cases + * + * @return {string} output string + */ + 'override virtual public createTestCaseResult'( result, total ) + { + return this.__super( result, total ).replace( /\n/g, "
" ); + }, + + + /** + * Produce test case result indicator + * + * The indicator is a single `.` on success, and `F` on failure. + * + * @param {Object} result test case result + * @param {number} total total number of test cases + * + * @return {string} output string + */ + 'override protected getInd'( result, total ) + { + const { i, failures, desc } = result; + + const ind = this.__super( result ); + const title = `[#${i}] ` + this._titleify( desc ); + + return `${ind}`; + }, + + + /** + * Format string for use as a truncated anchor title + * + * The title will be truncated to 60 characters (including the ellipsis, + * if one is added), and will escape double quotes and closing angled + * brackets. + * + * @param {string} str source string + * + * @return {string} formatted string + */ + 'private _titleify'( str ) + { + const newstr = ( str.length < 60 ) + ? str + : str.substr( 0, 57 ) + '...'; + + return newstr + .replace( />/g, ">" ) + .replace( /"/g, "&dquo;" ); + }, + + + /** + * Produce progress indicator for test case result + * + * Progress will be reported numerically every 50 test cases, followed + * by a newline. + * + * @param {Object} result test case result + * @param {number} total total number of test cases + * + * @return {string} progress string + */ + 'override protected createResultProgress'( results, total ) + { + return this.__super( results, total ).replace( / /g, ' ' ); + }, + + + /** + * Create diff output for failed assertions + * + * @param {Object} result failure data + * + * @return {string} diff + */ + 'override protected createFailureDiff'( failure ) + { + return this._htmlizeText( this.__super( failure ) ); + }, + + + /** + * Create heading for individual test case failure + * + * @param {number} i test case index + * @param {string} desc test case description + * + * @return {string} heading + */ + 'override protected createFailureHeading'( i, desc ) + { + return `` + + `[#${i+1}] ${desc}
`; + }, + + + /** + * Compile failure result strings into a single report + * + * @param {Array} results failures + * + * @return {string} combined report + */ + 'override protected combineFailureResults'( results ) + { + return "

" + results.join( "
" ); + }, + + + /** + * Output a line, preceded by an empty line, summarizing the number of + * tests, assertions, and failures for each + * + * @param {number} test_total total number of test cases run + * @param {number} failed number of failed test cases + * @param {number} acount total number of assertions + * @param {number} failed number of assertion failures + * + * @return {string} summary line + */ + 'override protected createSummaryLine'( test_total, failed, acount, afailed ) + { + return this._htmlizeText( + this.__super( test_total, failed, acount, afailed ) + ); + }, + + + /** + * Format string for output in HTML while maintaining appearance + * + * Spaces are converted into non-breaking spaces (so as not to be + * normalized) and line breaks are converted into `br` tags. + * + * @param {string} source string + * + * @return {string} formatted string + */ + 'private _htmlizeText'( str ) + { + return str + .replace( / /g, " " ) + .replace( /\n/g, '
' ); + }, +} ); diff --git a/progtest/test/TestRunnerTest.js b/progtest/test/TestRunnerTest.js index eff50eb9..5a09cfe5 100644 --- a/progtest/test/TestRunnerTest.js +++ b/progtest/test/TestRunnerTest.js @@ -102,6 +102,8 @@ describe( "TestRunner", () => expect( result.failures ).to.deep.equal( expect_failures[ i ] ); + expect( result.given ).to.equal( test_cases[ i ].data ); + expect( result.expect ).to.equal( test_cases[ i ].expect ); } ); } ); } );