Skip to content

Commit 310ab76

Browse files
authored
Merge pull request #20 from rallyhealth/gen-collect
Add Gen.collect operation
2 parents 1503a0d + e143388 commit 310ab76

File tree

4 files changed

+52
-9
lines changed

4 files changed

+52
-9
lines changed

README.md

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
<a href='https://travis-ci.org/gloriousfutureio/scalacheck-ops'>
2-
<img src='https://travis-ci.org/gloriousfutureio/scalacheck-ops.svg?branch=master' alt='Build Status' />
1+
<a href='https://travis-ci.org/rallyhealth/scalacheck-ops'>
2+
<img src='https://travis-ci.org/rallyhealth/scalacheck-ops.svg?branch=master' alt='Build Status' />
33
</a>
4-
<a href='https://coveralls.io/github/gloriousfutureio/scalacheck-ops?branch=master'>
5-
<img src='https://coveralls.io/repos/github/gloriousfutureio/scalacheck-ops/badge.svg?branch=master' alt='Coverage Status' />
4+
<a href='https://coveralls.io/github/rallyhealth/scalacheck-ops?branch=master'>
5+
<img src='https://coveralls.io/repos/github/rallyhealth/scalacheck-ops/badge.svg?branch=master' alt='Coverage Status' />
66
</a>
77
<table>
88
<tr>
@@ -19,17 +19,17 @@
1919
</td>
2020
<td>
2121
<a href='https://bintray.com/jeffmay/maven/scalacheck-ops_1-12/_latestVersion'>
22-
<img src='https://api.bintray.com/packages/jeffmay/maven/scalacheck-ops_1-12/images/download.svg'>
22+
<img src='https://api.bintray.com/packages/rallyhealth/ivy-scala-libs/scalacheck-ops_1-12/images/download.svg'>
2323
</a>
2424
</td>
2525
<td>
2626
<a href='https://bintray.com/jeffmay/maven/scalacheck-ops_1-13/_latestVersion'>
27-
<img src='https://api.bintray.com/packages/jeffmay/maven/scalacheck-ops_1-13/images/download.svg'>
27+
<img src='https://api.bintray.com/packages/rallyhealth/ivy-scala-libs/scalacheck-ops_1-13/images/download.svg'>
2828
</a>
2929
</td>
3030
<td>
3131
<a href='https://bintray.com/jeffmay/maven/scalacheck-ops_1-14/_latestVersion'>
32-
<img src='https://api.bintray.com/packages/jeffmay/maven/scalacheck-ops_1-14/images/download.svg'>
32+
<img src='https://api.bintray.com/packages/rallyhealth/ivy-scala-libs/scalacheck-ops_1-14/images/download.svg'>
3333
</a>
3434
</td>
3535
</tr>
@@ -50,6 +50,9 @@ with scalacheck-ops.
5050

5151
### Compatibility
5252

53+
**NOTE** Version 1.x lives under the `"me.jeffmay"` organization. Version 2.x lives
54+
under `"com.rallyhealth"`
55+
5356
**NOTE** Version 2.x and above **requires** JDK >=8 and Scala >=2.11 as this
5457
library expects the java.time standard library module.
5558

build.sbt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,17 @@ publishLocal := {}
88
organization in ThisBuild := "com.rallyhealth"
99
organizationName in ThisBuild := "Rally Health"
1010

11-
semVerLimit in ThisBuild := "2.0.999"
11+
semVerLimit in ThisBuild := "2.1.999"
1212
scalaVersion in ThisBuild := "2.11.11"
1313
licenses in ThisBuild := Seq("MIT" -> url("http://opensource.org/licenses/MIT"))
1414

15-
bintrayOrganization in ThisBuild := Some("rallyhealth")
15+
val bintrayOrg = "rallyhealth"
16+
17+
bintrayOrganization := Some(bintrayOrg)
1618
bintrayRepository := "ivy-scala-libs"
1719

20+
resolvers in ThisBuild += Resolver.bintrayRepo(bintrayOrg, bintrayRepository.value)
21+
1822
def commonProject(id: String, artifact: String, path: String): Project = {
1923
Project(id, file(path)).settings(
2024
name := artifact,

core/src/main/scala/org/scalacheck/ops/GenOps.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ object GenOps {
1313
def bits(size: Int): Gen[BitSet] = Gen.listOfN(size, binary).map(BitSet(_: _*))
1414
def bits: Gen[BitSet] = Gen.sized(bits)
1515

16+
def collect[T, U](gen: Gen[T], retryUntilMatch: Boolean = false)(pf: PartialFunction[T, U]): Gen[U] = {
17+
{
18+
if (retryUntilMatch) gen.retryUntil(pf.isDefinedAt)
19+
else gen.suchThat(pf.isDefinedAt)
20+
} map pf
21+
}
22+
1623
def enumValue[E <: Enumeration](enum: E): Gen[enum.Value] =
1724
Gen.oneOf[enum.Value](enum.values.toSeq)
1825

core/src/test/scala/org/scalacheck/ops/GenOpsSpec.scala

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,35 @@ with ScalaCheckImplicits {
1919
}
2020
}
2121

22+
behavior of "Gen.collect"
23+
24+
it should "use suchThat when retryUntilMatch is false" in {
25+
val collectEven = Gen.collect(Gen.chooseNum(1, 10), retryUntilMatch = false) {
26+
case x if x % 2 == 0 => x
27+
}
28+
val examples = collectEven.sampleIterator.take(100).toSeq
29+
examples should contain (None)
30+
assert(examples forall (_ forall (_ % 2 == 0)))
31+
}
32+
33+
it should "use retryUntil when retryUntilMatch is true" in {
34+
val collectEven = Gen.collect(Gen.chooseNum(1, 10), retryUntilMatch = true) {
35+
case x if x % 2 == 0 => x
36+
}
37+
val examples = collectEven.sampleIterator.take(100).toSeq
38+
examples should not contain None
39+
assert(examples forall (_ exists (_ % 2 == 0)))
40+
}
41+
42+
it should "avoid infinite recursion by default" in {
43+
val collectNegative = Gen.collect(Gen.chooseNum(1, 10)) {
44+
case x if x < 0 => x
45+
}
46+
an [EmptyGenSampleException[Int]] shouldBe thrownBy {
47+
collectNegative.getOrThrow
48+
}
49+
}
50+
2251
behavior of "Gen.setOfN"
2352

2453
it should "be able to generate a certain size" in {

0 commit comments

Comments
 (0)