Skip to content

Instantly share code, notes, and snippets.

@benjaminjackman
Created June 16, 2010 01:39
Show Gist options
  • Save benjaminjackman/440002 to your computer and use it in GitHub Desktop.
Save benjaminjackman/440002 to your computer and use it in GitHub Desktop.
//Supposing we have this a list of arbs of this form perhaps in a lib ArbFunc
object ArbFunc {
//...
def apply[T1, T2, T3, T4, R](f: (T1, T2, T3, T4) => R)(implicit a: Arbitrary[(T1, T2, T3, T4)]): Arbitrary[R] =
Arbitrary {for (t <- Arbitrary.arbitrary[(T1, T2, T3, T4)]) yield f(t._1, t._2, t._3, t._4)}
//...
}
//Given this case class
case class Point(label : String, x : Int, y : Int, z : Int)
//Now in my specification i simply say:
implicit val arbPoint = ArbFunc(Point.apply _)
property("test geometry equation with points") = forAll {(point: List[Point])
//test code goes in here.
}
//Code to autogenerate all of the FuncGens
def makeFuncGensCode(highestArity: Int) = {
def makeFuncGenCode(arity: Int) = {
val template = "def apply[%ps%, R](f: (%ps%) => R)(implicit a: Arbitrary[%pps%]) : Arbitrary[R] =\n"+
"Arbitrary {for (t <- Arbitrary.arbitrary[%pps%]) yield f(%ts%)}"
val ps = (1 to arity) map ("T" + _) mkString (",")
val (pps, ts) = if (arity > 1) {
("(" + ps + ")", (1 to arity) map ("t._" + _) mkString (","))
} else {
(ps, "t")
}
template replaceAll ("%ps%", ps) replaceAll ("%pps%", pps) replaceAll ("%ts%", ts)
}
(1 to highestArity) map makeFuncGenCode mkString ("\n")
}
//And the results going out to 22
object ArbFunc {
def apply[T1, R](f: (T1) => R)(implicit a: Arbitrary[T1]): Arbitrary[R] =
Arbitrary {for (t <- Arbitrary.arbitrary[T1]) yield f(t)}
def apply[T1, T2, R](f: (T1, T2) => R)(implicit a: Arbitrary[(T1, T2)]): Arbitrary[R] =
Arbitrary {for (t <- Arbitrary.arbitrary[(T1, T2)]) yield f(t._1, t._2)}
def apply[T1, T2, T3, R](f: (T1, T2, T3) => R)(implicit a: Arbitrary[(T1, T2, T3)]): Arbitrary[R] =
Arbitrary {for (t <- Arbitrary.arbitrary[(T1, T2, T3)]) yield f(t._1, t._2, t._3)}
def apply[T1, T2, T3, T4, R](f: (T1, T2, T3, T4) => R)(implicit a: Arbitrary[(T1, T2, T3, T4)]): Arbitrary[R] =
Arbitrary {for (t <- Arbitrary.arbitrary[(T1, T2, T3, T4)]) yield f(t._1, t._2, t._3, t._4)}
def apply[T1, T2, T3, T4, T5, R](f: (T1, T2, T3, T4, T5) => R)(implicit a: Arbitrary[(T1, T2, T3, T4, T5)]): Arbitrary[R] =
Arbitrary {for (t <- Arbitrary.arbitrary[(T1, T2, T3, T4, T5)]) yield f(t._1, t._2, t._3, t._4, t._5)}
def apply[T1, T2, T3, T4, T5, T6, R](f: (T1, T2, T3, T4, T5, T6) => R)(implicit a: Arbitrary[(T1, T2, T3, T4, T5, T6)]): Arbitrary[R] =
Arbitrary {for (t <- Arbitrary.arbitrary[(T1, T2, T3, T4, T5, T6)]) yield f(t._1, t._2, t._3, t._4, t._5, t._6)}
def apply[T1, T2, T3, T4, T5, T6, T7, R](f: (T1, T2, T3, T4, T5, T6, T7) => R)(implicit a: Arbitrary[(T1, T2, T3, T4, T5, T6, T7)]): Arbitrary[R] =
Arbitrary {for (t <- Arbitrary.arbitrary[(T1, T2, T3, T4, T5, T6, T7)]) yield f(t._1, t._2, t._3, t._4, t._5, t._6, t._7)}
def apply[T1, T2, T3, T4, T5, T6, T7, T8, R](f: (T1, T2, T3, T4, T5, T6, T7, T8) => R)(implicit a: Arbitrary[(T1, T2, T3, T4, T5, T6, T7, T8)]): Arbitrary[R] =
Arbitrary {for (t <- Arbitrary.arbitrary[(T1, T2, T3, T4, T5, T6, T7, T8)]) yield f(t._1, t._2, t._3, t._4, t._5, t._6, t._7, t._8)}
def apply[T1, T2, T3, T4, T5, T6, T7, T8, T9, R](f: (T1, T2, T3, T4, T5, T6, T7, T8, T9) => R)(implicit a: Arbitrary[(T1, T2, T3, T4, T5, T6, T7, T8, T9)]): Arbitrary[R] =
Arbitrary {for (t <- Arbitrary.arbitrary[(T1, T2, T3, T4, T5, T6, T7, T8, T9)]) yield f(t._1, t._2, t._3, t._4, t._5, t._6, t._7, t._8, t._9)}
def apply[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, R](f: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10) => R)(implicit a: Arbitrary[(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10)]): Arbitrary[R] =
Arbitrary {for (t <- Arbitrary.arbitrary[(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10)]) yield f(t._1, t._2, t._3, t._4, t._5, t._6, t._7, t._8, t._9, t._10)}
def apply[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, R](f: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11) => R)(implicit a: Arbitrary[(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11)]): Arbitrary[R] =
Arbitrary {for (t <- Arbitrary.arbitrary[(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11)]) yield f(t._1, t._2, t._3, t._4, t._5, t._6, t._7, t._8, t._9, t._10, t._11)}
def apply[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, R](f: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12) => R)(implicit a: Arbitrary[(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12)]): Arbitrary[R] =
Arbitrary {for (t <- Arbitrary.arbitrary[(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12)]) yield f(t._1, t._2, t._3, t._4, t._5, t._6, t._7, t._8, t._9, t._10, t._11, t._12)}
def apply[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, R](f: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13) => R)(implicit a: Arbitrary[(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13)]): Arbitrary[R] =
Arbitrary {for (t <- Arbitrary.arbitrary[(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13)]) yield f(t._1, t._2, t._3, t._4, t._5, t._6, t._7, t._8, t._9, t._10, t._11, t._12, t._13)}
def apply[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, R](f: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14) => R)(implicit a: Arbitrary[(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14)]): Arbitrary[R] =
Arbitrary {for (t <- Arbitrary.arbitrary[(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14)]) yield f(t._1, t._2, t._3, t._4, t._5, t._6, t._7, t._8, t._9, t._10, t._11, t._12, t._13, t._14)}
def apply[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, R](f: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15) => R)(implicit a: Arbitrary[(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15)]): Arbitrary[R] =
Arbitrary {for (t <- Arbitrary.arbitrary[(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15)]) yield f(t._1, t._2, t._3, t._4, t._5, t._6, t._7, t._8, t._9, t._10, t._11, t._12, t._13, t._14, t._15)}
def apply[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, R](f: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16) => R)(implicit a: Arbitrary[(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16)]): Arbitrary[R] =
Arbitrary {for (t <- Arbitrary.arbitrary[(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16)]) yield f(t._1, t._2, t._3, t._4, t._5, t._6, t._7, t._8, t._9, t._10, t._11, t._12, t._13, t._14, t._15, t._16)}
def apply[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, R](f: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17) => R)(implicit a: Arbitrary[(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17)]): Arbitrary[R] =
Arbitrary {for (t <- Arbitrary.arbitrary[(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17)]) yield f(t._1, t._2, t._3, t._4, t._5, t._6, t._7, t._8, t._9, t._10, t._11, t._12, t._13, t._14, t._15, t._16, t._17)}
def apply[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, R](f: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18) => R)(implicit a: Arbitrary[(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18)]): Arbitrary[R] =
Arbitrary {for (t <- Arbitrary.arbitrary[(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18)]) yield f(t._1, t._2, t._3, t._4, t._5, t._6, t._7, t._8, t._9, t._10, t._11, t._12, t._13, t._14, t._15, t._16, t._17, t._18)}
def apply[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, R](f: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19) => R)(implicit a: Arbitrary[(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19)]): Arbitrary[R] =
Arbitrary {for (t <- Arbitrary.arbitrary[(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19)]) yield f(t._1, t._2, t._3, t._4, t._5, t._6, t._7, t._8, t._9, t._10, t._11, t._12, t._13, t._14, t._15, t._16, t._17, t._18, t._19)}
def apply[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, R](f: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20) => R)(implicit a: Arbitrary[(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20)]): Arbitrary[R] =
Arbitrary {for (t <- Arbitrary.arbitrary[(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20)]) yield f(t._1, t._2, t._3, t._4, t._5, t._6, t._7, t._8, t._9, t._10, t._11, t._12, t._13, t._14, t._15, t._16, t._17, t._18, t._19, t._20)}
def apply[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, R](f: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21) => R)(implicit a: Arbitrary[(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21)]): Arbitrary[R] =
Arbitrary {for (t <- Arbitrary.arbitrary[(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21)]) yield f(t._1, t._2, t._3, t._4, t._5, t._6, t._7, t._8, t._9, t._10, t._11, t._12, t._13, t._14, t._15, t._16, t._17, t._18, t._19, t._20, t._21)}
def apply[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, R](f: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22) => R)(implicit a: Arbitrary[(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22)]): Arbitrary[R] =
Arbitrary {for (t <- Arbitrary.arbitrary[(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22)]) yield f(t._1, t._2, t._3, t._4, t._5, t._6, t._7, t._8, t._9, t._10, t._11, t._12, t._13, t._14, t._15, t._16, t._17, t._18, t._19, t._20, t._21, t._22)}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment