3
votes

I want to test a generic stack with 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 doesn't compile obviously. How do I specify the generic type as part of the test?

1

1 Answers

1
votes

if you want to generate random values, e.g. ints:

check(doPushPop(_: Int))

but instead of testing with a single value, you should instead generate a sequence of push/pop actions and verify some invariant; stacks obviously don't depend on the values you put into them so I'd say it makes little sense to have such a trivial test as the one above.

You should instead read up on how to test stateful systems with ScalaCheck:

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