Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Arbitrary instances for NonEmpty Lists and Vectors #486

Merged
merged 2 commits into from
Apr 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ object all
with RefTypeInstances
with StringInstances
with CollectionInstances
with CollectionInstancesBinCompat1
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package eu.timepit.refined.scalacheck

import eu.timepit.refined.api.{Refined, RefType}
import eu.timepit.refined.collection.Size
import eu.timepit.refined.collection.{NonEmpty, Size}
import org.scalacheck.{Arbitrary, Gen}
import org.scalacheck.util.Buildable

/** Module that provides `Arbitrary` instances for collection predicates. */
object collection extends CollectionInstances
object collection extends CollectionInstances with CollectionInstancesBinCompat1

trait CollectionInstances {

Expand Down Expand Up @@ -36,3 +36,22 @@ trait CollectionInstances {
Gen.buildableOfN[C, T](n.value, arbT.arbitrary)
})
}

trait CollectionInstancesBinCompat1 {

implicit def listNonEmptyArbitrary[F[_, _]: RefType, T: Arbitrary]
: Arbitrary[F[List[T], NonEmpty]] =
buildableNonEmptyArbitrary[F, List[T], T]

implicit def vectorNonEmptyArbitrary[F[_, _]: RefType, T: Arbitrary]
: Arbitrary[F[Vector[T], NonEmpty]] =
buildableNonEmptyArbitrary[F, Vector[T], T]

private[scalacheck] def buildableNonEmptyArbitrary[F[_, _]: RefType, C, T](
implicit
arbT: Arbitrary[T],
ev1: Buildable[T, C],
ev2: C => Traversable[T]
): Arbitrary[F[C, NonEmpty]] =
arbitraryRefType(Gen.nonEmptyBuildableOf(arbT.arbitrary))
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,8 @@ trait StringInstances {

implicit def nonEmptyStringArbitrary[F[_, _]](
implicit rt: RefType[F]
): Arbitrary[F[String, NonEmpty]] = {
val nonEmptyStringGen = for {
s <- Arbitrary.arbString.arbitrary
c <- Arbitrary.arbChar.arbitrary
} yield s + c.toString
arbitraryRefType(nonEmptyStringGen)
}
): Arbitrary[F[String, NonEmpty]] =
collection.buildableNonEmptyArbitrary[F, String, Char]

implicit def stringSizeArbitrary[F[_, _]: RefType, P](
implicit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package eu.timepit.refined.scalacheck

import eu.timepit.refined.W
import eu.timepit.refined.api.Refined
import eu.timepit.refined.collection.{MaxSize, Size}
import eu.timepit.refined.collection.{MaxSize, NonEmpty, Size}
import eu.timepit.refined.numeric.Interval
import eu.timepit.refined.scalacheck.collection._
import eu.timepit.refined.scalacheck.numeric._
Expand All @@ -17,9 +17,14 @@ class CollectionArbitrarySpec extends Properties("CollectionArbitrary") {
property("List[String] Refined MaxSize[_13]") =
checkArbitraryRefinedType[List[String] Refined MaxSize[_13]]

property("List[Int] Refined NonEmpty") = checkArbitraryRefinedType[List[Int] Refined NonEmpty]

property("Vector[Int] Refined MaxSize[23]") =
checkArbitraryRefinedType[Vector[Int] Refined MaxSize[W.`23`.T]]

property("Vector[Double] Refined NonEmpty") =
checkArbitraryRefinedType[Vector[Double] Refined NonEmpty]

property("Size[Interval.Closed[23, 42]]") =
checkArbitraryRefinedType[List[Char] Refined Size[Interval.Closed[W.`23`.T, W.`42`.T]]]
}
8 changes: 8 additions & 0 deletions notes/0.9.1.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
### New features

* Add `Arbitrary` instances for `NonEmpty` `List`s and `Vector`s.
([#486][#486] by [@fthomas][@fthomas])

[#486]: https://github.com/fthomas/refined/pull/486

[@fthomas]: https://github.com/fthomas