-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cross-build scalacheck module on scala 3
- Loading branch information
1 parent
e3eec4a
commit 0f12318
Showing
23 changed files
with
348 additions
and
239 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
scalacheck/src/main/scala-2/magnolify/scalacheck/ArbitraryDerivation.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Copyright 2019 Spotify AB | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package magnolify.scalacheck | ||
|
||
import magnolia1.* | ||
import org.scalacheck.{Arbitrary, Gen} | ||
|
||
object ArbitraryDerivation { | ||
type Typeclass[T] = Arbitrary[T] | ||
|
||
private implicit val monadicGen: Monadic[Gen] = new Monadic[Gen] { | ||
override def point[A](value: A): Gen[A] = Gen.const(value) | ||
override def map[A, B](from: Gen[A])(fn: A => B): Gen[B] = from.map(fn) | ||
override def flatMap[A, B](from: Gen[A])(fn: A => Gen[B]): Gen[B] = from.flatMap(fn) | ||
} | ||
|
||
def join[T](caseClass: CaseClass[Arbitrary, T]): Arbitrary[T] = Arbitrary { | ||
caseClass.constructMonadic(_.typeclass.arbitrary) | ||
} | ||
|
||
def split[T](sealedTrait: SealedTrait[Arbitrary, T]): Arbitrary[T] = Arbitrary { | ||
Gen.lzy { | ||
Gen.sized { size => | ||
val subtypes = sealedTrait.subtypes | ||
for { | ||
i <- | ||
if (size >= 0) { | ||
// pick any subtype | ||
Gen.choose(0, subtypes.size - 1) | ||
} else { | ||
// pick a fixed subtype to have a chance to stop recursion | ||
Gen.const(subtypes.size + size) | ||
} | ||
subtypeGen <- Gen.resize(size - 1, sealedTrait.subtypes(i).typeclass.arbitrary) | ||
} yield subtypeGen | ||
} | ||
} | ||
} | ||
|
||
implicit def gen[T]: Arbitrary[T] = macro Magnolia.gen[T] | ||
|
||
@deprecated("Use gen instead", "0.7.0") | ||
def apply[T]: Arbitrary[T] = macro Magnolia.gen[T] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
scalacheck/src/main/scala-2/magnolify/scalacheck/ScalacheckMacros.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package magnolify.scalacheck | ||
|
||
import org.scalacheck.{Arbitrary, Cogen} | ||
|
||
import scala.reflect.macros.* | ||
object ScalaCheckMacros { | ||
def genArbitraryMacro[T: c.WeakTypeTag](c: whitebox.Context): c.Tree = { | ||
import c.universe._ | ||
val wtt = weakTypeTag[T] | ||
q"""_root_.magnolify.scalacheck.ArbitraryDerivation.gen[$wtt]""" | ||
} | ||
|
||
def genCogenMacro[T: c.WeakTypeTag](c: whitebox.Context): c.Tree = { | ||
import c.universe._ | ||
val wtt = weakTypeTag[T] | ||
q"""_root_.magnolify.scalacheck.CogenDerivation.gen[$wtt]""" | ||
} | ||
|
||
} | ||
|
||
trait AutoDerivations { | ||
implicit def genArbitrary[T]: Arbitrary[T] = macro ScalaCheckMacros.genArbitraryMacro[T] | ||
implicit def genCogen[T]: Cogen[T] = macro ScalaCheckMacros.genCogenMacro[T] | ||
} |
54 changes: 54 additions & 0 deletions
54
scalacheck/src/main/scala-3/magnolify/scalacheck/ArbitraryDerivation.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Copyright 2023 Spotify AB | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package magnolify.scalacheck | ||
|
||
import magnolia1.* | ||
import org.scalacheck.{Arbitrary, Gen} | ||
|
||
import scala.deriving.Mirror | ||
|
||
object ArbitraryDerivation extends Derivation[Arbitrary]: | ||
|
||
private given Monadic[Gen] with | ||
def point[A](value: A): Gen[A] = Gen.const(value) | ||
def map[A, B](from: Gen[A])(fn: A => B): Gen[B] = from.map(fn) | ||
def flatMap[A, B](from: Gen[A])(fn: A => Gen[B]): Gen[B] = from.flatMap(fn) | ||
|
||
def join[T](caseClass: CaseClass[Arbitrary, T]): Arbitrary[T] = Arbitrary { | ||
caseClass.constructMonadic(_.typeclass.arbitrary) | ||
} | ||
|
||
def split[T](sealedTrait: SealedTrait[Arbitrary, T]): Arbitrary[T] = Arbitrary { | ||
Gen.lzy { | ||
Gen.sized { size => | ||
val subtypes = sealedTrait.subtypes | ||
for { | ||
i <- | ||
if (size >= 0) { | ||
// pick any subtype | ||
Gen.choose(0, subtypes.size - 1) | ||
} else { | ||
// pick a fixed subtype to have a chance to stop recursion | ||
Gen.const(subtypes.size + size) | ||
} | ||
subtypeGen <- Gen.resize(size - 1, sealedTrait.subtypes(i).typeclass.arbitrary) | ||
} yield subtypeGen | ||
} | ||
} | ||
} | ||
|
||
inline def gen[T](using Mirror.Of[T]): Arbitrary[T] = derivedMirror[T] |
40 changes: 40 additions & 0 deletions
40
scalacheck/src/main/scala-3/magnolify/scalacheck/CogenDerivation.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Copyright 2023 Spotify AB | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package magnolify.scalacheck | ||
|
||
import magnolia1.* | ||
import org.scalacheck.Cogen | ||
|
||
import scala.deriving.Mirror | ||
|
||
object CogenDerivation extends Derivation[Cogen]: | ||
|
||
def join[T](caseClass: CaseClass[Cogen, T]): Cogen[T] = Cogen[T] { (seed, t) => | ||
caseClass.params.foldLeft(seed) { (s, p) => | ||
// inject index to distinguish cases like `(Some(false), None)` and `(None, Some(0))` | ||
p.typeclass.perturb(Cogen.perturb(s, p.index), p.deref(t)) | ||
} | ||
} | ||
|
||
def split[T](sealedTrait: SealedTrait[Cogen, T]): Cogen[T] = Cogen[T] { (seed, t) => | ||
sealedTrait.choose(t) { sub => | ||
// inject index to distinguish case objects instances | ||
sub.typeclass.perturb(Cogen.perturb(seed, sub.subtype.index), sub.cast(t)) | ||
} | ||
} | ||
|
||
inline def gen[T](using Mirror.Of[T]): Cogen[T] = derivedMirror[T] |
9 changes: 9 additions & 0 deletions
9
scalacheck/src/main/scala-3/magnolify/scalacheck/ScalacheckMacros.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package magnolify.scalacheck | ||
|
||
import org.scalacheck.{Arbitrary, Cogen} | ||
|
||
import scala.deriving.Mirror | ||
trait AutoDerivations: | ||
|
||
inline given genArbitrary[T](using Mirror.Of[T]): Arbitrary[T] = ArbitraryDerivation.derivedMirror[T] | ||
inline given genCogen[T](using Mirror.Of[T]): Cogen[T] = CogenDerivation.derivedMirror[T] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 0 additions & 82 deletions
82
scalacheck/src/main/scala/magnolify/scalacheck/semiauto/ArbitraryDerivation.scala
This file was deleted.
Oops, something went wrong.
16 changes: 16 additions & 0 deletions
16
scalacheck/src/main/scala/magnolify/scalacheck/semiauto/package.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package magnolify.scalacheck | ||
|
||
import org.scalacheck.{Arbitrary, Cogen} | ||
|
||
package object semiauto { | ||
|
||
@deprecated("Use Arbitrary.gen[T] instead", "0.7.0") | ||
val ArbitraryDerivation = magnolify.scalacheck.ArbitraryDerivation | ||
@deprecated("Use Gogen.gen[T] instead", "0.7.0") | ||
val CogenDerivation = magnolify.scalacheck.CogenDerivation | ||
|
||
implicit def genArbitrary(a: Arbitrary.type): magnolify.scalacheck.ArbitraryDerivation.type = | ||
magnolify.scalacheck.ArbitraryDerivation | ||
implicit def genCogen(c: Cogen.type): magnolify.scalacheck.CogenDerivation.type = | ||
magnolify.scalacheck.CogenDerivation | ||
} |
Oops, something went wrong.