如何使用ScalaTest"包含allOf"在两个名单上?

时间:2014-11-07 12:52:57

标签: scala scalatest

我正在寻找一个ScalaTest匹配器来检查列表是否包含所有需要的元素(在另一个列表中给出),但也可能是其他元素。

contain allOf要求获得两个固定元素,出于某种原因,其余部分为varargs。

我可以做这样的解决方法,但这非常难看:

val list = List(1,2,3,4)
val wanted = List(1,2,3)
list should contain allOf ( wanted.head, wanted.tail.head, wanted.tail.tail  :_* )    // ugly workaround

为了给列表作为匹配,有contain theSameElementsAs。但是,它不允许无关元素出现在探测值中(我认为)。

所以:

  • 我错过了什么吗?
  • 为什么allOf声明必须在前面给出两个固定元素(即为什么不通过varargs?)
  • 应该有theSameElementsAndMaybeMoreThan方法(大概有更好的名字)?

我尝试过的一些代码:

val list = List.empty[String]
//list should contain allOf("a")         // does not compile
list should contain allOf("a","b")
list should contain allOf("a","b","c")

val wanted = List("a","b","c")
//list should contain allOf( wanted )    // does not compile
list should contain allOf( wanted.head, wanted.tail )   // compiles, but tests the wrong thing; against List(head,List(tail))

documentation

Scala 2.11.4,ScalaTest 2.2.1

修改

我可能最终会使用类似的东西:

wanted.foreach( list should contain(_) )

然而,这对我来说似乎不可读(should是嵌入式的)作为内置集合构造。

3 个答案:

答案 0 :(得分:4)

Bill Venners在ScalaTest邮件列表上有这个说法:

  

是的,我们并不想阻止2.0版本添加,但有   自从添加它。我相信我们把它添加到掌握,但不是2.2.x   科。无论如何,语法如下:

xSet should contain allElementsOf (ySet)

链接到message

答案 1 :(得分:2)

我认为没有任何充分的理由。你可以通过我的班级皮条客来解决这个问题:

object ScalaTestUtils {
    import org.scalatest.words.ResultOfContainWord

    implicit class ResultOfContainWordImprovements[T](val contains: ResultOfContainWord[Seq[T]]) {
        def allOf(right: Seq[T]) = contains allOf(right.head, right.tail.head, right.tail.tail :_*)
    }
}

您应该为Seqs设置少于2个元素的帐户(这样会失败)。

然后,你可以这样做:

import ScalaTestUtils._
Seq(1, 2, 3) should contain allOf Seq(1, 2)

答案 2 :(得分:1)

另一种可能的解决方案是:

import org.scalatest.Inspectors.forAll

forAll(list) { wanted should contain(_) }

预计会出现与此类似的错误消息:

scala> forAll(List(1, 2)) { List(1) should contain(_) }
org.scalatest.exceptions.TestFailedException: forAll failed, because:
  at index 1, List(1) did not contain element 2 (<console>:18)
in List(1, 2)
...
Caused by: org.scalatest.exceptions.TestFailedException: List(1) did not contain element 2