Skip to content

Commit

Permalink
Merge pull request #438 from Hydrotoast/master
Browse files Browse the repository at this point in the history
Widen "oneOf" argument to "Iterable" from "Seq"
  • Loading branch information
rickynils authored Feb 25, 2019
2 parents aadbaa9 + 1cf6d1b commit 86bd34e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
12 changes: 12 additions & 0 deletions jvm/src/test/scala/org/scalacheck/GenSpecification.scala
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ object GenSpecification extends Properties("Gen") with GenSpecificationVersionSp
forAll(frequency(List.fill(n)((1,const(0))): _*)) { _ == 0 }
}

property("frequency 4") =
Prop.throws(classOf[IllegalArgumentException]) {
frequency()
}

property("lzy") = forAll((g: Gen[Int]) => lzy(g) == g)

property("wrap") = forAll((g: Gen[Int]) => delay(g) == g)
Expand Down Expand Up @@ -141,6 +146,13 @@ object GenSpecification extends Properties("Gen") with GenSpecificationVersionSp
}
}

property("oneOf n in set") = forAll { (s: Set[Int]) =>
Try(oneOf(s)) match {
case Success(g) => forAll(g)(s.contains)
case Failure(_) => Prop(s.isEmpty)
}
}

property("oneOf 2") = forAll { (n1:Int, n2:Int) =>
forAll(oneOf(n1, n2)) { n => n == n1 || n == n2 }
}
Expand Down
10 changes: 8 additions & 2 deletions src/main/scala/org/scalacheck/Gen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -521,15 +521,21 @@ object Gen extends GenArities with GenVersionSpecific {
/** Creates a resized version of a generator */
def resize[T](s: Int, g: Gen[T]) = gen((p, seed) => g.doApply(p.withSize(s), seed))

/** Picks a random value from a list */
def oneOf[T](xs: Seq[T]): Gen[T] =
/** Picks a random value from a list. */
def oneOf[T](xs: Iterable[T]): Gen[T] =
if (xs.isEmpty) {
throw new IllegalArgumentException("oneOf called on empty collection")
} else {
val vector = xs.toVector
choose(0, vector.size - 1).map(vector(_))
}

/** Picks a random value from a list.
* @todo Remove this overloaded method in the next major release. See #438.
*/
def oneOf[T](xs: Seq[T]): Gen[T] =
oneOf(xs: Iterable[T])

/** Picks a random value from a list */
def oneOf[T](t0: T, t1: T, tn: T*): Gen[T] = oneOf(t0 +: t1 +: tn)

Expand Down

0 comments on commit 86bd34e

Please sign in to comment.