scoop:scoop.test:0.1.0
src/execution/report.scoop
Selected production source · src/execution/report.scoop
1
package scoop.test.execution3
@Extern(name = "scoop_test_begin", abi = "c")4
private fun nativeBegin(pointer: Ptr<UInt8>, length: Long): Int5
@Extern(name = "scoop_test_end", abi = "c")6
private fun nativeEnd(): Long7
@Extern(name = "scoop_test_event", abi = "c")8
private fun nativeEvent(pointer: Ptr<UInt8>, length: Long): Int9
@Extern(name = "scoop_test_complete", abi = "c")10
private fun nativeComplete(): Int12
private fun begin(caseIndex: String): Int {13
@Unsafe {14
return caseIndex.withUtf8Bytes<Int>(fun(pointer: Ptr<UInt8>, length: Long): Int {15
@Unsafe { return nativeBegin(pointer, length) }16
})17
}18
}20
private fun event(record: String) {21
@Unsafe {22
val status = record.withUtf8Bytes<Int>(fun(pointer: Ptr<UInt8>, length: Long): Int {23
@Unsafe { return nativeEvent(pointer, length) }24
})25
if (status < 0) { throw IllegalStateException(Some("cannot write test event")) }26
}27
}29
private fun endCapture(): Long {30
@Unsafe { return nativeEnd() }31
}33
internal fun testFailuresJson(result: TestResult): String = when (result) {34
case TestResult.Passed -> "[]"35
case TestResult.Failed(val first, None) -> "[" + testFailureJson(first) + "]"36
case TestResult.Failed(val first, Some(val last)) ->37
"[" + testFailureJson(first) + "," + testFailureJson(last) + "]"38
}40
/**41
* Executes one selected test case and emits its structured start and result events for the42
* command-line test runner.43
*/44
public fun runReportedTestCase(45
caseIndex: String, location: SourceLocation, initialize: () -> TestActions46
): TestResult {47
val recording = begin(caseIndex)48
if (recording < 0) { throw IllegalStateException(Some("cannot begin test capture")) }49
val result = runTestCase(location, initialize)50
if (recording > 0) {51
val elapsed = endCapture()52
if (elapsed < 0L) { throw IllegalStateException(Some("cannot finish test capture")) }53
val failures = testFailuresJson(result)54
event("{\"event\":\"end\",\"case\":" + testJsonString(caseIndex) +55
",\"duration_ns\":" + elapsed.toString() + ",\"failures\":" + failures + "}")56
}57
return result58
}60
/**61
* Emits the final completion event for the generated test runner after all selected cases have62
* been handled.63
*/64
public fun completeTestRun() {65
@Unsafe {66
if (nativeComplete() < 0) {67
throw IllegalStateException(Some("cannot write test completion"))68
}69
}70
}