-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresults.kt
160 lines (129 loc) · 2.8 KB
/
results.kt
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package datkt.tape
import datkt.tape.Test
import datkt.tape.Stream
/**
* The `Results` class represents a container for all
* observed test results.
*/
class Results {
var closed: Boolean = false
val stream: Stream
var tests: Array<Test> = emptyArray()
var count: Int = 0
var fail: Int = 0
var pass: Int = 0
/**
* Results class constructor.
*/
constructor(stream: Stream? = null) {
this.stream = if (null != stream) stream else Stream(::print)
}
/**
* Adds a test to watch for results
*/
fun push(t: Test): Results {
this.tests += t
this.watch(t)
return this
}
/**
* Watch a test for results.
*/
fun watch(t: Test): Results {
if (this.closed) {
return this
}
val stream = this.stream
t.onBeforeRun(fun(_) {
if (this.closed) {
return
}
stream.write("# ${t.name}\n")
})
t.onResult(fun(_, result: Any?) {
if (this.closed) {
return
}
if (result is String) {
stream.write("# ${result}\n")
return
}
this.count++
if (result is AssertionResult) {
stream.write(encode(result, this.count))
if (result.ok) {
this.pass++
} else {
this.fail++
}
}
})
return this
}
/**
* Collect results and write them to underlying
* write stream.
*/
fun collect(): Results {
if (this.closed || stream.ended) {
return this
}
stream.write("\n")
stream.write("1..${this.count}\n")
stream.write("# tests ${this.count}\n")
stream.write("# pass ${this.pass}\n")
if (this.fail > 0) {
stream.write("# fail ${this.fail}\n")
} else {
stream.write("\n")
stream.write("# ok\n")
}
stream.end()
return this
}
/**
* Closes the results
*/
fun close(): Results {
if (this.closed) {
return this
}
this.closed = true
return this
}
}
/**
* Encodes an AssertionResult into a TAP result with YAML details
* showing expected and actual values, if given.
*/
fun encode(result: AssertionResult, count: Int): String {
var output: Array<String> = emptyArray()
var inner = " "
var outer = " "
fun push(s: Any?) { output += "${s?.toString()}" }
fun join() = output.joinToString(" ")
if (result.ok) {
push("ok")
} else {
push("not ok")
}
push(count)
if (truthy(result.name)) {
push(result.name?.replace("""\s+""", " "))
}
if (result.skip) {
push("# SKIP")
}
push("\n")
if (result.ok) {
return join()
}
push("${outer}---\n")
push("${inner}operator: ${result.op}\n")
if (null != result.expected || null != result.actual) {
push("${inner}expected: ${result.expected}\n")
push("${inner}actual: ${result.actual}\n")
}
push("${outer}...\n")
return join()
}