Skip to content

Commit

Permalink
Remove provided implicit evidence
Browse files Browse the repository at this point in the history
  • Loading branch information
RustedBones committed Mar 25, 2024
1 parent 5bfe51a commit 94532f3
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@ import generic.CanBuildFrom
import scala.collection.mutable.Builder

private[util] trait BuildableVersionSpecific {
import scala.collection.JavaConverters._
import scala.language.implicitConversions

implicit def wrapArrayList[T](xs: ArrayList[T]): Traversable[T] = xs.asScala
implicit def wrapHashMap[K, V](xs: HashMap[K, V]): Traversable[(K, V)] = xs.asScala

implicit def buildableCanBuildFrom[T, F, C](implicit c: CanBuildFrom[F, T, C]): Buildable[T, C] =
new Buildable[T, C] {
def builder = c.apply
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ import scala.collection.mutable.Builder
import scala.collection.{Map => _, _}

private[util] trait BuildableVersionSpecific {
import scala.jdk.CollectionConverters._

implicit def wrapArrayList[T](xs: ArrayList[T]): Iterable[T] = xs.asScala
implicit def wrapHashMap[K, V](xs: HashMap[K, V]): Iterable[(K, V)] = xs.asScala

implicit def buildableFactory[T, C](implicit f: Factory[T, C]): Buildable[T, C] =
new Buildable[T, C] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ object BuildableSpecification {
evt: C[String] => Traversable[String]
) = Gen.containerOf[C, String](Gen.alphaStr)

def buildable[C[_, _]](implicit
evb: Buildable[(String, Long), C[String, Long]],
evt: C[String, Long] => Traversable[(String, Long)]
) = Gen.buildableOf[C[String, Long], (String, Long)](Gen.zip(Gen.alphaStr, Gen.long))

implicit val listGen: Gen[List[String]] = container[List]

implicit val streamGen: Gen[Stream[String]] = container[Stream]
Expand Down Expand Up @@ -49,14 +54,16 @@ object BuildableSpecification {

implicit val trieIteratorGen: Gen[immutable.Queue[String]] = container[immutable.Queue]

implicit val arrayListGen: Gen[java.util.ArrayList[String]] = container[java.util.ArrayList]
implicit val mapGen: Gen[Map[String, Long]] = buildable[Map]

Gen.buildableOf[Map[String, Int], (String, Int)](Gen.zip(Gen.alphaStr, Gen.choose(0, 100)))

def buildable[C[_, _]](implicit
evb: Buildable[(String, Long), C[String, Long]],
evt: C[String, Long] => Traversable[(String, Long)]
) = Gen.buildableOf[C[String, Long], (String, Long)](for (str <- Gen.alphaStr; lng <- Gen.long) yield (str, lng))
// java containers
{
import scala.collection.convert.ImplicitConversionsToScala._

implicit val mapGen: Gen[Map[String, Long]] = buildable[Map]
implicit val arrayListGen: Gen[java.util.ArrayList[String]] = container[java.util.ArrayList]

implicit val hashMapGen: Gen[java.util.HashMap[String, Long]] = buildable[java.util.HashMap]
implicit val hashMapGen: Gen[java.util.HashMap[String, Long]] = buildable[java.util.HashMap]
}
}
33 changes: 20 additions & 13 deletions doc/UserGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -468,32 +468,39 @@ value ultimately reported as failing might not satisfy the condition given to
case that does. To avoid confusion, the corresponding shrink for the type can
use `suchThat` method too.

#### Generating Containers
#### Generating Buildables and Containers

There is a special generator, `Gen.containerOf`, that generates containers such
as lists and arrays. They take another generator as argument, that is
responsible for generating the individual items. You can use it in the
following way:
There are special generators
* `Gen.buildableOf`, that generates any buildable collection
* `Gen.containerOf`, a convenience method for single parameter buildable like lists and arrays

They take another generator as argument, that is responsible for generating the individual items.
You can use it in the following way:

```scala
val genIntList = Gen.containerOf[List,Int](Gen.oneOf(1, 3, 5))
implicit genInt: Gen[Int] = Gen.oneOf(1, 3, 5)
val genIntList = Gen.containerOf[List, Int]

val genStringLazyList = Gen.containerOf[LazyList,String](Gen.alphaStr)
val genStringLazyList = Gen.containerOf[LazyList, String](Gen.alphaStr)
val genBoolArray = Gen.containerOf[Array, Boolean](Gen.const(true))

val genBoolArray = Gen.containerOf[Array,Boolean](true)
implicit getTuple: Gen[(String, Int)] = Gen.zip(Gen.alphaStr, Gen.choose(0, 100))
val genMap = Gen.buildableOf[Map[String, Int], (String, Int)]
```

By default, ScalaCheck supports generation of `List`, `Stream` (Scala 2.10 -
2.12, deprecated in 2.13), `LazyList` (Scala 2.13), `Set`, `Array`, and
`ArrayList` (from `java.util`). You can add support for additional containers
by adding implicit `Buildable` instances. See `Buildable.scala` for examples.
2.12, deprecated in 2.13), `LazyList` (Scala 2.13), `Set`, `Array` and `Map`.
Additionally, ScalaCheck can generate `java.util.ArrayList` and `java.util.HashMap` when an implicit
`Traversable` conversion evidence is in scope.

You can add support for additional containers by adding implicit `Buildable` instances.
See `Buildable.scala` for examples.

There is also `Gen.nonEmptyContainerOf` for generating non-empty containers, and
`Gen.containerOfN` for generating containers of a given size.

To generate a container by picking an arbitrary number of elements use
`Gen.someOf`, or by picking one or more elements with
`Gen.atLeastOne`.
`Gen.someOf`, or by picking one or more elements with `Gen.atLeastOne`.

```scala
val zeroOrMoreDigits = Gen.someOf(1 to 9)
Expand Down

0 comments on commit 94532f3

Please sign in to comment.