Skip to content

Commit

Permalink
feat: Add always-succeeding arbitraries for not empty collections (#206)
Browse files Browse the repository at this point in the history
  • Loading branch information
matwojcik authored Jan 10, 2024
1 parent 3c8f4ec commit 6776639
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,6 @@ object collection:
constrainedLast <- arbConstrained.arbitrary
init <- Gen.containerOf[CC, A](arb.arbitrary)
yield (buildable.builder ++= evt(init) += constrainedLast ).result()
).asInstanceOf
).asInstanceOf

inline given notEmptyCollection[CC, A](using evb: Buildable[A, CC], ev2: CC => Iterable[A], arb: Arbitrary[A]): Arbitrary[CC :| Not[Empty]] = Arbitrary(Gen.nonEmptyBuildableOf[CC, A](arb.arbitrary)).asInstanceOf
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package io.github.iltotore.iron.scalacheck

import io.github.iltotore.iron.*
import io.github.iltotore.iron.constraint.collection.Empty
import io.github.iltotore.iron.constraint.string.*
import org.scalacheck.{Arbitrary, Gen}
import org.scalacheck.Gen.Choose

import scala.compiletime.constValue

object string:

inline given notEmptyString: Arbitrary[String :| Not[Empty]] = collection.notEmptyCollection[String, Char]

inline given startWith[V <: String]: Arbitrary[String :| StartWith[V]] =
Arbitrary(Gen.asciiStr.map(constValue[V] + _)).asInstanceOf

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ package io.github.iltotore.iron.scalacheck
import io.github.iltotore.iron.*
import io.github.iltotore.iron.constraint.all.*
import io.github.iltotore.iron.constraint.numeric.*
import io.github.iltotore.iron.scalacheck.any.given
import io.github.iltotore.iron.scalacheck.collection.given
import io.github.iltotore.iron.scalacheck.numeric.given
import io.github.iltotore.iron.scalacheck.all.given
import org.scalacheck.*
import utest.*

Expand All @@ -23,6 +21,7 @@ object CollectionSuite extends TestSuite:
test("empty") {
test("seq") - testGen[Seq[Boolean], Empty]
test("string") - testGen[String, Empty]
test("not empty") - testGen[Seq[Boolean], Not[Empty]]
}
test("contain") {
test("seq") - testGen[Seq[Boolean], Contain[true]]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.iltotore.iron.scalacheck

import io.github.iltotore.iron.*
import io.github.iltotore.iron.constraint.collection.Empty
import io.github.iltotore.iron.constraint.string.*
import io.github.iltotore.iron.scalacheck.string.given
import org.scalacheck.*
Expand All @@ -11,4 +12,5 @@ object StringSuite extends TestSuite:
val tests: Tests = Tests {
test("startWith") - testGen[String, StartWith["abc"]]
test("endWith") - testGen[String, EndWith["abc"]]
test("not empty string") - testGen[String, Not[Empty]]
}

0 comments on commit 6776639

Please sign in to comment.