我想为 a 编写一个测试用例case class
,它有一个toXML
方法。
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
}
}
}
现在,我想断言,输出是正确的。为此我使用scala.xml.Utility.trim
import scala.xml.Utility.trim
val p1 = Person("author", "John Doe", Some("jd@example.com"),
Some(new URI("http://example.com/john")))
val p2 =
<author>
<name>John Doe</name>
<email>jd@example.com</name>
<uri>http://example.com/john</uri>
</author>
assert(trim(p1.toXML) == trim(p2))
但这会导致断言错误。如果我尝试通过比较字符串表示来断言相等
assert(trim(p1.toXML).toString == trim(p2).toString)
没有断言错误。
我究竟做错了什么?