Skip to content

Instantly share code, notes, and snippets.

@loicknuchel
Last active September 18, 2018 14:05
Show Gist options
  • Save loicknuchel/710924358b6f06bb82eb806b1d953b00 to your computer and use it in GitHub Desktop.
Save loicknuchel/710924358b6f06bb82eb806b1d953b00 to your computer and use it in GitHub Desktop.
Error with scalacheck on shrinking classes with assert
Testing started at 15:02 ...
Id.apply(7982e330-7daf-4cbd-a299-67af73ad24de)
Generated: Test(Id(7982e330-7daf-4cbd-a299-67af73ad24de),,464053996)
Id.apply(7982e330-7daf-4cbd)
TestFailedException was thrown during property evaluation.
Message: TestSpec.this.Id.isValid(Id.this.value) was false Invalid Id: 7982e330-7daf-4cbd
Location: (TestSpec.scala:16)
Occurred when passed generated values (
)
ScalaTestFailureLocation: com.criteo.hadoop.mumak.core.domain.TestSpec$$anonfun$2 at (TestSpec.scala:29)
org.scalatest.exceptions.GeneratorDrivenPropertyCheckFailedException: TestFailedException was thrown during property evaluation.
Message: TestSpec.this.Id.isValid(Id.this.value) was false Invalid Id: 7982e330-7daf-4cbd
Location: (TestSpec.scala:16)
Occurred when passed generated values (
)
at org.scalatest.enablers.CheckerAsserting$$anon$2.indicateFailure(CheckerAsserting.scala:224)
at org.scalatest.enablers.CheckerAsserting$$anon$2.indicateFailure(CheckerAsserting.scala:220)
at org.scalatest.enablers.UnitCheckerAsserting$CheckerAssertingImpl.check(CheckerAsserting.scala:141)
at org.scalatest.prop.GeneratorDrivenPropertyChecks$class.forAll(GeneratorDrivenPropertyChecks.scala:851)
at com.criteo.hadoop.mumak.core.domain.TestSpec.forAll(TestSpec.scala:12)
at com.criteo.hadoop.mumak.core.domain.TestSpec$$anonfun$2.apply(TestSpec.scala:29)
at com.criteo.hadoop.mumak.core.domain.TestSpec$$anonfun$2.apply(TestSpec.scala:29)
at org.scalatest.OutcomeOf$class.outcomeOf(OutcomeOf.scala:85)
at org.scalatest.OutcomeOf$.outcomeOf(OutcomeOf.scala:104)
at org.scalatest.Transformer.apply(Transformer.scala:22)
at org.scalatest.Transformer.apply(Transformer.scala:20)
at org.scalatest.FunSpecLike$$anon$1.apply(FunSpecLike.scala:454)
at org.scalatest.TestSuite$class.withFixture(TestSuite.scala:196)
at org.scalatest.FunSpec.withFixture(FunSpec.scala:1630)
at org.scalatest.FunSpecLike$class.invokeWithFixture$1(FunSpecLike.scala:451)
at org.scalatest.FunSpecLike$$anonfun$runTest$1.apply(FunSpecLike.scala:464)
at org.scalatest.FunSpecLike$$anonfun$runTest$1.apply(FunSpecLike.scala:464)
at org.scalatest.SuperEngine.runTestImpl(Engine.scala:289)
at org.scalatest.FunSpecLike$class.runTest(FunSpecLike.scala:464)
at org.scalatest.FunSpec.runTest(FunSpec.scala:1630)
at org.scalatest.FunSpecLike$$anonfun$runTests$1.apply(FunSpecLike.scala:497)
at org.scalatest.FunSpecLike$$anonfun$runTests$1.apply(FunSpecLike.scala:497)
at org.scalatest.SuperEngine$$anonfun$traverseSubNodes$1$1.apply(Engine.scala:396)
at org.scalatest.SuperEngine$$anonfun$traverseSubNodes$1$1.apply(Engine.scala:384)
at scala.collection.immutable.List.foreach(List.scala:392)
at org.scalatest.SuperEngine.traverseSubNodes$1(Engine.scala:384)
at org.scalatest.SuperEngine.org$scalatest$SuperEngine$$runTestsInBranch(Engine.scala:379)
at org.scalatest.SuperEngine.runTestsImpl(Engine.scala:461)
at org.scalatest.FunSpecLike$class.runTests(FunSpecLike.scala:497)
at org.scalatest.FunSpec.runTests(FunSpec.scala:1630)
at org.scalatest.Suite$class.run(Suite.scala:1147)
at org.scalatest.FunSpec.org$scalatest$FunSpecLike$$super$run(FunSpec.scala:1630)
at org.scalatest.FunSpecLike$$anonfun$run$1.apply(FunSpecLike.scala:501)
at org.scalatest.FunSpecLike$$anonfun$run$1.apply(FunSpecLike.scala:501)
at org.scalatest.SuperEngine.runImpl(Engine.scala:521)
at org.scalatest.FunSpecLike$class.run(FunSpecLike.scala:501)
at org.scalatest.FunSpec.run(FunSpec.scala:1630)
at org.scalatest.tools.SuiteRunner.run(SuiteRunner.scala:45)
at org.scalatest.tools.Runner$$anonfun$doRunRunRunDaDoRunRun$1.apply(Runner.scala:1340)
at org.scalatest.tools.Runner$$anonfun$doRunRunRunDaDoRunRun$1.apply(Runner.scala:1334)
at scala.collection.immutable.List.foreach(List.scala:392)
at org.scalatest.tools.Runner$.doRunRunRunDaDoRunRun(Runner.scala:1334)
at org.scalatest.tools.Runner$$anonfun$runOptionallyWithPassFailReporter$2.apply(Runner.scala:1011)
at org.scalatest.tools.Runner$$anonfun$runOptionallyWithPassFailReporter$2.apply(Runner.scala:1010)
at org.scalatest.tools.Runner$.withClassLoaderAndDispatchReporter(Runner.scala:1500)
at org.scalatest.tools.Runner$.runOptionallyWithPassFailReporter(Runner.scala:1010)
at org.scalatest.tools.Runner$.run(Runner.scala:850)
at org.scalatest.tools.Runner.run(Runner.scala)
at org.jetbrains.plugins.scala.testingSupport.scalaTest.ScalaTestRunner.runScalaTest2(ScalaTestRunner.java:131)
at org.jetbrains.plugins.scala.testingSupport.scalaTest.ScalaTestRunner.main(ScalaTestRunner.java:28)
Caused by: org.scalatest.exceptions.TestFailedException: TestSpec.this.Id.isValid(Id.this.value) was false Invalid Id: 7982e330-7daf-4cbd
at org.scalatest.Assertions$class.newAssertionFailedException(Assertions.scala:528)
at org.scalatest.FunSpec.newAssertionFailedException(FunSpec.scala:1630)
at org.scalatest.Assertions$AssertionsHelper.macroAssert(Assertions.scala:501)
at com.criteo.hadoop.mumak.core.domain.TestSpec$Id.<init>(TestSpec.scala:16)
at com.criteo.hadoop.mumak.core.domain.TestSpec$$anonfun$2$anon$derivedShrink$macro$42$1$anon$macro$36$1.from(TestSpec.scala:29)
at com.criteo.hadoop.mumak.core.domain.TestSpec$$anonfun$2$anon$derivedShrink$macro$42$1$anon$macro$36$1.from(TestSpec.scala:29)
at org.scalacheck.derive.MkShrink$$anonfun$genericProduct$1$$anonfun$apply$1.apply(MkShrink.scala:41)
at org.scalacheck.derive.MkShrink$$anonfun$genericProduct$1$$anonfun$apply$1.apply(MkShrink.scala:41)
at scala.collection.immutable.Stream.map(Stream.scala:418)
at org.scalacheck.derive.MkShrink$$anonfun$org$scalacheck$derive$MkShrink$$lazyxmap$1.apply(MkShrink.scala:32)
at org.scalacheck.derive.MkShrink$$anonfun$org$scalacheck$derive$MkShrink$$lazyxmap$1.apply(MkShrink.scala:31)
at org.scalacheck.Shrink$$anon$1.shrink(Shrink.scala:41)
at org.scalacheck.derive.MkHListShrink$$anonfun$hcons$1$$anonfun$apply$7.apply(MkShrink.scala:78)
at org.scalacheck.derive.MkHListShrink$$anonfun$hcons$1$$anonfun$apply$7.apply(MkShrink.scala:76)
at org.scalacheck.Shrink$$anon$1.shrink(Shrink.scala:41)
at org.scalacheck.derive.MkShrink$$anonfun$org$scalacheck$derive$MkShrink$$lazyxmap$1.apply(MkShrink.scala:32)
at org.scalacheck.derive.MkShrink$$anonfun$org$scalacheck$derive$MkShrink$$lazyxmap$1.apply(MkShrink.scala:31)
at org.scalacheck.Shrink$$anon$1.shrink(Shrink.scala:41)
at org.scalacheck.Shrink$.shrink(Shrink.scala:45)
at org.scalacheck.Prop$$anonfun$forAll$9.apply(Prop.scala:862)
at org.scalacheck.Prop$$anonfun$forAll$9.apply(Prop.scala:862)
at org.scalacheck.Prop$$anonfun$forAllShrink$1.shrinker$1(Prop.scala:727)
at org.scalacheck.Prop$$anonfun$forAllShrink$1.apply(Prop.scala:751)
at org.scalacheck.Prop$$anonfun$forAllShrink$1.apply(Prop.scala:706)
at org.scalacheck.Prop$$anonfun$apply$5.apply(Prop.scala:292)
at org.scalacheck.Prop$$anonfun$apply$5.apply(Prop.scala:291)
at org.scalacheck.PropFromFun.apply(Prop.scala:22)
at org.scalacheck.Test$.org$scalacheck$Test$$workerFun$1(Test.scala:294)
at org.scalacheck.Test$$anonfun$3.apply(Test.scala:323)
at org.scalacheck.Test$$anonfun$3.apply(Test.scala:323)
at org.scalacheck.Platform$.runWorkers(Platform.scala:40)
at org.scalacheck.Test$.check(Test.scala:323)
at org.scalatest.enablers.UnitCheckerAsserting$CheckerAssertingImpl.check(CheckerAsserting.scala:89)
... 47 more
Process finished with exit code 0
import java.util.UUID
import org.scalacheck.ScalacheckShapeless._
import org.scalacheck.{Arbitrary, Gen}
import org.scalatest.prop.PropertyChecks
import org.scalatest.{FunSpec, Matchers}
import scala.util.Try
class TestSpec extends FunSpec with Matchers with PropertyChecks {
case class Id(value: String) {
println(s"Id.apply($value)")
assert(Id.isValid(value), s"Invalid Id: $value")
}
object Id {
def isValid(value: String): Boolean = Try(UUID.fromString(value)).isSuccess
}
case class Test(id: Id, value: String, num: Int)
implicit val aId = Arbitrary(Gen.uuid.map(id => Id(id.toString)))
implicit val aTest = implicitly[Arbitrary[Test]]
it("test") {
forAll { t: Test =>
println(s"Generated: $t")
true shouldBe false
}
}
}
@nbardiuk
Copy link

Shapeless library tries to shrink id field of Test as it would shrink a String, that is why it fails.

To fix your test you need to define your shrink strategy for Id type, that does not break assertion.

But in your specific case id is UUID that does not make sense to shrink, so it should be empty

implicit val sId = Shrink[Id](_ => Stream.empty[Id])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment