0

I have a newbie question for ScalaCheck which I am playing around with for the first time. Is it possible to create a Gen[Int] which will progress linearly from say 0 to N.

Such that when I use forAll in ScalaCheck it will increase the input Int by 1.

I would like this example to test with an increasing value

"Increase" should "always increase" in {
  forAll(validNumbers){ i:Int =>
    increase(i) should be (i + 1)
  }
}

Maybe this destroys ScalaChecks purpose and I should just test this way in ScalaTest.

Mellson
  • 2,918
  • 1
  • 21
  • 23

1 Answers1

1

You could do something like this:

def validNumbers(n: Int): Gen[Int] = Gen.resultOf[Int, Int] {
  new (Int => Int) {
    val current = (0 to n).iterator

    def apply(i: Int): Int = {
      if(current.hasNext) current.next else sys.error("No more numbers")
    }
  }
}

However I think you are indeed right, that this destroys ScalaChecks purpose. A simple for-loop would do better in this case.

Volker Stampa
  • 849
  • 6
  • 10
  • Thanks for that - you are right a simple for loop in ScalaTest is much better for my current need. – Mellson Jul 03 '14 at 15:55