-
-
Notifications
You must be signed in to change notification settings - Fork 182
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[enhancement] allow custom assertions in xqsuite tests
Add new functions test:fail#3 and test:fail#4 to XQSuite. Calling this function inside a XQSuite test function will stop execution by throwing a special error `test:failure`. This error is caught and handled as a failure allowing to create arbitrary custom assertions and the ability to provide specific error messages. Works in both XQuery and jUnit context. In jUnit you will only be presented with the serialized expected and actual values. For an example custom assertion see custom-assertion.xqm.
- Loading branch information
Showing
2 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
xquery version "3.1"; | ||
|
||
(:~ | ||
: Some tests on features of the test suite itself. | ||
:) | ||
module namespace ca="http://exist-db.org/xquery/test/xqsuite/custom-assertion"; | ||
|
||
import module namespace test="http://exist-db.org/xquery/xqsuite" | ||
at "resource:org/exist/xquery/lib/xqsuite/xqsuite.xql"; | ||
|
||
declare variable $ca:var := map {"a": 1, "b": 2}; | ||
|
||
declare | ||
%test:assertEquals("Key 'b' is missing", "map-assertion-failure") | ||
function ca:missing-key-default-type() as item()* { | ||
try { | ||
ca:map-assertion($ca:var, map {"a": 1, "c": 3}) | ||
} | ||
catch test:failure { | ||
$err:description, $err:value?type | ||
} | ||
}; | ||
|
||
declare | ||
%test:assertEquals("Value mismatch for key 'b'", "custom-assertion-failure") | ||
function ca:wrong-value-custom-type() as item()* { | ||
try { | ||
ca:map-assertion($ca:var, map {"a": 1, "b": 3}) | ||
} | ||
catch test:failure { | ||
$err:description, $err:value?type | ||
} | ||
}; | ||
|
||
declare | ||
%test:assertEquals("Type mismatch", "type-mismatch") | ||
function ca:type-mismatch-custom-type() as item()* { | ||
try { | ||
ca:map-assertion($ca:var, [1,2]) | ||
} | ||
catch test:failure { | ||
$err:description, $err:value?type | ||
} | ||
}; | ||
|
||
declare %private | ||
function ca:map-assertion ($expected as map(*), $actual as item()*) as item()* { | ||
if (exists($actual) and count($actual) eq 1 and $actual instance of map(*)) | ||
then ( | ||
for-each(map:keys($expected), function ($key as xs:anyAtomicType) { | ||
if (not(map:contains($actual, $key))) | ||
then test:fail($expected, $actual, "Key '" || $key || "' is missing", "map-assertion-failure") | ||
else if ($expected($key) ne $actual($key)) | ||
then test:fail($expected, $actual, "Value mismatch for key '" || $key || "'") | ||
else () | ||
}) | ||
, | ||
true() | ||
) | ||
else test:fail($expected, $actual, "Type mismatch", "type-mismatch") | ||
}; |