Last active
February 5, 2018 09:34
-
-
Save juanjovazquez/e27948edb8a94aea5c659eb94a913b52 to your computer and use it in GitHub Desktop.
Scalacheck generator for iota coproduct (No longer necessary after this http://bit.ly/2EFdY4k)
This file contains hidden or 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 is an ammonite scala script see: http://bit.ly/2FyQf58 | |
| // Dependencies | |
| import $ivy.`org.typelevel::cats-core:1.0.1` | |
| import $ivy.`com.chuusai::shapeless:2.3.3` | |
| import $ivy.`io.frees::iota-core:0.3.4` | |
| import $ivy.`org.scalacheck::scalacheck:1.13.5` | |
| // Regular imports | |
| import iota.{ Cop, TNil } | |
| import iota.TList.:: | |
| import iota.syntax.inject._ | |
| import org.scalacheck.Gen | |
| object Monday | |
| object Tuesday | |
| object Wednesday | |
| object Thursday | |
| object Friday | |
| type Weekday = | |
| Cop[Monday.type :: Tuesday.type :: Wednesday.type :: Thursday.type :: Friday.type :: TNil] | |
| // This won't compile as `Gen.oneOf` works with regular sequences so | |
| // iota won't find a proper instance for the `Inject` typeclass | |
| //val weekdayGen: Gen[Weekday] = | |
| // Gen.oneOf(Monday, Tuesday, Wednesday, Thursday, Friday).map(_.inject[Weekday]) | |
| //iotaGen.sc:24: could not find implicit value for parameter ev: iota.Cop.Inject[Object,ammonite.$file.iotaGen.Weekday] | |
| //Gen.oneOf(Monday, Tuesday, Wednesday, Thursday, Friday).map(_.inject[Weekday]) | |
| // We need to track the types, e.g.: | |
| import shapeless.{ HNil, Poly1 } | |
| object injectToWeekday extends Poly1 { | |
| implicit def genericCase[A](implicit ev: Cop.Inject[A, Weekday]) = | |
| at[A](_.inject[Weekday]) | |
| } | |
| val weekdayGen: Gen[Weekday] = | |
| Gen.oneOf( | |
| (Monday :: Tuesday :: Wednesday :: Thursday :: Friday :: HNil).map(injectToWeekday).toList | |
| ) | |
| // It works! | |
| println(weekdayGen.sample) | |
| // Some(Cop(ammonite.$file.iotaGen$Monday$@3c5fc608 @ 0)) | |
| println(weekdayGen.sample) | |
| // Some(Cop(ammonite.$file.iotaGen$Friday$@277d8c08 @ 4)) | |
| println(weekdayGen.sample) | |
| // Some(Cop(ammonite.$file.iotaGen$Wednesday$@7f7f6805 @ 2)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment