@@ -32,79 +32,137 @@ 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" )
36- function ca:test-fail-3 () as item ()* {
35+ %test:assertEquals("A custom assertion failed " , "custom-assertion-failure" , "true" , "false " )
36+ function ca:test-fail-0 () as item ()* {
3737 try {
38- test:fail ("expected" , "actual" , "Custom message" )
38+ test:fail ()
3939 }
4040 catch test:failure {
41- $err:value?expected , $err:value?actual , $err:description , $err:value?type
41+ $err:description , $err:value?type , $err:value?expected , $err:value?actual
4242 }
4343};
4444
4545declare
46- %test:assertEquals("expected" , "actual" , "Custom message" , "custom-type" )
46+ %test:assertEquals("The custom assertion of type "" custom-type"" failed" , "custom-type" , "true" , "false" )
47+ function ca:test-fail-1 () as item ()* {
48+ try {
49+ test:fail ("custom-type" )
50+ }
51+ catch test:failure {
52+ $err:description, $err:value?type , $err:value?expected, $err:value?actual
53+ }
54+ };
55+
56+ declare
57+ %test:assertEquals("Custom message" , "custom-type" , "true" , "false" )
58+ function ca:test-fail-2 () as item ()* {
59+ try {
60+ test:fail ("custom-type" , "Custom message" )
61+ }
62+ catch test:failure {
63+ $err:description, $err:value?type , $err:value?expected, $err:value?actual
64+ }
65+ };
66+
67+ declare
68+ %test:assertEquals("Custom message" , "custom-type" , "expected" , "actual" )
4769function ca:test-fail-4 () as item ()* {
4870 try {
49- test:fail ("expected " , "actual " , "Custom message " , "custom-type " )
71+ test:fail ("custom-type " , "Custom message " , "expected " , "actual " )
5072 }
5173 catch test:failure {
52- $err:value?expected , $err:value?actual , $err:description , $err:value?type
74+ $err:description , $err:value?type , $err:value?expected , $err:value?actual
5375 }
5476};
5577
5678declare
5779 %test:assertTrue
58- function ca:pass () as item ()* {
80+ function ca:map-assertion- pass () as item ()* {
5981 ca:map-assertion ($ca:var, map {"b" : 2 , "a" : 1 })
6082};
6183
6284declare
63- %test:assertEquals("Key 'b' is missing" , "map-assertion-failure" )
64- function ca:missing-key-default-type () as item ()* {
85+ %test:assertEquals("Key 'b' is missing" , "map-assertion-failure" , "{ "" a "" :1}" )
86+ function ca:map-assertion-missing-key () as item ()* {
6587 try {
66- ca:map-assertion ($ca:var, map {"a" : 1 , "c" : 3 })
88+ ca:map-assertion ($ca:var, map {"a" : 1 })
6789 }
6890 catch test:failure {
69- $err:description, $err:value?type
91+ $err:description, $err:value?type ,
92+ fn:serialize ($err:value?actual, map{"method" :"json" })
7093 }
7194};
7295
7396declare
74- %test:assertEquals("Value mismatch for key 'b'" , "custom -assertion-failure" )
75- function ca:wrong-value-custom-type () as item ()* {
97+ %test:assertEquals("Value mismatch for key 'b'" , "map -assertion-failure" , "{ "" a "" :1, "" b "" :3} " )
98+ function ca:map-assertion-wrong-value () as item ()* {
7699 try {
77100 ca:map-assertion ($ca:var, map {"a" : 1 , "b" : 3 })
78101 }
79102 catch test:failure {
80- $err:description, $err:value?type
103+ $err:description, $err:value?type ,
104+ fn:serialize ($err:value?actual, map{"method" :"json" })
81105 }
82106};
83107
84108declare
85- %test:assertEquals("Type mismatch" , "type-mismatch" )
86- function ca:type-mismatch-custom-type () as item ()* {
109+ %test:assertEquals("Additional keys found: (23, o)" , "map-assertion-failure" , "{"" a"" :1,"" 23"" :3,"" o"" :"" o"" }" )
110+ function ca:map-assertion-additional-key () as item ()* {
111+ try {
112+ ca:map-assertion ($ca:var, map {"a" : 1 , 23 : 3 , "o" : "o" })
113+ }
114+ catch test:failure {
115+ $err:description, $err:value?type ,
116+ fn:serialize ($err:value?actual, map{"method" :"json" })
117+ }
118+ };
119+
120+ declare
121+ %test:assertEquals("Type mismatch" , "type-mismatch" , "[1,2]" )
122+ function ca:map-assertion-type-mismatch () as item ()* {
87123 try {
88124 ca:map-assertion ($ca:var, [1 ,2 ])
89125 }
90126 catch test:failure {
91- $err:description, $err:value?type
127+ $err:description, $err:value?type ,
128+ fn:serialize ($err:value?actual, map{"method" :"json" })
92129 }
93130};
94131
132+ (:
133+ : custom assertion, which could also be imported from a library module
134+ :)
135+
136+ declare %private variable $ca:MAP_ASSERTION_TYPE := "map-assertion-failure" ;
137+
95138declare %private
96139function 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- ,
140+ if (not (exists ($actual)))
141+ then test:fail ("type-mismatch" , "Actual is empty" )
142+ else if (count ($actual) gt 1 )
143+ then test:fail ("type-mismatch" , "Actual is a sequence with more than one item" , $expected, $actual)
144+ else if (not ($actual instance of map (*)))
145+ then test:fail ("type-mismatch" , "Type mismatch" , $expected, $actual)
146+ else if (not (empty (
147+ map:keys (map:remove ($actual, map:keys ($expected))))))
148+ then test:fail (
149+ $ca:MAP_ASSERTION_TYPE,
150+ "Additional keys found: (" || string-join (
151+ map:keys (map:remove ($actual, map:keys ($expected))), ', ' ) || ")" ,
152+ $expected,
153+ $actual
154+ )
155+ else (
156+ for-each (map:keys ($expected), ca:map-assert-key (?, $expected, $actual)),
107157 true ()
108158 )
109- else test:fail ($expected, $actual, "Type mismatch" , "type-mismatch" )
159+ };
160+
161+ declare %private
162+ function ca:map-assert-key ($key as xs:anyAtomicType, $expected as map (*), $actual as map (*)) as item ()* {
163+ if (not (map:contains ($actual, $key)))
164+ then test:fail ($ca:MAP_ASSERTION_TYPE, "Key '" || $key || "' is missing" , $expected, $actual)
165+ else if ($expected($key) ne $actual($key))
166+ then test:fail ($ca:MAP_ASSERTION_TYPE, "Value mismatch for key '" || $key || "'" , $expected, $actual)
167+ else ()
110168};
0 commit comments