I want to write a test case for a case class
, that has a toXML
method.
import java.net.URI
case class Person(
label: String = "author",
name: String,
email: Option[String] = None,
uri: Option[URI] = None) {
// author must be either "author" or "contributor"
assert(label == "author" ||
label == "contributor")
def toXML = {
val res =
<author>
<name>{ name }</name>
{
email match {
case Some(email) => <email>{ email }</email>
case None => Null
}
}
{
uri match {
case Some(uri) => <uri>{ uri }</uri>
case None => Null
}
}
</author>
label match {
case "author" => res
case _ => res.copy(label = label) // rename element
}
}
}
现在我要说的是,产出是正确的。 The for I use scala.xml.Utility.trim
import scala.xml.Utility.trim
val p1 = Person("author", "John Doe", Some("[email protected]"),
Some(new URI("http://example.com/john")))
val p2 =
<author>
<name>John Doe</name>
<email>[email protected]</name>
<uri>http://example.com/john</uri>
</author>
assert(trim(p1.toXML) == trim(p2))
But this will cause an assertion error. If I try to assert equality by comparing the String representations
assert(trim(p1.toXML).toString == trim(p2).toString)
没有任何说法错误。
What am I doing wrong?