How to use ScalaTest “contain allOf” on two lists?

前端 未结 3 1210
礼貌的吻别
礼貌的吻别 2021-01-17 13:45

I was looking for a ScalaTest matcher to check that a list contains all of the needed elements (given within another list), but that may also be others.

contai

3条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-17 14:11

    I don't think there's any good reason for this. You can remedy this with a pimp my class:

    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 :_*)
        }
    }
    

    You should probably make this account for Seqs with fewer than 2 elements (for which this would fail).

    Then, you can do:

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

提交回复
热议问题