Skip to content

Commit

Permalink
feat: generate mismatch diffs for XML attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
Ronald Holshausen committed Aug 11, 2020
1 parent 7c1bf90 commit afbb2e1
Showing 1 changed file with 22 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -222,16 +222,16 @@ object XmlBodyMatcher : BodyMatcher, KLogging() {
listOf(BodyItemMatchResult(path.joinToString("."), listOf(BodyMismatch(expected, actual,
"Expected a Tag with at least ${expectedAttrs.size} attributes but " +
"received ${actual.attributes.length} attributes",
path.joinToString(".")))))
path.joinToString("."), generateAttrDiff(expected, actual)))))
} else {
val mismatches = if (expectedAttrs.size > actualAttrs.size) {
listOf(BodyMismatch(expected, actual, "Expected a Tag with at least ${expected.attributes.length} " +
"attributes but received ${actual.attributes.length} attributes",
path.joinToString(".")))
path.joinToString("."), generateAttrDiff(expected, actual)))
} else if (!allowUnexpectedKeys && expectedAttrs.size != actualAttrs.size) {
listOf(BodyMismatch(expected, actual, "Expected a Tag with ${expected.attributes.length} " +
"attributes but received ${actual.attributes.length} attributes",
path.joinToString(".")))
path.joinToString("."), generateAttrDiff(expected, actual)))
} else {
emptyList()
}
Expand All @@ -248,18 +248,35 @@ object XmlBodyMatcher : BodyMatcher, KLogging() {
attr.value.nodeValue != actualVal?.nodeValue ->
listOf(BodyMismatch(expected, actual, "Expected ${attr.key}='${attr.value.nodeValue}' " +
"but received ${attr.key}='${actualVal?.nodeValue}'",
attrPath.joinToString(".")))
attrPath.joinToString("."), generateAttrDiff(expected, actual)))
else -> emptyList()
}
} else {
listOf(BodyMismatch(expected, actual, "Expected ${attr.key}='${attr.value.nodeValue}' " +
"but was missing",
appendAttribute(path, attr.key).joinToString(".")))
appendAttribute(path, attr.key).joinToString("."), generateAttrDiff(expected, actual)))
}
}))
}
}

private fun generateAttrDiff(expected: Node, actual: Node): String {
val expectedXml = generateXMLForNode(expected)
val actualXml = generateXMLForNode(actual)
return generateDiff(expectedXml, actualXml).joinToString(separator = "\n")
}

private fun generateXMLForNode(node: Node): String {
return if (node.hasAttributes()) {
val attr = attributesToMap(node.attributes).entries.sortedBy { it.key.toString() }.joinToString {
" @${it.key}=\"${it.value.nodeValue}\"\n"
}
"<${node.nodeName}\n$attr>"
} else {
"<${node.nodeName}>"
}
}

private fun attributesToMap(attributes: NamedNodeMap?): Map<QualifiedName, Node> {
return if (attributes == null) {
emptyMap()
Expand Down

0 comments on commit afbb2e1

Please sign in to comment.