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

Enable customization of Commands.Command shrinking #740

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions project/MimaSettings.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ object MimaSettings {
)

private def newMethods = Seq(
"org.scalacheck.commands.Commands.shrinkCommand",
"org.scalacheck.commands.Commands.shrinkSequentialCommands",
"org.scalacheck.commands.Commands.shrinkParallelCommands",
)

private def removedPrivateMethods = Seq(
Expand Down
25 changes: 23 additions & 2 deletions src/main/scala/org/scalacheck/commands/Commands.scala
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,24 @@ trait Commands {
* [[State]]. By default no shrinking is done for [[State]]. */
def shrinkState: Shrink[State] = implicitly

/** Override this to provide a custom Shrinker for your internal
* [[Command]]. By default no shrinking is done for [[Command]]. */
def shrinkCommand: Shrink[Command] = implicitly

/** Override this to provide a custom Shrinker of sequential [[Command]]s.
* By default, the implicit List shrinker is used with [[shrinkCommand]]. */
def shrinkSequentialCommands: Shrink[List[Command]] = {
implicit val commandShrinker = shrinkCommand
implicitly
}

/** Override this to provide a custom Shrinker of parallel [[Command]]s.
* By default, the implict List shrinker is used with [[shrinkCommand]]. */
def shrinkParallelCommands: Shrink[List[List[Command]]] = {
implicit val commandShrinker = shrinkCommand
implicitly
}

// Private methods //
private type Commands = List[Command]

Expand All @@ -270,9 +288,12 @@ trait Commands {
)

private implicit val shrinkActions: Shrink[Actions] = Shrink[Actions] { as =>
val shrinkSeq = shrinkSequentialCommands
val shrinkPar = shrinkParallelCommands

val shrinkedCmds: Stream[Actions] =
Shrink.shrink(as.seqCmds).map(cs => as.copy(seqCmds = cs)) append
Shrink.shrink(as.parCmds).map(cs => as.copy(parCmds = cs))
shrinkSeq.shrink(as.seqCmds).map(cs => as.copy(seqCmds = cs)) append
shrinkPar.shrink(as.parCmds).map(cs => as.copy(parCmds = cs))

Shrink.shrinkWithOrig[State](as.s)(shrinkState) flatMap { state =>
shrinkedCmds.map(_.copy(s = state))
Expand Down