|
| 1 | +/** |
| 2 | + * Renders TestResult from TestBox in Codewars format. |
| 3 | + * Based on `CLIRenderer@testbox-commands`. |
| 4 | + */ |
| 5 | +component { |
| 6 | + |
| 7 | + /** |
| 8 | + * @print a print buffer to use |
| 9 | + * @testData test results from TestBox |
| 10 | + */ |
| 11 | + function render( print, testData ){ |
| 12 | + for ( thisBundle in testData.bundleStats ) { |
| 13 | + // Check if the bundle threw a global exception |
| 14 | + if ( !isSimpleValue( thisBundle.globalException ) ) { |
| 15 | + var message = escapeLF( |
| 16 | + "#thisBundle.globalException.type#:#thisBundle.globalException.message#:#thisBundle.globalException.detail#" |
| 17 | + ); |
| 18 | + print.line( prependLF( "<ERROR::>#message#" ) ); |
| 19 | + |
| 20 | + // ACF has an array for the stack trace |
| 21 | + if ( isSimpleValue( thisBundle.globalException.stacktrace ) ) { |
| 22 | + print.line( prependLF( "<LOG::-Stacktrace>#escapeLF( thisBundle.globalException.stacktrace )#" ) ); |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + // Generate reports for each suite |
| 27 | + for ( var suiteStats in thisBundle.suiteStats ) { |
| 28 | + genSuiteReport( suiteStats = suiteStats, bundleStats = thisBundle, print = print ); |
| 29 | + } |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Recursive Output for suites |
| 35 | + * @suiteStats Suite stats |
| 36 | + * @bundleStats Bundle stats |
| 37 | + * @print The print Buffer |
| 38 | + */ |
| 39 | + function genSuiteReport( required suiteStats, required bundleStats, required print ){ |
| 40 | + print.line( prependLF( "<DESCRIBE::>#arguments.suiteStats.name#" ) ); |
| 41 | + |
| 42 | + for ( local.thisSpec in arguments.suiteStats.specStats ) { |
| 43 | + print.line( prependLF( "<IT::>#local.thisSpec.name#" ) ); |
| 44 | + |
| 45 | + if ( local.thisSpec.status == "passed" ) { |
| 46 | + print.line( prependLF( "<PASSED::>Test Passed" ) ); |
| 47 | + } else if ( local.thisSpec.status == "failed" ) { |
| 48 | + print.line( prependLF( "<FAILED::>#escapeLF( local.thisSpec.failMessage )#" ) ); |
| 49 | + } else if ( local.thisSpec.status == "error" ) { |
| 50 | + print.line( prependLF( "<ERROR::>#escapeLF( local.thisSpec.error.message )#" ) ); |
| 51 | + |
| 52 | + var errorStack = []; |
| 53 | + // If there's a tag context, show the file name and line number where the error occurred |
| 54 | + if ( |
| 55 | + isDefined( "local.thisSpec.error.tagContext" ) && isArray( local.thisSpec.error.tagContext ) && local.thisSpec.error.tagContext.len() |
| 56 | + ) { |
| 57 | + errorStack = thisSpec.error.tagContext; |
| 58 | + } else if ( |
| 59 | + isDefined( "local.thisSpec.failOrigin" ) && isArray( local.thisSpec.failOrigin ) && local.thisSpec.failOrigin.len() |
| 60 | + ) { |
| 61 | + errorStack = thisSpec.failOrigin; |
| 62 | + } |
| 63 | + |
| 64 | + if ( errorStack.len() ) { |
| 65 | + var stacktrace = errorStack |
| 66 | + .slice( 1, 5 ) |
| 67 | + .map( function( item ){ |
| 68 | + return "at #item.template#:#item.line#"; |
| 69 | + } ) |
| 70 | + .toList( "<:LF:>" ); |
| 71 | + print.line( prependLF( "<LOG::-Stacktrace>#stacktrace#" ) ); |
| 72 | + } |
| 73 | + } else { |
| 74 | + print.line( prependLF( "<ERROR::>Unknown test status: #local.thisSpec.status#" ) ); |
| 75 | + } |
| 76 | + |
| 77 | + print.line( prependLF( "<COMPLETEDIN::>#local.thisSpec.totalDuration#" ) ); |
| 78 | + } |
| 79 | + |
| 80 | + // Handle nested Suites |
| 81 | + if ( arguments.suiteStats.suiteStats.len() ) { |
| 82 | + for ( local.nestedSuite in arguments.suiteStats.suiteStats ) { |
| 83 | + genSuiteReport( local.nestedSuite, arguments.bundleStats, print ) |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + print.line( prependLF( "<COMPLETEDIN::>#arguments.suiteStats.totalDuration#" ) ); |
| 88 | + } |
| 89 | + |
| 90 | + private function escapeLF( required text ){ |
| 91 | + return replace( text, chr( 10 ), "<:LF:>", "all" ); |
| 92 | + } |
| 93 | + |
| 94 | + private function prependLF( required text ){ |
| 95 | + return "#chr( 10 )##text#"; |
| 96 | + } |
| 97 | + |
| 98 | +} |
0 commit comments