-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtap.elv
378 lines (342 loc) · 9.57 KB
/
tap.elv
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
use re
use str
# TAP producer to run all the tests in a tests suite.
# Note that checking test success must be done separately, using `status`.
#
# `tests` is a list of maps, where each map is a test.
# A test comprises a map with the following keys:
# - `d` - a string, the test name or description
# - `f` - a function of no arguments, outputing the results as maps. Multiple results are possible, and correspond to TAP subtests.
# The map has a mandatory field `ok`, a boolean, whether the test passed. Any other fields are included as a TAP YAML block.
# - `skip` - test is skipped
# - `todo` - test is not yet implemented
#
# `d` is mandatory, and so is `f` unless `todo` is present.
fn run { |tests|
fn validate-tests { |tests|
var tests-kind = (kind-of $tests)
if (not-eq $tests-kind list) {
fail 'tests must be list, found '$tests-kind
}
var i = 0
for test $tests {
# TAP numbers tests from 1
set i = (+ 1 $i)
var test-kind = (kind-of $test)
if (not-eq $test-kind map) {
fail 'test '$i' must be map, found '$test-kind': '$test
}
if (not (has-key $test d)) {
fail 'test '$i' missing `d`'
}
var d-kind = (kind-of $test[d])
if (not-eq $d-kind string) {
fail 'test '$i' `d` must be string, found '$d-kind
}
if (has-key $test skip) {
var skip-kind = (kind-of $test[skip])
if (not-eq $skip-kind bool) {
fail 'test '$i' `skip` must be bool, found '$skip-kind
}
}
var todo = $false
if (has-key $test todo) {
var todo-kind = (kind-of $test[todo])
if (not-eq $todo-kind bool) {
fail 'test '$i' `todo` must be bool, found '$todo-kind
}
if $test[todo] {
set todo = $true
}
}
if (not (has-key $test f)) {
if (not $todo) {
fail 'test '$i' missing `f`'
}
} else {
var f-kind = (kind-of $test[f])
if (not-eq $f-kind fn) {
fail 'test '$i' `f` must be fn, found '$f-kind
}
}
}
}
fn validate-test-results { |i-test results|
var i = 0
for result $results {
# number results from 1 like TAP numbers tests
set i = (+ 1 $i)
var result-kind = (kind-of $result)
if (not-eq $result-kind map) {
fail 'test '$i-test' result '$i' must be map, found '$result-kind': '(to-string $result)
}
if (not (has-key $result ok)) {
fail 'test '$i-test' result '$i' missing `ok`'
}
var ok-kind = (kind-of $result[ok])
if (not-eq $ok-kind bool) {
fail 'test '$i-test' result '$i' `ok` must be bool, found '$ok-kind
}
}
}
fn write-yaml-block { |block|
var yaml = (var ok = ?(
put $block | to-json | yq --yaml-output --sort-keys --explicit-start --explicit-end | from-lines | {
each { |line|
put ' '$line
} | put [(all)]
}
))
if $ok {
echo (str:join "\n" $yaml)
} else {
echo ' --- YAML block elided because yq not found'
echo ' ...'
}
}
fn write-result { |i test result|
var status = (if $result[ok] { put 'ok' } else { put 'not ok' })
var directive = (
if (and (has-key $test skip) $test[skip]) {
put ' # skip'
} elif (and (has-key $test todo) $test[todo]) {
put ' # todo'
} else {
put ''
}
)
echo $status' '$i' - '$test[d]$directive
var yaml-block = (dissoc $result ok)
if (not-eq [&] $yaml-block) {
write-yaml-block $yaml-block
}
}
validate-tests $tests
echo 'TAP version 13'
echo '1..'(count $tests)
var i-test = 0
for test $tests {
# TAP numbers tests from 1
set i-test = (+ 1 $i-test)
if (and (has-key $test todo) $test[todo]) {
# TODO tests are recorded as failure
write-result $i-test $test [&ok=$false]
continue
}
var results = (var f_ok = ?($test[f] | put [(all)]))
if (not $f_ok) {
write-result $i-test $test [&ok=$false &exception=$f_ok[reason]]
} elif (== (count $results) 0) {
# no results, which we interpret as todo
write-result $i-test (assoc $test todo $true) [&ok &reason='test function wrote no result']
} else {
validate-test-results $i-test $results
if (== (count $results) 1) {
write-result $i-test $test $results[0]
} else {
# multiple results would be subtests in TAP14, but for TAP13 we squish them into a single test
var all-ok = $true
var all-results = [&]
var i-result = 0
for result $results {
# number results from 1
set i-result = (+ 1 $i-result)
if (not $result[ok]) {
set all-ok = $false
}
set all-results = (assoc $all-results $i-result $result)
}
var composite-description = $test[d]' ('(count $results)' results)'
var composite-test = [&d= $composite-description]
if $all-ok {
write-result $i-test $composite-test [&ok=$all-ok]
} else {
write-result $i-test $composite-test (assoc $all-results ok $all-ok)
}
}
}
}
}
# Simple assertion of condition being $true
fn assert { |condition|
put [&ok=$condition]
}
# Assert that the actual value is as expected
fn assert-expected { |actual expected|
if (eq $actual $expected) {
put [&ok]
} else {
put [&ok=$false &expected=$expected &actual=$actual]
}
}
# Simple TAP consumer to check test success and format output
# Assumes valid TAP input
#
# Exits with status code 1 on test failure, unless inhibited with &exit=false
fn status { |&exit=true|
fn find-one { |pattern source|
{ re:find &max=1 $pattern $source ; put [&] } | take 1
}
fn parse-line { |line|
if (str:has-prefix $line 'TAP version') {
put [&type=version]
} else {
# plan
var m = (find-one '^1\.\.(\d+)' $line)
if (has-key $m start) {
put [
&type=plan
&n=(num $m[groups][1][text])
]
} else {
# test point
var m = (find-one '^(not *)?ok *((\d+)?( *-)? *([^#]*)( *# *(\w*)( *(.*))?)?)$' $line)
if (has-key $m start) {
var pass = (eq $m[groups][1][text] '')
var status = (if $pass { put '✓' } else { put '✗' })
put [
&type=test-point
&pass=$pass
&directive=(str:to-lower $m[groups][7][text])
&text=$status' '$m[groups][2][text]
]
} else {
# begin YAML
var m = (find-one '^ *---' $line)
if (has-key $m start) {
put [
&type=begin-yaml
&text=$line
]
} else {
# end YAML
var m = (find-one '^ *\.\.\.' $line)
if (has-key $m start) {
put [
&type=end-yaml
&text=$line
]
} else {
# bail out
var m = (find-one '^Bail out!' $line)
if (has-key $m start) {
put [
&type=end-yaml
&text=$line
]
} else {
# empty
var m = (find-one '^\s*$' $line)
if (has-key $m start) {
put [
&type=empty
&text=$line
]
} else {
put [
&type=unknown
&text=$line
]
}
}
}
}
}
}
}
}
fn plural { |n|
if (== $n 1) {
put ''
} else {
put 's'
}
}
var red = "\e[31m"
var yellow = "\e[33m"
var green = "\e[32m"
var bold = "\e[1m"
var normal = "\e[39;0m"
var plan = $false
var n-plan = 0
var n-pass = 0
var n-fail = 0
var n-skip = 0
var n-todo = 0
var in-yaml = $false
var bail_out = $false
var colour = $normal
from-lines | each { |line|
var parsed = (parse-line $line)
if (==s plan $parsed[type]) {
set n-plan = $parsed[n]
} elif (==s test-point $parsed[type]) {
set colour = (
if (==s skip $parsed[directive]) {
set n-skip = (+ 1 $n-skip)
put $yellow
} elif (==s todo $parsed[directive]) {
set n-todo = (+ 1 $n-todo)
put $yellow
} elif $parsed[pass] {
set n-pass = (+ 1 $n-pass)
put $green
} else {
set n-fail = (+ 1 $n-fail)
put $red
}
)
} elif (==s begin-yaml $parsed[type]) {
set in-yaml = $true
} elif (==s end-yaml $parsed[type]) {
set in-yaml = $false
} elif (==s bail-out $parsed[type]) {
set bail_out = $true
set colour = $red
} elif (and (==s unknown $parsed[type]) (not $in-yaml)) {
set colour = $yellow
}
if (has-key $parsed text) {
echo $colour$parsed[text]$normal
}
}
# summary
set colour = (
if (> $n-fail 0) {
put $red
} elif (or (> $n-skip 0) (> $n-todo 0)) {
put $yellow
} else {
put $green
}
)
echo
print $bold$colour$n-plan' tests'
if (> $n-pass 0) {
print ', '$n-pass' passed'
}
if (> $n-fail 0) {
print ', '$n-fail' failed'
}
if (> $n-skip 0) {
print ', '$n-skip' skipped'
}
if (> $n-todo 0) {
print ', '$n-todo' todo'
}
echo $normal
var ok = (== $n-fail 0)
if (and $exit (not $ok)) {
exit 1
}
if (not $exit) {
put $ok
}
}
# Simple TAP consumer to check test success and format output
# Assumes valid TAP input
#
# Returns test success as a boolean
fn format {
status &exit=$false
}