forked from nova/jmonkey-test
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
67 lines
2.1 KiB
67 lines
2.1 KiB
package wow.doge.mygame
|
|
|
|
import cats.data.ReaderT
|
|
import monix.bio.UIO
|
|
import org.scalatest.funsuite.AnyFunSuite
|
|
import monix.execution.Scheduler.Implicits.global
|
|
import monix.bio.IO
|
|
|
|
class ReaderT_Test extends AnyFunSuite {
|
|
|
|
// type IoReaderT[S, E, A] = ReaderT[UIO, S, Either[E, A]]
|
|
// val IoReaderT = ReaderT
|
|
// val t =
|
|
// ReaderT[UIO, String, Either[Error, Unit]](s => UIO.unit.attempt)
|
|
// .run("s")
|
|
// .rethrow
|
|
// val r: IoReaderT[String, Error, Unit] = IoReaderT(s => UIO.unit.attempt)
|
|
// val t2 = r.run("s").rethrow
|
|
// Kleisli[IO, String, Unit](s => IO.unit)
|
|
|
|
case class Environment(str: String, num: Int)
|
|
|
|
// test("runReaderT_Test") {
|
|
// def fun1: ReaderT[UIO, String, Unit] = ReaderT(str => UIO(println(str)))
|
|
// def fun2: ReaderT[UIO, Int, Unit] = ReaderT(num => UIO(println(num)))
|
|
// def total: ReaderT[UIO, Environment, Unit] =
|
|
// for {
|
|
// _ <- fun1.local[Environment](_.str)
|
|
// _ <- fun2.local[Environment](_.num)
|
|
// } yield ()
|
|
|
|
// val uio: UIO[Unit] = total.run(Environment("hello", 50))
|
|
// uio.runSyncUnsafe()
|
|
// }
|
|
|
|
test("2") {
|
|
def fun1: ReaderT[IO[String, ?], String, Unit] =
|
|
ReaderT(s => IO(println(s)).onErrorHandleWith(_ => IO.raiseError("wow")))
|
|
def fun2: ReaderT[IO[String, ?], Int, Unit] =
|
|
ReaderT(num =>
|
|
IO(println(num)).onErrorHandleWith(_ => IO.raiseError("whew"))
|
|
)
|
|
def fun3: ReaderT[IO[String, ?], Environment, Unit] =
|
|
for {
|
|
env <- ReaderT.ask[IO[String, ?], Environment]
|
|
} yield ()
|
|
def fun4: ReaderT[IO[String, ?], Environment, Unit] =
|
|
for {
|
|
env <- ReaderT.ask[IO[String, ?], Environment]
|
|
_ <- ReaderT.liftF(
|
|
IO(println(env)).onErrorHandleWith(_ => IO.raiseError("wow"))
|
|
)
|
|
_ <- fun3
|
|
} yield ()
|
|
def app: ReaderT[IO[String, ?], Environment, Unit] =
|
|
for {
|
|
_ <- fun1.local[Environment](_.str)
|
|
_ <- fun2.local[Environment](_.num)
|
|
_ <- fun3
|
|
_ <- fun4
|
|
} yield ()
|
|
|
|
val io: IO[String, Unit] = app.run(Environment("hello", 50))
|
|
io.attempt.runSyncUnsafe()
|
|
}
|
|
|
|
}
|