比较集合内容与 ScalaTest

问题描述:

我正在尝试对一些需要大量收集的 Scala 进行单元测试.这些集合作为 Iterable[T] 返回,所以我对集合的 内容 感兴趣,即使基础类型不同.这实际上是两个相关的问题:

I'm trying to unit-test some Scala that is very collection-heavy. These collections are returned as Iterable[T], so I am interested in the contents of the collection, even if the underlying types differ. This is actually two related problems:

  1. 如何断言两个有序集合包含相同的元素序列?
  2. 如何断言两个无序集合包含相同的元素集?
  1. How do I assert that two ordered collections contain the same sequence of elements?
  2. How do I assert that two unordered collections contain the same set of elements?

总而言之,我正在 ScalaTest 中寻找 NUnit 的 CollectionAssert.AreEqual(有序)和 CollectionAssert.AreEquivalent(无序)的 Scala 等效项:

In summary, I'm looking the Scala-equivalent of NUnit's CollectionAssert.AreEqual (ordered) and CollectionAssert.AreEquivalent (unordered) in ScalaTest:

Set(1, 2) should equal (List(1, 2))          // ordered, pass
Iterable(2, 1) should equal (Iterable(1, 2)) // unordered, pass

您可以尝试 .toSeq 用于有序集合和 .toSet 用于无序集合,它可以捕获您想要的内容据我了解.

You could try .toSeq for ordered collections and .toSet for unordered, which captures what you want as far as I understand it.

以下通过:

class Temp extends FunSuite with ShouldMatchers {
  test("1")  { Array(1, 2).toSeq should equal (List(1, 2).toSeq) }
  test("2")  { Array(2, 1).toSeq should not equal (List(1, 2).toSeq) }
  test("2b") { Array(2, 1) should not equal (List(1, 2)) }  
  test("3")  { Iterable(2, 1).toSet should equal (Iterable(1, 2).toSet) }
  test("4")  { Iterable(2, 1) should not equal (Iterable(1, 2)) }
}

顺便说一句,Set 没有被订购.

BTW a Set is not ordered.

为避免删除重复元素,请尝试toSeq.sorted.下面通过:

edit: To avoid removing duplicate elements, try toSeq.sorted. The following pass:

  test("5")  { Iterable(2, 1).toSeq.sorted should equal (Iterable(1, 2).toSeq.sorted) }
  test("6")  { Iterable(2, 1).toSeq should not equal (Iterable(1, 2).toSeq) }

对于元素无法排序的无序集合,可以使用这种方法:

edit 2: For unordered collections where elements cannot be sorted, you can use this method:

  def sameAs[A](c: Traversable[A], d: Traversable[A]): Boolean = 
    if (c.isEmpty) d.isEmpty
    else {
      val (e, f) = d span (c.head !=)
      if (f.isEmpty) false else sameAs(c.tail, e ++ f.tail)
    }

例如(注意使用没有定义顺序的符号 'a 'b 'c)

e.g. (note use of symbols 'a 'b 'c which have no defined ordering)

  test("7")  { assert( sameAs(Iterable(2, 1),    Iterable(1, 2)     )) }
  test("8")  { assert( sameAs(Array('a, 'c, 'b), List('c, 'a, 'b)   )) }
  test("9")  { assert( sameAs("cba",             Set('a', 'b', 'c') )) }

替代 sameAs 实现:

  def sameAs[A](c: Traversable[A], d: Traversable[A]) = {
    def counts(e: Traversable[A]) = e groupBy identity mapValues (_.size)
    counts(c) == counts(d)
  }