-
Notifications
You must be signed in to change notification settings - Fork 26
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
Cross-build scalacheck module on scala 3 #857
Open
RustedBones
wants to merge
1
commit into
main
Choose a base branch
from
scala3-scalacheck
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 2024 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, subtypes(i).typeclass.arbitrary) | ||
} yield subtypeGen | ||
} | ||
} | ||
} | ||
|
||
implicit def gen[T]: Arbitrary[T] = macro Magnolia.gen[T] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like this signature change, it matches the Scalacheck API better too imo 👍 |
||
|
||
@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
41 changes: 41 additions & 0 deletions
41
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,41 @@ | ||
/* | ||
* 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 org.scalacheck.{Arbitrary, Cogen} | ||
|
||
import scala.reflect.macros.* | ||
object ScalaCheckMacros { | ||
def derivationArbitraryMacro[T: c.WeakTypeTag](c: whitebox.Context): c.Tree = { | ||
import c.universe._ | ||
val wtt = weakTypeTag[T] | ||
q"""_root_.magnolify.scalacheck.ArbitraryDerivation.gen[$wtt]""" | ||
} | ||
|
||
def derivationCogenMacro[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 autoDerivationArbitrary[T]: Arbitrary[T] = | ||
macro ScalaCheckMacros.derivationArbitraryMacro[T] | ||
implicit def autoDerivationCogen[T]: Cogen[T] = | ||
macro ScalaCheckMacros.derivationCogenMacro[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 2024 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, 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] |
27 changes: 27 additions & 0 deletions
27
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,27 @@ | ||
/* | ||
* Copyright 2024 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 org.scalacheck.{Arbitrary, Cogen} | ||
|
||
import scala.deriving.Mirror | ||
|
||
trait AutoDerivations: | ||
inline implicit def autoDerivationArbitrary[T](using Mirror.Of[T]): Arbitrary[T] = | ||
ArbitraryDerivation.derivedMirror[T] | ||
inline implicit def autoDerivationCogen[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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not totally following the
subtypes.size + size
logic, can you explain a bit?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Once we reach negative size, we'll stop picking type at random, and deterministically pick one of the subtype. This is done so we'll have a chance to end-up recursion when tree structure is big.
The downside is that such generated struct will often have the same kind of tali/leaves
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was previously done by providing the
Fallback
implicit.