Scoop API

scoop:scoop.test:0.1.0

src/execution/report_json.scoop

Selected production source · src/execution/report_json.scoop

1package scoop.test.execution
3import scoop.test.AssertionFailure
4import scoop.test.AssertionValue
6internal fun testJsonString(value: String): String {
7 val result = StringBuilder().append("\"")
8 for (character in value) {
9 val code = character.code
10 if (character == '"') { result.append("\\\"") }
11 else if (character == '\\') { result.append("\\\\") }
12 else if (code < 32) {
13 val digits = "0123456789abcdef"
14 result.append("\\u00").append(digits.getCharAt((code / 16).toLong()))
15 .append(digits.getCharAt((code % 16).toLong()))
16 } else { result.append(character) }
17 }
18 return result.append("\"").toString()
21private fun optionalText(value: String?): String = when (value) {
22 case None -> "null"
23 case Some(val text) -> testJsonString(text)
26private fun describeThrowable(error: Throwable): String {
27 if (error is Exception) {
28 return when (error.message) {
29 case Some(val message) -> message
30 case None -> "Exception"
31 }
32 }
33 if (error is ToString) {
34 return try { error.toString() }
35 catch (rendering: Throwable) { "Throwable (description rendering failed)" }
36 }
37 return "Throwable"
40private fun assertionValue(value: AssertionValue): String = when (value) {
41 case AssertionValue.Unavailable -> "{\"kind\":\"unavailable\"}"
42 case AssertionValue.Text(val text) -> "{\"kind\":\"text\",\"text\":" + testJsonString(text) + "}"
43 case AssertionValue.RenderingFailed(val error) ->
44 "{\"kind\":\"rendering-failed\",\"message\":" + testJsonString(describeThrowable(error)) + "}"
47private fun locationJson(location: SourceLocation): String =
48 "{\"file\":" + testJsonString(location.file) +
49 ",\"line\":" + location.line.toString() + ",\"column\":" + location.column.toString() +
50 ",\"function\":" + testJsonString(location.functionName) +
51 ",\"type\":" + testJsonString(location.typeName) + "}"
53internal fun testFailureJson(failure: TestFailure): String {
54 val phase = when (failure.phase) {
55 case TestPhase.Initialization -> "initialization"
56 case TestPhase.BeforeTest -> "before-test"
57 case TestPhase.Body -> "body"
58 case TestPhase.AfterTest -> "after-test"
59 }
60 val start = "{\"phase\":" + testJsonString(phase) + ",\"location\":" + locationJson(failure.location)
61 val error = failure.cause
62 if (error is AssertionFailure) {
63 val cause: String = when (error.cause) {
64 case None -> "null"
65 case Some(val value) -> testJsonString(describeThrowable(value))
66 }
67 return start + ",\"kind\":\"assertion\",\"assertion\":" + testJsonString(error.kind) +
68 ",\"message\":" + optionalText(error.message) +
69 ",\"expected\":" + assertionValue(error.expected) +
70 ",\"actual\":" + assertionValue(error.actual) + ",\"cause\":" + cause + "}"
71 }
72 return start + ",\"kind\":\"exception\",\"message\":" + testJsonString(describeThrowable(error)) + "}"