-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCodewarsReporter.cfc
120 lines (101 loc) · 3.98 KB
/
CodewarsReporter.cfc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/**
* Renders TestResult from TestBox in Codewars format.
* Based on `CLIRenderer@testbox-commands`.
*/
component {
/**
* @testData test results from TestBox
*/
function render( testData ){
for ( thisBundle in testData.bundleStats ) {
// Check if the bundle threw a global exception
if ( !isSimpleValue( thisBundle.globalException ) ) {
var message = escapeLF(
"#thisBundle.globalException.type#:#thisBundle.globalException.message#:#thisBundle.globalException.detail#"
);
printLine( prependLF( "<ERROR::>#message#" ) );
// ACF has an array for the stack trace
if ( isSimpleValue( thisBundle.globalException.stacktrace ) ) {
printLine( prependLF( "<LOG::-Stacktrace>#escapeLF( thisBundle.globalException.stacktrace )#" ) );
}
}
var debugMap = prepareDebugBuffer( thisBundle.debugBuffer );
// Generate reports for each suite
for ( var suiteStats in thisBundle.suiteStats ) {
genSuiteReport( suiteStats = suiteStats, bundleStats = thisBundle, debugMap = debugMap );
}
}
}
/**
* Recursive Output for suites
* @suiteStats Suite stats
* @bundleStats Bundle stats
*/
function genSuiteReport( required suiteStats, required bundleStats, debugMap={}, labelPrefix='' ){
labelPrefix &= '/' & arguments.suiteStats.name;
printLine( prependLF( "<DESCRIBE::>#arguments.suiteStats.name#" ) );
for ( local.thisSpec in arguments.suiteStats.specStats ) {
var thisSpecLabel = labelPrefix & '/' & local.thisSpec.name;
printLine( prependLF( "<IT::>#local.thisSpec.name#" ) );
if( debugMap.keyExists( thisSpecLabel ) ) {
printLine( debugMap[ thisSpecLabel ] )
}
if ( local.thisSpec.status == "passed" ) {
printLine( prependLF( "<PASSED::>Test Passed" ) );
} else if ( local.thisSpec.status == "failed" ) {
printLine( prependLF( "<FAILED::>#escapeLF( local.thisSpec.failMessage )#" ) );
} else if ( local.thisSpec.status == "skipped" ) {
printLine( prependLF( "<FAILED::>Test Skipped" ) );
} else if ( local.thisSpec.status == "error" ) {
printLine( prependLF( "<ERROR::>#escapeLF( local.thisSpec.error.message )#" ) );
var errorStack = [];
// If there's a tag context, show the file name and line number where the error occurred
if (
isDefined( "local.thisSpec.error.tagContext" ) && isArray( local.thisSpec.error.tagContext ) && local.thisSpec.error.tagContext.len()
) {
errorStack = thisSpec.error.tagContext;
} else if (
isDefined( "local.thisSpec.failOrigin" ) && isArray( local.thisSpec.failOrigin ) && local.thisSpec.failOrigin.len()
) {
errorStack = thisSpec.failOrigin;
}
if ( errorStack.len() ) {
var stacktrace = errorStack
.slice( 1, 5 )
.map( function( item ){
return "at #item.template#:#item.line#";
} )
.toList( "<:LF:>" );
printLine( prependLF( "<LOG::-Stacktrace>#stacktrace#" ) );
}
} else {
printLine( prependLF( "<ERROR::>Unknown test status: #local.thisSpec.status#" ) );
}
printLine( prependLF( "<COMPLETEDIN::>#local.thisSpec.totalDuration#" ) );
}
// Handle nested Suites
if ( arguments.suiteStats.suiteStats.len() ) {
for ( local.nestedSuite in arguments.suiteStats.suiteStats ) {
genSuiteReport( local.nestedSuite, arguments.bundleStats, debugMap, labelPrefix )
}
}
printLine( prependLF( "<COMPLETEDIN::>#arguments.suiteStats.totalDuration#" ) );
}
private function escapeLF( required text ){
return replace( text, chr( 10 ), "<:LF:>", "all" );
}
private function prependLF( required text ){
return "#chr( 10 )##text#";
}
// Transofrm array of messages to struct keyed on message label containing an array of
private function prepareDebugBuffer( array debugBuffer ) {
return debugBuffer.reduce( ( debugMap={}, d )=> {
debugMap[ d.label ] = debugMap[ d.label ] ?: '';
debugMap[ d.label ] &= prependLF( isSimpleValue( d.data ) ? d.data : serialize( d.data ) );
return debugMap;
} ) ?: {};
}
private function printLine( string str ) {
systemoutput( str, 1 );
}
}