!= b c测试中的行为" />

避免 Scala XML 的 b {"c"} != b c测试中的行为

发布于 2024-12-16 22:58:48 字数 636 浏览 0 评论 0 原文

我正在使用 scalatest,特别想说

actualXML should be === expectedXML

的是 === 不关心属性顺序。然而,当使用 Scala XML 的 { ... } 语法嵌入文本时,断言会失败,因为

scala> <a>b {"c"}</a>.child
res8: scala.xml.Node* = ArrayBuffer(b , c)

scala> <a>b c</a>.child
res9: scala.xml.Node* = ArrayBuffer(b c)

我可以编写一个

import scala.xml.Elem
import scala.xml.XML
def launder(xml: Elem): Elem = XML.loadString(xml.toString)

给出的

launder(actualXML) should be === expectedXML

方法,但希望能够使用普通语法。

I'm using scalatest and want to say

actualXML should be === expectedXML

especially as === doesn't care about attribute order. However the assertion fails when text has been embedded using Scala XML's { ... } syntax because

scala> <a>b {"c"}</a>.child
res8: scala.xml.Node* = ArrayBuffer(b , c)

whereas:

scala> <a>b c</a>.child
res9: scala.xml.Node* = ArrayBuffer(b c)

I can write a method

import scala.xml.Elem
import scala.xml.XML
def launder(xml: Elem): Elem = XML.loadString(xml.toString)

giving

launder(actualXML) should be === expectedXML

but would like to be able to use the vanilla syntax.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一页 2024-12-23 22:58:48

您可以专门为 Xml Elem 引入自己的 Equalizer 类:

class ElemEqualizer(left: scala.xml.Elem) {
  def ===(right: Any) = new Equalizer(launder(left).===(launder(right))
}
implicit def convertToElemEqualizer(left: scala.xml.Elem) = new ElemEqualizer(left)

@Test def foobar(): Unit = {
  val a = <a>b {"c"}</a>
  val b = <a>b c</a>

  assert(a === b)
}

因此,您将引入另一个隐式转换,但这次专门针对 Elem,它比较经过清洗的 Elem。

You can introduce your own Equalizer class specifically for Xml Elem:

class ElemEqualizer(left: scala.xml.Elem) {
  def ===(right: Any) = new Equalizer(launder(left).===(launder(right))
}
implicit def convertToElemEqualizer(left: scala.xml.Elem) = new ElemEqualizer(left)

@Test def foobar(): Unit = {
  val a = <a>b {"c"}</a>
  val b = <a>b c</a>

  assert(a === b)
}

So you're introducing another implicit conversion but this time specifically for Elem, which compares the laundered Elems.

段念尘 2024-12-23 22:58:48

请参阅上面的答案 - 我现在更好地了解该地区。下面是修正了括号的代码以及基于 Matthew 和 Dave 评论的测试。

我对它的尺寸表示歉意:

// Tested with Scala 2.9.1

import org.junit.Test
import org.scalatest.Assertions._
import org.scalatest.TestFailedException
import scala.xml.Elem
import scala.xml.Node
import scala.xml.XML
import scala.xml.Utility

class TestEquals {

  val a = <a>b {"c"}</a>
  val b = <a>b c</a>

  @Test def defaultBehaviour {
    assert(a === a)
    assert(b === b)

    // _but_ this fails
    intercept[TestFailedException] { assert(a === b) } // i.e. a != b
  }

  // Add class & implicit to improve the behaviour
  class ElemEqualizer(left: Any) {
    private def launder(x: Any): Node = Utility.trim(x match {
      case xml: String => XML.loadString(xml)
      case xml: Elem => XML.loadString(xml.toString)
    })

    def ===(right: Any): Option[String] =
      new Equalizer(launder(left)).===(launder(right))
  }

  implicit def convertToElemEqualizer(left: Elem) = new ElemEqualizer(left)

  // Retest the behaviour with ElemEqualizer available
  @Test def betterBehaviour {
    assert(a === b)
    assert(a === "<a>b c</a>")  // works for 'right' string that's XML - see launder

    intercept[TestFailedException]
      { assert("<a>b c</a>" === a) } // but not this way round

    // attribute order doesn't matter
    assert(<a attr0="123" attr1="456"/> === <a attr1="456" attr0="123"/>)

    // fails if attribute values don't match - that's good
    intercept[TestFailedException]
      { assert(<a attr0="123" attr1="456"/> === <a attr1="456" attr0="xxx"/>) }

    // and again with 'right' as a string
    intercept[TestFailedException]
      { assert(<a attr0="123" attr1="456"/> === XML.loadString("<a attr1='456' attr0='xxx'/>")) }
  }

  @Test def xmlContainingText {
    // Here left and right wouldn't be equal without use of Utility.trim()
    val left = <a>
 <b>text</b>
</a>
    val right = <a><b>text</b></a>
    assert(left === right)
  }

}

See answer above - I now understand the area better. Here's the code with brackets corrected and a test that builds on Matthew and Dave's comments.

My apologies for it's size:

// Tested with Scala 2.9.1

import org.junit.Test
import org.scalatest.Assertions._
import org.scalatest.TestFailedException
import scala.xml.Elem
import scala.xml.Node
import scala.xml.XML
import scala.xml.Utility

class TestEquals {

  val a = <a>b {"c"}</a>
  val b = <a>b c</a>

  @Test def defaultBehaviour {
    assert(a === a)
    assert(b === b)

    // _but_ this fails
    intercept[TestFailedException] { assert(a === b) } // i.e. a != b
  }

  // Add class & implicit to improve the behaviour
  class ElemEqualizer(left: Any) {
    private def launder(x: Any): Node = Utility.trim(x match {
      case xml: String => XML.loadString(xml)
      case xml: Elem => XML.loadString(xml.toString)
    })

    def ===(right: Any): Option[String] =
      new Equalizer(launder(left)).===(launder(right))
  }

  implicit def convertToElemEqualizer(left: Elem) = new ElemEqualizer(left)

  // Retest the behaviour with ElemEqualizer available
  @Test def betterBehaviour {
    assert(a === b)
    assert(a === "<a>b c</a>")  // works for 'right' string that's XML - see launder

    intercept[TestFailedException]
      { assert("<a>b c</a>" === a) } // but not this way round

    // attribute order doesn't matter
    assert(<a attr0="123" attr1="456"/> === <a attr1="456" attr0="123"/>)

    // fails if attribute values don't match - that's good
    intercept[TestFailedException]
      { assert(<a attr0="123" attr1="456"/> === <a attr1="456" attr0="xxx"/>) }

    // and again with 'right' as a string
    intercept[TestFailedException]
      { assert(<a attr0="123" attr1="456"/> === XML.loadString("<a attr1='456' attr0='xxx'/>")) }
  }

  @Test def xmlContainingText {
    // Here left and right wouldn't be equal without use of Utility.trim()
    val left = <a>
 <b>text</b>
</a>
    val right = <a><b>text</b></a>
    assert(left === right)
  }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文