Skip to content

Commit bd2a9ee

Browse files
committed
Handle scala-xml quirk where some text nodes are Atoms
1 parent 5789d71 commit bd2a9ee

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

xml-compare/src/main/scala/software/purpledragon/xml/compare/XmlCompare.scala

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ package software.purpledragon.xml.compare
1919
import software.purpledragon.xml.compare.options.DiffOption._
2020
import software.purpledragon.xml.compare.options.DiffOptions
2121

22-
import scala.xml.{Node, Text}
22+
import scala.xml.{Atom, Node}
2323

2424
/**
2525
* Utility for comparing XML documents.
@@ -79,8 +79,9 @@ object XmlCompare {
7979
}
8080

8181
private def compareText(left: Node, right: Node, options: DiffOptions, path: Seq[String]): XmlDiff = {
82-
val leftText = left.child.collect({ case t: Text => t }).map(_.text.trim).mkString
83-
val rightText = right.child.collect({ case t: Text => t }).map(_.text.trim).mkString
82+
def extractText(node: Node): String = node.child.collect({ case a: Atom[_] => a }).map(_.text.trim).mkString
83+
val leftText = extractText(left)
84+
val rightText = extractText(right)
8485

8586
if (leftText != rightText) {
8687
XmlDiffers("different text", leftText, rightText, extendPath(path, left))
@@ -90,8 +91,8 @@ object XmlCompare {
9091
}
9192

9293
private def compareChildren(left: Node, right: Node, options: DiffOptions, path: Seq[String]): XmlDiff = {
93-
val leftChildren = left.child.filterNot(_.isInstanceOf[Text])
94-
val rightChildren = right.child.filterNot(_.isInstanceOf[Text])
94+
val leftChildren = left.child.filterNot(c => c.isInstanceOf[Atom[_]])
95+
val rightChildren = right.child.filterNot(c => c.isInstanceOf[Atom[_]])
9596

9697
if (leftChildren.size != rightChildren.size) {
9798
XmlDiffers("different child count", leftChildren.size, rightChildren.size, extendPath(path, left))

xml-compare/src/test/scala/software/purpledragon/xml/compare/XmlCompareSpec.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ class XmlCompareSpec extends FlatSpec with Matchers {
6262
XmlCompare.compare(<test>contents </test>, <test>contents</test>) shouldBe XmlEqual
6363
}
6464

65+
it should "match with text content from a variable" in {
66+
val content = "contents"
67+
XmlCompare.compare(<test>contents</test>, <test>{content}</test>) shouldBe XmlEqual
68+
}
69+
6570
it should "match with child & text content in different order" in {
6671
XmlCompare.compare(<test><test2/>contents</test>, <test>contents<test2/></test>) shouldBe XmlEqual
6772
}

0 commit comments

Comments
 (0)