-
Notifications
You must be signed in to change notification settings - Fork 0
/
jq-unit.jq
94 lines (84 loc) · 1.98 KB
/
jq-unit.jq
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
module {
"name": "jq-unit",
"description": "jq-unit is a testframework for jq script.",
"repository": {
"type": "git",
"url": "https://github.com/mike-neck/jq-unit.git",
"license": "MIT",
"author": "Shinya Mochida(mike-neck)"
}
};
# type-name: TestTitle
# properties:
# - name: string
# type-name: TestInput
# properties:
# - name: string
# - input: object | array | string | number | boolean | null
# type-name: TestSubject
# properties:
# - name: string
# - subject: object | array | string | number | boolean | null
# - error: object | null
# type-name: Test
# properties:
# - name: string
# - subject: object | array | string | number | boolean | null
# - test: boolean
# argument: string
# input: N/A
# output: TestTitle
def test(title):
{name: "\(title)"}
;
# argument: object | array | string | number | boolean | null
# input: TestTitle
# output: TestInput
def Given(input):
{ name: .name, input: input }
;
# argument: filter(to subject: object | array | string | number | boolean | null)
# input: TestInput
# output: TestSubject
def When(operation):
. as $input |
try
{ name: $input.name, subject: ($input.input | operation) }
catch
{ name: $input.name, error: "\($input.input) cannot be applied to the operation on test \($input.name)." }
;
# argument: filter(to test: boolean)
# input: TestSubject
# output: Test
def Then(condition):
if .error == null then
{
name: .name,
subject: .subject,
test: (.subject | condition)
}
else
{
name: .name,
subject: .error,
test: false
}
end
;
# argument: iterator(of: Test)
# input: N/A
# output: "ok" or error
def allSpecs(tests):
[tests] |
length as $testSize |
map(select(.test != true)) |
if length == 0
then
([ "ok - tests: \($testSize)" ] +
( [ tests ] | map(" - " + .name) ) )[]
else
["test failed", (map( "test: \(.name) [unexpected-value: \(.subject)]" ) | .[]), "count: \(length)/\($testSize)"] |
join("\n") |
error(.)
end
;