beEqualToIgnoringSpace fails, if a XML text node contains an entity #161

Closed
heffer opened this Issue May 23, 2013 · 1 comment

Comments

Projects
None yet
2 participants
@heffer

heffer commented May 23, 2013

EqualsIgnoringSpaceMatcher seems to have a problem with entities inside XML tags. In the following test class I save an XML element to a file. After that, I read it and use the matcher to compare it to the initially created XML element. The matcher works fine for normal text nodes without entities (ex1) as well as entities inside attribute values (ex2). But it won't work for entities inside text nodes (ex3). In Specs2 1.14 it simply says that the both elements aren't equal and in version 2.0-RC1 it even throws an exception (see error output below).

package de.heffer

import java.io.File
import scala.xml._
import org.specs2.SpecificationWithJUnit
import org.junit.runner._
import org.specs2.runner.JUnitRunner
import org.specs2.matcher.XmlMatchers._

@RunWith(classOf[JUnitRunner])
class Specs2HtmlEntitiesInsideTagsSpec extends SpecificationWithJUnit { def is =
  "A XML element should be matched positive with the EqualsIgnoringSpaceMatcher " +
  "after saving to and loading from a file, if a XML tag contains"                      ^
    "only 'normal' text"                                                          ! ex1 ^
    "a HTML entity inside an attribute value"                                     ! ex2 ^
    "a HTML entity inside the text content of an XML tag"                         ! ex3 ^
                                                                                      end

  def ex1 = {
    val xmlWithoutEntities = <xml>only text without entities</xml>
    xmlWithoutEntities should beEqualToIgnoringSpace(saveAndLoadXML(xmlWithoutEntities))
  }

  def ex2 = {
    val xmlWithEntityInsideAttribute = <xml entity="&amp;">normal text</xml>
    xmlWithEntityInsideAttribute should beEqualToIgnoringSpace(saveAndLoadXML(xmlWithEntityInsideAttribute))
  }

  def ex3 = {
    val xmlWithEntityInsideTextContent = <xml>some &amp; text</xml>
    xmlWithEntityInsideTextContent.toString should beEqualTo(saveAndLoadXML(xmlWithEntityInsideTextContent).toString)
    xmlWithEntityInsideTextContent should beEqualTo(saveAndLoadXML(xmlWithEntityInsideTextContent))

    xmlWithEntityInsideTextContent should beEqualToIgnoringSpace(saveAndLoadXML(xmlWithEntityInsideTextContent))
  }

  private def saveAndLoadXML(xml: Elem): Elem = {
    val tempFile = File.createTempFile("some", ".xml")
    tempFile deleteOnExit

    XML.save(tempFile getAbsolutePath, xml, "utf-8")
    XML loadFile tempFile
  }
}

Error output from test run with Specs2 1.14

[info] Specs2HtmlEntitiesInsideTagsSpec
[info]
[info] A XML element should be matched positive with the EqualsIgnoringSpaceMatcher after saving to and loading from a file, if a XML tag contains
[info] + only 'normal' text
[info] + a HTML entity inside an attribute value
[error] x a HTML entity inside the text content of an XML tag
[error] some & text is not equal to some & text (Specs2HtmlEntitiesInsideTagsSpec.scala:34)
[info]
[info] Total for specification Specs2HtmlEntitiesInsideTagsSpec
[info] Finished in 13 ms
[info] 3 examples, 1 failure, 0 error

Error output from test run with Specs2 2.0-RC1

[info] Specs2HtmlEntitiesInsideTagsSpec
[info]
[info] A XML element should be matched positive with the EqualsIgnoringSpaceMatcher after saving to and loading from a file, if a XML tag contains
[info] + only 'normal' text
[info] + an HTML entity inside an attribute value
[info] ! an HTML entity inside the text content of an XML tag
[error] ThrowableException: null (Promise.scala:125)
[error] null
[info]
[info] Total for specification Specs2HtmlEntitiesInsideTagsSpec
[info] Finished in 14 ms
[info] 3 examples, 0 failure, 1 error

@etorreborre

This comment has been minimized.

Show comment
Hide comment
@etorreborre

etorreborre May 24, 2013

Owner

This is a tricky question. I fixed the NPE in 2.0-RC2-SNAPSHOT but that doesn't really solve your problem.

Now you will get a message like this:

 <xml>some &amp; text</xml> is not equal to <xml>some &amp; text</xml>

The nodes have the same representation but contain different elements like <n>{"a"} b</n> (which is <n>Text("a") b</n>) and <n>a b</n>

This is because when you save xml and load it back you are not guaranteed to get the exact same structure. As method in the message this is also the case when you do xml interpolation.

Owner

etorreborre commented May 24, 2013

This is a tricky question. I fixed the NPE in 2.0-RC2-SNAPSHOT but that doesn't really solve your problem.

Now you will get a message like this:

 <xml>some &amp; text</xml> is not equal to <xml>some &amp; text</xml>

The nodes have the same representation but contain different elements like <n>{"a"} b</n> (which is <n>Text("a") b</n>) and <n>a b</n>

This is because when you save xml and load it back you are not guaranteed to get the exact same structure. As method in the message this is also the case when you do xml interpolation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment