forked from robwhitby/xray
/
xray.xqy
98 lines (87 loc) · 2.69 KB
/
xray.xqy
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
xquery version "1.0-ml";
module namespace xray = "http://github.com/robwhitby/xray";
declare namespace test = "http://github.com/robwhitby/xray/test";
import module namespace utils = "http://github.com/robwhitby/xray/utils" at "utils.xqy";
declare default element namespace "http://github.com/robwhitby/xray";
declare function xray:run-tests(
$test-dir as xs:string,
$module-pattern as xs:string?,
$test-pattern as xs:string?,
$format as xs:string?
) as item()*
{
let $modules := utils:get-modules($test-dir, fn:string($module-pattern))
let $tests :=
element tests {
for $module in $modules
let $fns :=
try { utils:get-functions($module) }
catch ($ex) { xray:error($ex) }
where fn:exists($fns)
return
element module {
attribute path { utils:relative-path($module) },
if ($fns instance of element(error:error)) then $fns
else (
xray:apply($fns[utils:get-local-name(.) = "setup"]),
for $fn in $fns[fn:not(utils:get-local-name(.) = ("setup", "teardown"))]
where fn:matches(utils:get-local-name($fn), fn:string($test-pattern))
return xray:run-test($fn),
xray:apply($fns[utils:get-local-name(.) = "teardown"])
)
}
}
return
utils:transform($tests, $test-dir, $module-pattern, $test-pattern, $format)
};
declare function xray:run-test(
$fn as xdmp:function
) as element(test)
{
let $ignore := fn:starts-with(utils:get-local-name($fn), "IGNORE")
let $test := if ($ignore) then () else xray:apply($fn)
return element test {
attribute name { utils:get-local-name($fn) },
attribute result {
if ($ignore) then "ignored"
else if ($test/error:error or $test//descendant-or-self::assert[@result="failed"]) then "failed"
else "passed"
},
$test
}
};
declare function xray:test-response(
$assertion as xs:string,
$passed as xs:boolean,
$actual as item()*,
$expected as item()*
) as element(assert)
{
element assert {
attribute test { $assertion },
attribute result { if ($passed) then "passed" else "failed" },
element actual { $actual },
element expected { $expected }
}
};
declare private function xray:apply(
$function as xdmp:function
) as item()*
{
try {
xdmp:eval("
declare variable $fn as xdmp:function external;
declare option xdmp:update 'true';
xdmp:apply($fn)",
(fn:QName("","fn"), $function),
<options xmlns="xdmp:eval"><isolation>different-transaction</isolation></options>
)
}
catch($ex) { element exception { xray:error($ex)} }
};
declare private function xray:error(
$ex as element(error:error)
) as element(error:error)
{
$ex
};