I have been working with some XML data and have been trying to write a test that tries to match that the direct children of two xml trees have all the same nodes of type bar
.
I started off using a simple test to ensure that both data structures match.
The assertion matching them as strings works:
result.toString should be(expected.toString)
but the assertion matching each individual element fails:
result should contain allElementsOf expected
which I found on this answer here
because these simpler assertion did not work:
result should be(expected)
result should contain(expected(1))
The test sets up the XML using before
block in the test class:
var foo: Elem = _
before {
foo = <foo>
<bar type="greet" style="pleasent" target="anyone">hi</bar>
<bar type="count">1</bar>
<bar type="color">yellow</bar>
<baz>
<bar type="bad">Bad</bar>
</baz>
</foo>
}
This is the code of the test:
it should "allow use of map and filter" in {
import scala.collection.mutable.ArrayBuffer
val expected = ArrayBuffer(
(
"bar",
Map(
"type" -> "greet",
"style" -> "pleasent",
"target" -> "anyone"
),
"hi"
),
(
"bar",
Map("type" -> "count"),
"1"
),
(
"bar",
Map("type" -> "color"),
"yellow"
)
)
val result = foo.child.map(x => {
x match {
case <bar>{text}</bar> => (x.label, x.attributes.asAttrMap, text)
case <baz>_</baz> => (x.label, Map[String,String](), "")
case _ => ("", Map[String,String](), "")
}
})
.filter(x => {
x match {
case ("bar", _, _) => true
case _ => false
}
})
result.toString should be(expected.toString) // works
// result should contain allElementsOf expected // fails
}