0

I'm new into Scala, Scalacheck and specs2 so bear with me if maybe question is kind of obvious. I tried to look an example of this but couldnt find anything related.

Basically I'm looking for a way to create a test class using specs2 to define a Prop.forAll property which receives as parameters another scalacheck Property ( donno if this is possible ), a generator ( Gen[A] ) and a filepath and checks if the property pass for a determined set of samples( coming from the generator) and saves the error in the file.

Is there anyway to implement this? Talking in a generic way

mtelloz
  • 5
  • 3
  • How is your property receiving and other property + a generator + a file path a "Prop"? In particular when you go `Prop.forAll((p: Prop, g: Gen[A], f: File) => ...)` you must have an `Arbitrary` for p, g and f. – Eric Apr 30 '16 at 19:29
  • The Gen[A] is being obtained from a Class FromRDDGen which reads an RDD and returns a row wrapped as a Gen[A] , the path would be given in the test class. The main objective is : given a property P to check if it fails for that Gen[A] and save it in the file. – mtelloz May 03 '16 at 10:52

1 Answers1

0

I propose this:

import org.specs2._
import org.specs2.execute._
import org.scalacheck._

class TestSpec extends Specification with ScalaCheck { def is = s2"""

  Test my RDD $test

  """

  def test = {
    // get / create a generator
    val gen: Gen[Row] = ???
    // run the property
    property(gen, "prop1.txt") { r: Row =>
      r.values must haveSize(3)
    }
  }

  def property[A, R : AsResult](g: Gen[A], path: String)(prop: A => R): Result =
    saveResult(path)(Prop.forAll(g)(prop))

  def saveResult[R : AsResult](path: String)(r: R): Result = {
    val result = AsResult(r)
    if (!result.isSuccess) writeToFile(result, path)
    result
  }

  def writeToFile(result: Result, path: String) = ???

  case class Row(values: List[Int])
}
Eric
  • 15,494
  • 38
  • 61
  • Hello there! Trying to apply your example in the property function when calling the saveResult could you explain this bit of code: `saveResult(path)(Prop.forAll(g)(prop))` how does passing 2 parameters in parentheses to the forAll prop works. Im getting a Unit does not take any parameter error :( but overall your example looks really promising thank you! – mtelloz May 03 '16 at 17:14
  • Sorry about that. I edited the code, it now compiles. – Eric May 03 '16 at 22:08
  • It works! Thank you ! I would like to ask you something else if you let me , when defining the gen in my case i wrap a row from a RDD that is created from an avro file but since im implementing it with generics when trying to define complex properties in the test function I'm unable to do so since it just return a Row , should i create a case class as you did and define parameters according to the schema in the avro and cast it? By the way thank you for taking the time to reply my questions! Best regards. – mtelloz May 04 '16 at 18:21
  • Yes I think it is worth defining types where you can and draw a boundary between the untyped world and the typed one. – Eric May 04 '16 at 18:27