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

Experiment: convert any CE effect type to a bifunctor effect #1766

Open
wants to merge 5 commits into
base: develop
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package izumi.functional.bio.laws

import cats.effect.kernel.Async
import cats.effect.laws.AsyncTests
import izumi.functional.bio.catz
import izumi.functional.bio.impl.CatsToBIO
import izumi.functional.bio.impl.CatsToBIO.Bifunctorized
import izumi.functional.bio.laws.env.CatsTestEnv

class CatsLawsTest extends CatsLawsTestBase with CatsTestEnv {

checkAll(
"AsyncCE", {
implicit val ticker: Ticker = Ticker()
implicit val BIO = CatsToBIO.asyncToBIO[cats.effect.IO]
implicit val CE: Async[Bifunctorized[cats.effect.IO, Throwable, +_]] = catz.BIOToAsync
import scala.concurrent.duration.DurationInt
AsyncTests[Bifunctorized[cats.effect.IO, Throwable, +_]].async[Int, Int, Int](5.second)
},
)

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package izumi.functional.bio.laws.env

import cats.Eq
import cats.effect.IO
import cats.effect.kernel.Outcome
import cats.effect.testkit.TestInstances
import cats.kernel.Order
import izumi.functional.bio.impl.CatsToBIO.Bifunctorized
import org.scalacheck.{Arbitrary, Cogen, Prop}

import scala.concurrent.duration.FiniteDuration

trait CatsTestEnv extends TestInstances with EqThrowable {

implicit def cogenIO2[A: Cogen](implicit ticker: Ticker): Cogen[Bifunctorized[IO, Throwable, A]] =
cogenIO[A].contramap(_.unwrap)

implicit def arbitraryIO2[A: Arbitrary: Cogen](implicit ticker: Ticker): Arbitrary[Bifunctorized[IO, Throwable, A]] = {
Arbitrary(arbitraryIO[A].arbitrary.map(Bifunctorized.convertThrowable(_)))
}

implicit def orderIo2FiniteDuration(implicit ticker: Ticker): Order[Bifunctorized[IO, Throwable, FiniteDuration]] =
Order.by(_.unwrap)

override implicit def eqIOA[A: Eq](implicit ticker: Ticker): Eq[cats.effect.IO[A]] = {
(ioa, iob) =>
{
val a = unsafeRun(ioa)
val b = unsafeRun(iob)
Eq[Outcome[Option, Throwable, A]].eqv(a, b) || {
System.err.println(s"not equal a=$a b=$b")
false
}
}
}

implicit def eqIOA2[A: Eq](implicit ticker: Ticker): Eq[Bifunctorized[IO, Throwable, A]] =
Eq.by(_.unwrap)

implicit def ioBooleanToProp2(implicit ticker: Ticker): Bifunctorized[IO, Throwable, Boolean] => Prop =
iob => ioBooleanToProp(iob.unwrap)

// implicit def clock2(implicit ticker: Ticker): Clock2[IO] = {
// new Clock2[IO] {
// override def epoch: IO[Nothing, Long] = UIO(ticker.ctx.now().toMillis)
// override def now(accuracy: Clock1.ClockAccuracy): IO[Nothing, ZonedDateTime] = UIO(
// ZonedDateTime.ofInstant(Instant.ofEpochMilli(ticker.ctx.now().toMillis), ZoneOffset.UTC)
// )
// override def nowLocal(accuracy: Clock1.ClockAccuracy): IO[Nothing, LocalDateTime] = UIO(
// LocalDateTime.ofInstant(Instant.ofEpochMilli(ticker.ctx.now().toMillis), ZoneOffset.UTC)
// )
// override def nowOffset(accuracy: Clock1.ClockAccuracy): IO[Nothing, OffsetDateTime] = UIO(
// OffsetDateTime.ofInstant(Instant.ofEpochMilli(ticker.ctx.now().toMillis), ZoneOffset.UTC)
// )
// override def monotonicNano: IO[Nothing, Long] = UIO(ticker.ctx.now().toNanos)
// }
// }

}
Loading