views:

76

answers:

2

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

+2  A: 
import org.specs.matcher.IterableMatchers._
calculation(Input22) should beTheSameSeqAs (Seq("x","u"))
Alexey Romanov
ah thanks... that's nice
Ichthyo
but in my current program, I don't have a dependency on *specs* yet and I'm reluctant to add it. So I ended up implementing my own custom matcher, which gave me exactly the same solution. See http://www.scalatest.org/scaladoc/doc-1.2/org/scalatest/matchers/Matcher.html
Ichthyo
Do you fear specs? It is quite excellent. Embrace it.
Synesso
no, not in general... but in this case there is an requirement to keep things as simple as possible. Specs would just be yet another library to explain to people already fighting with the very basics of Scala
Ichthyo
+3  A: 

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.)

Bill Venners