Skip to content

Commit

Permalink
Add JsonTestExecutionReportFormatter
Browse files Browse the repository at this point in the history
  • Loading branch information
platan committed Oct 31, 2023
1 parent 90ca0fb commit bc42671
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.github.platan.tests_execution_chart.reporters

import io.github.platan.tests_execution_chart.report.data.TestExecutionScheduleReport

interface TestExecutionReportFormatter {
fun format(report: TestExecutionScheduleReport): String
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,16 @@ package io.github.platan.tests_execution_chart.reporters.json
import io.github.platan.tests_execution_chart.report.ReportConfig
import io.github.platan.tests_execution_chart.report.data.TestExecutionScheduleReport
import io.github.platan.tests_execution_chart.reporters.GanttDiagramReporter
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import java.io.File

class JsonReporter : GanttDiagramReporter<JsonConfig>() {
private val json = Json {
prettyPrint = true
}

override fun report(
report: TestExecutionScheduleReport,
baseDir: File,
taskName: String
) {
val jsonReport = json.encodeToString(report)
val jsonReport = JsonTestExecutionReportFormatter().format(report)
val reportFile = save(jsonReport, taskName, baseDir, config.outputLocation, "json")
logger.lifecycle("Tests execution schedule report saved to ${reportFile.absolutePath} file.")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.github.platan.tests_execution_chart.reporters.json

import io.github.platan.tests_execution_chart.report.data.TestExecutionScheduleReport
import io.github.platan.tests_execution_chart.reporters.TestExecutionReportFormatter
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json

class JsonTestExecutionReportFormatter : TestExecutionReportFormatter {
private val json = Json {
prettyPrint = true
}

override fun format(report: TestExecutionScheduleReport): String {
return json.encodeToString(report)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package io.github.platan.tests_execution_chart.reporters.mermaid

import io.github.platan.tests_execution_chart.report.data.TestExecutionScheduleReport
import io.github.platan.tests_execution_chart.report.data.TimedTestResult
import io.github.platan.tests_execution_chart.reporters.TestExecutionReportFormatter
import io.github.platan.tests_execution_chart.reporters.mermaid.core.MermaidGanttDiagram
import io.github.platan.tests_execution_chart.reporters.mermaid.core.MermaidGanttDiagramFormatter

class TestExecutionMermaidDiagramFormatter {
class TestExecutionMermaidDiagramFormatter : TestExecutionReportFormatter {

companion object {
val taskFormat: Map<TimedTestResult.Type, Map<String, String>> = mapOf(
Expand All @@ -14,7 +15,7 @@ class TestExecutionMermaidDiagramFormatter {
)
}

fun format(report: TestExecutionScheduleReport): String {
override fun format(report: TestExecutionScheduleReport): String {
val diagramBuilder = MermaidGanttDiagram.MermaidGanttDiagramBuilder()
report.results.groupBy { result -> result.className }.forEach { (className, results) ->
diagramBuilder.addSection(className.orEmpty())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.github.platan.tests_execution_chart.reporters.json

import io.github.platan.tests_execution_chart.report.TestExecutionScheduleReportBuilder
import spock.lang.Specification
import spock.lang.Subject

class JsonTestExecutionReportFormatterTest extends Specification {

@Subject
def formatter = new JsonTestExecutionReportFormatter()

def "should format empty report"() {
given:
def emptyReport = new TestExecutionScheduleReportBuilder().getResults()

when:
def result = formatter.format(emptyReport)

then:
result ==
"""{
| "results": [
| ],
| "marks": [
| ]
|}""".stripMargin()
}

}

0 comments on commit bc42671

Please sign in to comment.