HtmlConsoleOutput: Add HTML-styled console output
parent
5aa29216f4
commit
1a51259a76
|
@ -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 );
|
||||
|
|
|
@ -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 )
|
||||
),
|
||||
};
|
||||
|
|
|
@ -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<string>} 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)`;
|
||||
},
|
||||
} );
|
||||
|
|
|
@ -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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
"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, "<br />" );
|
||||
},
|
||||
|
||||
|
||||
/**
|
||||
* 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 `<a href="#" data-case-index="${i}" title="${title}">${ind}</a>`;
|
||||
},
|
||||
|
||||
|
||||
/**
|
||||
* 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 `<a href="#" data-case-index="${i}">` +
|
||||
`[#${i+1}]</a> ${desc}<br />`;
|
||||
},
|
||||
|
||||
|
||||
/**
|
||||
* Compile failure result strings into a single report
|
||||
*
|
||||
* @param {Array<string>} results failures
|
||||
*
|
||||
* @return {string} combined report
|
||||
*/
|
||||
'override protected combineFailureResults'( results )
|
||||
{
|
||||
return "<br /><br />" + results.join( "<br />" );
|
||||
},
|
||||
|
||||
|
||||
/**
|
||||
* 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, '<br />' );
|
||||
},
|
||||
} );
|
|
@ -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 );
|
||||
} );
|
||||
} );
|
||||
} );
|
||||
|
|
Loading…
Reference in New Issue