Skip to content

Instantly share code, notes, and snippets.

@eouthwaite
Created January 21, 2020 22:04
Show Gist options
  • Save eouthwaite/d7eaf5273d0d921e886306ea911f655f to your computer and use it in GitHub Desktop.
Save eouthwaite/d7eaf5273d0d921e886306ea911f655f to your computer and use it in GitHub Desktop.
local assert-equal-xml function
declare function local:assert-equal-xml($expected, $actual) {
typeswitch ($actual)
case document-node() return
typeswitch ($expected)
case document-node() return
local:assert-equal-xml($expected/node(), $actual/node())
default return
local:assert-equal-xml($expected, $actual/node())
case element() return
if (fn:empty($expected)) then
test:assert-true(fn:false(), ("element not found in $expected : ", xdmp:path($actual)))
else typeswitch ($expected)
case element() return (
test:assert-equal(fn:name($expected), fn:name($actual), ("mismatched node name ($expected=", xdmp:path($expected), ", $actual=", xdmp:path($actual), ")")),
test:assert-equal(fn:count($expected/@*), fn:count($actual/@*), ("mismatched attribute count ($expected=", xdmp:path($expected), ", $actual=", xdmp:path($actual), ")")),
for $attribute in $actual/@* return
local:assert-equal-xml($expected/@*[fn:name(.) = fn:name($attribute)], $attribute),
for $text at $i in $actual/text() return
test:assert-equal(fn:normalize-space($expected/text()[$i]), fn:normalize-space($text), ("mismatched element text ($expected=", xdmp:path($expected), ", $actual=", xdmp:path($actual), ")")),
test:assert-equal(fn:count($expected/*), fn:count($actual/*), ("mismatched element count ($expected=", xdmp:path($expected), ", $actual=", xdmp:path($actual), ")")),
for $element at $i in $actual/* return
local:assert-equal-xml($expected/*[$i], $element)
)
default return
test:assert-true(fn:false(), ("type mismatch ($expected=", xdmp:path($expected), ", $actual=", xdmp:path($actual), ")"))
case attribute() return
if (fn:empty($expected)) then
test:assert-true(fn:false(), ("attribute not found in $expected : ", xdmp:path($actual)))
else typeswitch ($expected)
case attribute() return (
test:assert-equal(fn:name($expected), fn:name($actual), ("mismatched attribute name ($expected=", xdmp:path($expected), ", $actual=", xdmp:path($actual), ")")),
test:assert-equal($expected/fn:data(), $actual/fn:data(), ("mismatched attribute text ($expected=", xdmp:path($expected), ", $actual=", xdmp:path($actual), ")"))
)
default return
test:assert-true(fn:false(), ("type mismatch : $expected=", xdmp:path($expected), ", $actual=", xdmp:path($actual)))
default return
test:assert-true(fn:false(), ("unsupported type in $actual : ", xdmp:path($actual)))
};
@eouthwaite
Copy link
Author

this is the function taken from the Roxy test helper module and converted to help debug older tests where test:assert-equal (fn:deep-equal) against two bits of xml just plain fails

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment