Scalacheck / Scalatest with parametric types

I want to test a shared stack using scalatest and scalacheck. So far I have this:

"Stack" should "pop the last value pushed" in {
  check(doPushPop(element))
}

def doPushPop[T](element : T) : Boolean = {
  val stack = new Stack[T]
  stack.push(element)
  stack.pop() == element
}

      

However, this clearly doesn't compile. How do I specify the typical type as part of the test?

+3


source to share


1 answer


if you want to generate random values ​​eg. Ints:

check(doPushPop(_: Int))

      

but instead of testing with a single value, you should generate a push / pop sequence and check for some invariant; the stacks are obviously independent of the values ​​you put in them, so I would say that it makes no sense to have a trivial test like the one above.



Instead, you should read about how to test stateful systems with ScalaCheck:

http://www.scalacheck.org/files/scaladays2014/index.html

+1


source







All Articles