summaryrefslogtreecommitdiff
path: root/src/TestResultFormatter.kt
blob: 3a19444dabaf4a7edcb6335973ea3a56036d6a2e (plain)
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
package moe.nea.lisp

import java.text.SimpleDateFormat
import java.util.*
import javax.xml.stream.XMLStreamWriter

class TestResultFormatter(private val writer: XMLStreamWriter) {
    companion object {
        private val timestampFormatter = SimpleDateFormat(
            "yyyy-MM-dd'T'hh:mm:ss"
        )

        fun write(writer: XMLStreamWriter, testResults: List<TestFramework.TestSuite>) {
            TestResultFormatter(writer).writeAll(testResults)
        }
    }

    fun writeTestSuite(testSuite: TestFramework.TestSuite) {
        writer.writeStartElement("testsuite")
        writer.writeAttribute("name", testSuite.name)
        writer.writeAttribute("tests", testSuite.allTests.size.toString())
        writer.writeAttribute("skipped", testSuite.allTests.count { it.wasSkipped }.toString())
        writer.writeAttribute("failures", testSuite.allTests.count { it.failures.isNotEmpty() }.toString())
        writer.writeAttribute("errors", "0") // TODO: figure out how to differentiate errors and failures
        writer.writeAttribute("timestamp", timestampFormatter.format(Date.from(testSuite.startTime)))

        writer.writeStartElement("properties")
        writer.writeEndElement()

        testSuite.allTests.forEach {
            writeTestCase(it)
        }
        writer.writeStartElement("system-out")
        writer.writeCData(testSuite.otherOutput)
        writer.writeEndElement()

        writer.writeEndElement()
    }

    fun writeTestCase(test: TestFramework.TestResult) {
        writer.writeStartElement("testcase")
        writer.writeAttribute("name", test.name)
        writer.writeAttribute("time", "0.0") // TODO: proper timestamping

        if (test.wasSkipped) {
            writeSkipped()
        }
        for (fail in test.failures) {
            writeFailure(fail)
        }
        writer.writeStartElement("system-out")
        writer.writeCData(test.stdout)
        writer.writeEndElement()

        writer.writeEndElement()
    }

    fun writeSkipped() {
        writer.writeStartElement("skipped")
        writer.writeEndElement()
    }

    fun writeFailure(fail: TestFramework.TestFailure) {
        writer.writeStartElement("failure")
        writer.writeAttribute("message", fail.message)
        writer.writeCData(fail.callsite.toSource())
        writer.writeEndElement()
    }

    fun writeAll(testResults: List<TestFramework.TestSuite>) {
        writer.writeStartDocument()
        writer.writeStartElement("testsuites")

        testResults.forEach {
            writeTestSuite(it)
        }

        writer.writeEndElement()
        writer.writeEndDocument()
    }
}