Scoop API

scoop:scoop.test:0.1.0

src/execution/report.scoop

Selected production source · src/execution/report.scoop

1package scoop.test.execution
3@Extern(name = "scoop_test_begin", abi = "c")
4private fun nativeBegin(pointer: Ptr<UInt8>, length: Long): Int
5@Extern(name = "scoop_test_end", abi = "c")
6private fun nativeEnd(): Long
7@Extern(name = "scoop_test_event", abi = "c")
8private fun nativeEvent(pointer: Ptr<UInt8>, length: Long): Int
9@Extern(name = "scoop_test_complete", abi = "c")
10private fun nativeComplete(): Int
12private 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 }
20private 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 }
29private fun endCapture(): Long {
30 @Unsafe { return nativeEnd() }
33internal 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) + "]"
40/**
41 * Executes one selected test case and emits its structured start and result events for the
42 * command-line test runner.
43 */
44public fun runReportedTestCase(
45 caseIndex: String, location: SourceLocation, initialize: () -> TestActions
46): 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 result
60/**
61 * Emits the final completion event for the generated test runner after all selected cases have
62 * been handled.
63 */
64public fun completeTestRun() {
65 @Unsafe {
66 if (nativeComplete() < 0) {
67 throw IllegalStateException(Some("cannot write test completion"))
68 }
69 }