@@ -32,79 +32,119 @@ import module namespace test="http://exist-db.org/xquery/xqsuite"
3232declare variable $ca:var := map {"a" : 1 , "b" : 2 };
3333
3434declare
35- %test:assertEquals("expected " , "actual " , "Custom message " , "custom-assertion-failure" )
35+ %test:assertEquals("Custom message " , "expected " , "actual " , "custom-assertion-failure" )
3636function 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
4545declare
46- %test:assertEquals("expected " , "actual " , "Custom message " , "custom-type" )
46+ %test:assertEquals("Custom message " , "expected " , "actual " , "custom-type" )
4747function 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
5656declare
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
6262declare
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
7375declare
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
8488declare
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+
95120declare %private
96121function 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