English 中文(简体)
平等问题
原标题:Scala XML equality issues

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?

最佳回答

首先,你们的法典有一处错误:uri: 备选办法[URI](我假定是java.net)。 URI作为构造理由,但随后,你向施工者发出<条码>[标准]

This is also the source of your problem:

scala> val uri = "http://stackoverflow.com/q/10676812/334519"
uri: java.lang.String = http://stackoverflow.com/q/10676812/334519

scala> <u>{new java.net.URI(uri)}</u> == <u>{uri}</u>
res0: Boolean = false

scala> <u>{new java.net.URI(uri)}</u>.toString == <u>{uri}</u>.toString
res1: Boolean = true

What s happening is that the element s child is an Atom, which has a type parameter for the data value it carries. In your p1 this is a URI (assuming we ve corrected the test so that it matches the constructor), and in p2 it s a String.

问题回答

暂无回答




相关问题
how to represent it in dtd?

I have two element action and guid. guid is a required field when action is add. but when action is del it will not appear in file. How to represent this in dtd ?

.Net application configuration add xml-data

I need to add xml-content to my application configuration file. Is there a way to add it directly to the appSettings section or do I need to implement a configSection? Is it possible to add the xml ...

XStream serializing collections

I have a class structure that I would like to serialize with Xstream. The root class contains a collection of other objects (of varying types). I would like to only serialize part of the objects that ...

MS Word splits words in its XML format

I have a Word 2003 document saved as a XML in WordProcessingML format. It contains few placeholders which will be dynamically replaced by an appropriate content. But, the problem is that Word ...

Merging an XML file with a list of changes

I have two XML files that are generated by another application I have no control over. The first is a settings file, and the second is a list of changes that should be applied to the first. Main ...

How do I check if a node has no siblings?

I have a org.w3c.dom.Node object. I would like to see if it has any other siblings. Here s what I have tried: Node sibling = node.getNextSibling(); if(sibling == null) return true; else ...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

热门标签