Skip to content

Commit 7dd80c8

Browse files
committed
[enhancement] reorder arguments to test:fail
- `test:fail#3`: fail with custom **message**, expected and actual value - `test:fail#4`: allows to set an additional type The tests in custom-assertion.xqm were adapted and enhanced.
1 parent 62d6beb commit 7dd80c8

File tree

2 files changed

+74
-33
lines changed

2 files changed

+74
-33
lines changed

exist-core/src/main/resources/org/exist/xquery/lib/xqsuite/xqsuite.xql

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ declare variable $test:UNKNOWN_ASSERTION := QName($test:TEST_NAMESPACE, "no-such
5858
declare variable $test:WRONG_ARG_COUNT := QName($test:TEST_NAMESPACE, "wrong-number-of-arguments");
5959
declare variable $test:TYPE_ERROR := QName($test:TEST_NAMESPACE, "type-error");
6060
declare variable $test:UNKNOWN_ANNOTATION_VALUE_TYPE := QName($test:TEST_NAMESPACE, "unknown-annotation-value-type");
61+
declare variable $test:CUSTOM_ASSERTION_FAILURE_TYPE := "custom-assertion-failure";
6162

6263
(:~
6364
: Main entry point into the module. Takes a sequence of function items.
@@ -139,14 +140,14 @@ declare function test:suite(
139140
</testsuites>
140141
};
141142

142-
declare function test:fail ($expected as item()*, $actual as item()*, $message as xs:string) as empty-sequence() {
143-
test:fail($expected, $actual, $message, "custom-assertion-failure")
143+
declare function test:fail ($message as xs:string, $expected as item()*, $actual as item()*) as empty-sequence() {
144+
test:fail($message, $expected, $actual, $test:CUSTOM_ASSERTION_FAILURE_TYPE)
144145
};
145146

146147
declare function test:fail (
148+
$message as xs:string,
147149
$expected as item()*,
148150
$actual as item()*,
149-
$message as xs:string,
150151
$type as xs:string
151152
) as empty-sequence() {
152153
error(xs:QName("test:failure"), $message, map {
@@ -406,8 +407,8 @@ declare %private function test:test(
406407
test:print-result($meta, $result, $assertResult)
407408
)
408409
} catch test:failure {
409-
(: when test:fail was called :)
410-
(: expected and actual can be of any type including functional ones :)
410+
(: when test:fail was called read expected and actual values from $err:value :)
411+
(: expected and actual values can be function types and need to be serialized :)
411412
let $serialized-expected := serialize($err:value?expected, map {"method": "adaptive"})
412413
let $serialized-actual := serialize($err:value?actual, map {"method": "adaptive"})
413414

exist-core/src/test/xquery/xqsuite/custom-assertion.xqm

Lines changed: 68 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -32,79 +32,119 @@ import module namespace test="http://exist-db.org/xquery/xqsuite"
3232
declare variable $ca:var := map {"a": 1, "b": 2};
3333

3434
declare
35-
%test:assertEquals("expected", "actual", "Custom message", "custom-assertion-failure")
35+
%test:assertEquals("Custom message", "expected", "actual", "custom-assertion-failure")
3636
function ca:test-fail-3() as item()* {
3737
try {
38-
test:fail("expected", "actual", "Custom message")
38+
test:fail("Custom message", "expected", "actual")
3939
}
4040
catch test:failure {
41-
$err:value?expected, $err:value?actual, $err:description, $err:value?type
41+
$err:description, $err:value?expected, $err:value?actual, $err:value?type
4242
}
4343
};
4444

4545
declare
46-
%test:assertEquals("expected", "actual", "Custom message", "custom-type")
46+
%test:assertEquals("Custom message", "expected", "actual", "custom-type")
4747
function ca:test-fail-4() as item()* {
4848
try {
49-
test:fail("expected", "actual", "Custom message", "custom-type")
49+
test:fail("Custom message", "expected", "actual", "custom-type")
5050
}
5151
catch test:failure {
52-
$err:value?expected, $err:value?actual, $err:description, $err:value?type
52+
$err:description, $err:value?expected, $err:value?actual, $err:value?type
5353
}
5454
};
5555

5656
declare
5757
%test:assertTrue
58-
function ca:pass() as item()* {
58+
function ca:map-assertion-pass() as item()* {
5959
ca:map-assertion($ca:var, map {"b": 2, "a": 1})
6060
};
6161

6262
declare
63-
%test:assertEquals("Key 'b' is missing", "map-assertion-failure")
64-
function ca:missing-key-default-type() as item()* {
63+
%test:assertEquals("Key 'b' is missing", "{""a"":1}", "map-assertion-failure")
64+
function ca:map-assertion-missing-key() as item()* {
6565
try {
66-
ca:map-assertion($ca:var, map {"a": 1, "c": 3})
66+
ca:map-assertion($ca:var, map {"a": 1})
6767
}
6868
catch test:failure {
69-
$err:description, $err:value?type
69+
$err:description,
70+
fn:serialize($err:value?actual, map{"method":"json"}),
71+
$err:value?type
7072
}
7173
};
7274

7375
declare
74-
%test:assertEquals("Value mismatch for key 'b'", "custom-assertion-failure")
75-
function ca:wrong-value-custom-type() as item()* {
76+
%test:assertEquals("Value mismatch for key 'b'", "{""a"":1,""b"":3}", "map-assertion-failure")
77+
function ca:map-assertion-wrong-value() as item()* {
7678
try {
7779
ca:map-assertion($ca:var, map {"a": 1, "b": 3})
7880
}
7981
catch test:failure {
80-
$err:description, $err:value?type
82+
$err:description,
83+
fn:serialize($err:value?actual, map{"method":"json"}),
84+
$err:value?type
8185
}
8286
};
8387

8488
declare
85-
%test:assertEquals("Type mismatch", "type-mismatch")
86-
function ca:type-mismatch-custom-type() as item()* {
89+
%test:assertEquals("Additional keys found: (23, o)", "{""a"":1,""23"":3,""o"":""o""}", "map-assertion-failure")
90+
function ca:map-assertion-additional-key() as item()* {
91+
try {
92+
ca:map-assertion($ca:var, map {"a": 1, 23: 3, "o": "o"})
93+
}
94+
catch test:failure {
95+
$err:description,
96+
fn:serialize($err:value?actual, map{"method":"json"}),
97+
$err:value?type
98+
}
99+
};
100+
101+
declare
102+
%test:assertEquals("Type mismatch", "[1,2]", "type-mismatch")
103+
function ca:map-assertion-type-mismatch() as item()* {
87104
try {
88105
ca:map-assertion($ca:var, [1,2])
89106
}
90107
catch test:failure {
91-
$err:description, $err:value?type
108+
$err:description,
109+
fn:serialize($err:value?actual, map{"method":"json"}),
110+
$err:value?type
92111
}
93112
};
94113

114+
(:
115+
: custom assertion, which could also be imported from a library module
116+
:)
117+
118+
declare %private variable $ca:MAP_ASSERTION_TYPE := "map-assertion-failure";
119+
95120
declare %private
96121
function ca:map-assertion ($expected as map(*), $actual as item()*) as item()* {
97-
if (exists($actual) and count($actual) eq 1 and $actual instance of map(*))
98-
then (
99-
for-each(map:keys($expected), function ($key as xs:anyAtomicType) {
100-
if (not(map:contains($actual, $key)))
101-
then test:fail($expected, $actual, "Key '" || $key || "' is missing", "map-assertion-failure")
102-
else if ($expected($key) ne $actual($key))
103-
then test:fail($expected, $actual, "Value mismatch for key '" || $key || "'")
104-
else ()
105-
})
106-
,
122+
if (not(exists($actual)))
123+
then test:fail("Actual is empty", $expected, $actual, "type-mismatch")
124+
else if (count($actual) gt 1)
125+
then test:fail("Actual is a sequence with more than one item", $expected, $actual, "type-mismatch")
126+
else if (not($actual instance of map(*)))
127+
then test:fail("Type mismatch", $expected, $actual, "type-mismatch")
128+
else if (not(empty(
129+
map:keys(map:remove($actual, map:keys($expected))))))
130+
then test:fail(
131+
"Additional keys found: (" || string-join(
132+
map:keys(map:remove($actual, map:keys($expected))), ', ') || ")",
133+
$expected,
134+
$actual,
135+
$ca:MAP_ASSERTION_TYPE
136+
)
137+
else (
138+
for-each(map:keys($expected), ca:map-assert-key(?, $expected, $actual)),
107139
true()
108140
)
109-
else test:fail($expected, $actual, "Type mismatch", "type-mismatch")
141+
};
142+
143+
declare %private
144+
function ca:map-assert-key ($key as xs:anyAtomicType, $expected as map(*), $actual as map(*)) as item()* {
145+
if (not(map:contains($actual, $key)))
146+
then test:fail("Key '" || $key || "' is missing", $expected, $actual, $ca:MAP_ASSERTION_TYPE)
147+
else if ($expected($key) ne $actual($key))
148+
then test:fail("Value mismatch for key '" || $key || "'", $expected, $actual, $ca:MAP_ASSERTION_TYPE)
149+
else ()
110150
};

0 commit comments

Comments
 (0)