-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_runner.jsonata
62 lines (54 loc) · 2.14 KB
/
test_runner.jsonata
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
(
$appendPath := function($prefix, $path) {
$prefix & '/' & $path
};
$compare := function($actual, $expected, $path) {(
$actual_keys := $keys($actual);
$expected_keys := $keys($expected);
$appender := function($p){appendPath($path, $p)};
$extra_keys := $filter($actual_keys, function($key) { $not($key in $expected_keys) }) ~> $map($appender);
$missing_keys := $filter($expected_keys, function($key) { $not($key in $actual_keys) }) ~> $map($appender);
$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}}
)
)};
function($name, $expression, $input, $expected) { (
$assert($expression != null, "Expression must be set");
$assert($name != null, "Name must be set");
$actual := $eval($expression, $input);
($actual != $expected) ? (
$errors := $compare($actual, $expected);
$errorReport($errors~>|$|{'test': $name}|);
{
'test': $name,
'result': 'failed',
'errors': $errors
}
)
: {
'test': $name,
'result': 'passed'
}
)}
)