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

test: more tests for implicit precedence #15

Merged
merged 1 commit into from
Jul 24, 2023
Merged
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
10 changes: 9 additions & 1 deletion src/test/ArbitraryDerivingSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,21 @@ class ArbitraryDerivingSuite extends munit.FunSuite:
equalValues(ComplexADTWithNestedMembers.expectedGen)
}

test("Gives precedence to any arbitraries in scope") {
test("Gives precedence to any arbitraries in local scope") {
given arb: Arbitrary[SimpleADT] = Arbitrary(SimpleCaseClass.expectedGen)
val expectedGen: Gen[AbstractSubClass.SubclassB] =
arb.arbitrary.map(AbstractSubClass.SubclassB.apply)
equalValues(expectedGen)
}

test("given derivation does not take precedence over existing givens") {
equalValues(HasGivenInstances.specialHasGivenInstancesArbitrary.arbitrary)
}

test("given derivation of child-instances does not take precedence over existing givens") {
equalValues(HasMemberThatHasGivenInstances.expectedGen)
}

// not a hard requirement (just guarding against accidental worsening by refactoring)
test("supports case classes with up to 27 fields (if -Xmax-inlines=32)") {
summon[Arbitrary[MaxCaseClass]]
Expand Down
8 changes: 8 additions & 0 deletions src/test/CogenDerivingSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ class CogenDerivingSuite extends munit.ScalaCheckSuite:
equalValues(CaseClassWithContainers.expectedCogen)
}

test("given derivation does not take precedence over existing givens") {
equalValues(HasGivenInstances.specialHasGivenInstancesCogen)
}

test("given derivation of child-instances does not take precedence over existing givens") {
equalValues(HasMemberThatHasGivenInstances.expectedCogen)
}

test("enables derivation of Arbitrary instances for functions") {
val arbFunction1: Arbitrary[ComplexADTWithNestedMembers => ABC] =
summon
Expand Down
4 changes: 2 additions & 2 deletions src/test/ShrinkDerivingSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ class ShrinkDerivingSuite extends munit.ScalaCheckSuite:
equalValues(ABC.expectedShrink)
}

property("derivation does not take precedence over existing givens") {
property("given derivation does not take precedence over existing givens") {
equalValues(HasGivenInstances.specialHasGivenInstancesShrink)
}

property("derivation of child-instances does not take precedence over existing givens") {
property("given derivation of child-instances does not take precedence over existing givens") {
equalValues(HasMemberThatHasGivenInstances.expectedShrink)
}

Expand Down
29 changes: 27 additions & 2 deletions src/test/test_classes.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.martinhh

import org.scalacheck.Arbitrary
import org.scalacheck.Arbitrary.arbitrary
import org.scalacheck.Cogen
import org.scalacheck.Cogen.perturb
Expand Down Expand Up @@ -186,8 +187,15 @@ case class HasGivenInstances(x: Int)

object HasGivenInstances:

// this is a nonsense-implementation, only here to prove that it takes precedence
// over derivation (and over `Shrink.shrinkAny`):
// these are a nonsense-implementations, only here to prove that they takes precedence
// over/within derivation:

given specialHasGivenInstancesArbitrary: Arbitrary[HasGivenInstances] =
Arbitrary(Gen.const(HasGivenInstances(42)))

given specialHasGivenInstancesCogen: Cogen[HasGivenInstances] =
Cogen(_.x + 1)

@annotation.nowarn("msg=Stream .* is deprecated")
given specialHasGivenInstancesShrink: Shrink[HasGivenInstances] = Shrink { hgi =>
(1 to 3).map(i => HasGivenInstances(hgi.x + i)).toStream
Expand All @@ -196,6 +204,23 @@ object HasGivenInstances:
case class HasMemberThatHasGivenInstances(member: HasGivenInstances)

object HasMemberThatHasGivenInstances:

val expectedGen: Gen[HasMemberThatHasGivenInstances] =
HasGivenInstances.specialHasGivenInstancesArbitrary.arbitrary.map(
HasMemberThatHasGivenInstances.apply
)

val expectedCogen: Cogen[HasMemberThatHasGivenInstances] =
Cogen { (seed, value) =>
perturb[Unit](
perturb[HasGivenInstances](
seed,
value.member
)(using HasGivenInstances.specialHasGivenInstancesCogen),
()
)
}

val expectedShrink: Shrink[HasMemberThatHasGivenInstances] =
Shrink.xmap(HasMemberThatHasGivenInstances.apply, _.member)

Expand Down