ScalaTest:使用 ShouldMatcher 检查序列的内容

发布于 2024-09-18 16:25:07 字数 354 浏览 7 评论 0原文

在我的单元测试中,我想表达计算(结果)序列产生了预定义的结果值序列。但无需假设序列容器的实际实现类型。

我想清楚且不言自明地阐明我的意图。
如果我尝试使用 ScalaTest 的“ShouldMatchers”并编写

val Input22 = ...
calculation(Input22) should equal (Seq("x","u"))

...那么我就会遇到简单的等式的麻烦,因为 calculation(..) 可能会返回一个 ArrayBuffer,而 Seq("x","u") 是一个 List

In my unit test, I want to express that a computed (result) sequence yielded a predefined sequence of result values. But without assuming anything about the actual implementation type of the sequence container.

And I want to spell out my intent rather clear and self-explanatory.
If I try to use the "ShouldMatchers" of ScalaTest and write

val Input22 = ...
calculation(Input22) should equal (Seq("x","u"))

...then I get into trouble with the simple equality, because calculation(..) might return an ArrayBuffer, while Seq("x","u") is an List

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

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

发布评论

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

评论(2

掩耳倾听 2024-09-25 16:25:07

你用的是2.7.7吗?在 2.8 中,具有相等元素(相同顺序)的不同 Seq 应该相等:

scala> import org.scalatest.matchers.ShouldMatchers._
import org.scalatest.matchers.ShouldMatchers._

scala> import scala.collection.mutable.ArrayBuffer
import scala.collection.mutable.ArrayBuffer

scala> val list = List(1, 2, 3)
list: List[Int] = List(1, 2, 3)

scala> val arrayBuffer = ArrayBuffer(1, 2, 3)
arrayBuffer: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3)

scala> list == arrayBuffer
res2: Boolean = true

scala> arrayBuffer == list
res3: Boolean = true

scala> list should equal (arrayBuffer)

scala> arrayBuffer should equal (list)

2.8 中这一规则的一个例外是数组,它只能与其他数组相等,因为它们是 Java 数组。 (当您对 Java 数组调用 .equals 时,它们不会在结构上进行比较,但 ScalaTest 匹配器确实会对两个数组进行结构相等。)

Are you using 2.7.7? In 2.8 different Seq's with the equal elements (in the same order) should be equal:

scala> import org.scalatest.matchers.ShouldMatchers._
import org.scalatest.matchers.ShouldMatchers._

scala> import scala.collection.mutable.ArrayBuffer
import scala.collection.mutable.ArrayBuffer

scala> val list = List(1, 2, 3)
list: List[Int] = List(1, 2, 3)

scala> val arrayBuffer = ArrayBuffer(1, 2, 3)
arrayBuffer: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3)

scala> list == arrayBuffer
res2: Boolean = true

scala> arrayBuffer == list
res3: Boolean = true

scala> list should equal (arrayBuffer)

scala> arrayBuffer should equal (list)

The one exception to this rule in 2.8 is arrays, which can only be equal to other arrays, because they are Java arrays. (Java arrays are not compared structurally when you call .equals on them, but ScalaTest matchers does do structural equality on two arrays.)

So尛奶瓶 2024-09-25 16:25:07
import org.specs.matcher.IterableMatchers._
calculation(Input22) should beTheSameSeqAs (Seq("x","u"))
import org.specs.matcher.IterableMatchers._
calculation(Input22) should beTheSameSeqAs (Seq("x","u"))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文