-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_functions.yaml
70 lines (67 loc) · 2.18 KB
/
test_functions.yaml
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
appendPath: |-
${
function($prefix, $path) {
$prefix & '/' & $path
}
}
compare: |-
${
function($actual, $expected, $path) {(
$actual_keys := $keys($actual);
$expected_keys := $keys($expected);
$extra_keys := $filter($actual_keys, function($key) { $not($key in $expected_keys) }) ~> $map(appendPath($path, ?));
$missing_keys := $filter($expected_keys, function($key) { $not($key in $actual_keys) }) ~> $map(appendPath($path, ?));
$diff := $map($actual_keys, function($key) {
$key in $expected_keys ? findDiff($lookup($actual, $key), $lookup($expected, $key), appendPath($path, $key))
});
{
"missing": $missing_keys ~> $append($diff.missing),
"extra": $extra_keys ~> $append($diff.extra),
"diff": $diff.diff[]
}
)}
}
findDiff: |-
${
function($actual, $expected, $path) {(
$actual != $expected ?
(
$type($actual) != $type($expected) ?
{
'diff': {'path': $path, 'diff': 'type', 'expected': $type($expected), 'actual': $type($actual) }
} :
$type($actual) = 'array' ?
(
$count($actual) != $count($expected) ?
{'diff': {'path': $path, 'diff': 'count', 'expected': $count($expected), 'actual': $count($actual)}}
: $map($actual, function($item, $i) {
compare($item, $expected[$i], $path & '[' & $i & ']' )
})~>$reduce($append);
) :
$type($actual) = 'object' ?
compare($actual, $expected, $path) :
{
'diff': {'path': $path, 'diff': 'value', 'expected': $expected, 'actual': $actual}
}
)
)}
}
runTest: |-
${
function($name, $function, $input, $expected) {(
$actual := $eval($function, $input);
($actual != $expected) ? (
$errors := compare($actual, $expected);
$errorReport($errors~>|$|{'test': $name}|);
{
'test': $name,
'result': 'failed',
'errors': $errors
}
):
{
'test': $name,
'result': 'passed'
}
)}
}