Testing out JmonkeyEngine to make a game in Scala with Akka Actors within a pure FP layer
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

3 years ago
  1. package wow.doge.mygame
  2. import cats.data.ReaderT
  3. import monix.bio.UIO
  4. import org.scalatest.funsuite.AnyFunSuite
  5. import monix.execution.Scheduler.Implicits.global
  6. import monix.bio.IO
  7. class ReaderT_Test extends AnyFunSuite {
  8. // type IoReaderT[S, E, A] = ReaderT[UIO, S, Either[E, A]]
  9. // val IoReaderT = ReaderT
  10. // val t =
  11. // ReaderT[UIO, String, Either[Error, Unit]](s => UIO.unit.attempt)
  12. // .run("s")
  13. // .rethrow
  14. // val r: IoReaderT[String, Error, Unit] = IoReaderT(s => UIO.unit.attempt)
  15. // val t2 = r.run("s").rethrow
  16. // Kleisli[IO, String, Unit](s => IO.unit)
  17. case class Environment(str: String, num: Int)
  18. // test("runReaderT_Test") {
  19. // def fun1: ReaderT[UIO, String, Unit] = ReaderT(str => UIO(println(str)))
  20. // def fun2: ReaderT[UIO, Int, Unit] = ReaderT(num => UIO(println(num)))
  21. // def total: ReaderT[UIO, Environment, Unit] =
  22. // for {
  23. // _ <- fun1.local[Environment](_.str)
  24. // _ <- fun2.local[Environment](_.num)
  25. // } yield ()
  26. // val uio: UIO[Unit] = total.run(Environment("hello", 50))
  27. // uio.runSyncUnsafe()
  28. // }
  29. test("2") {
  30. def fun1: ReaderT[IO[String, ?], String, Unit] =
  31. ReaderT(s => IO(println(s)).onErrorHandleWith(_ => IO.raiseError("wow")))
  32. def fun2: ReaderT[IO[String, ?], Int, Unit] =
  33. ReaderT(num =>
  34. IO(println(num)).onErrorHandleWith(_ => IO.raiseError("whew"))
  35. )
  36. def fun3: ReaderT[IO[String, ?], Environment, Unit] =
  37. for {
  38. env <- ReaderT.ask[IO[String, ?], Environment]
  39. } yield ()
  40. def fun4: ReaderT[IO[String, ?], Environment, Unit] =
  41. for {
  42. env <- ReaderT.ask[IO[String, ?], Environment]
  43. _ <- ReaderT.liftF(
  44. IO(println(env)).onErrorHandleWith(_ => IO.raiseError("wow"))
  45. )
  46. _ <- fun3
  47. } yield ()
  48. def app: ReaderT[IO[String, ?], Environment, Unit] =
  49. for {
  50. _ <- fun1.local[Environment](_.str)
  51. _ <- fun2.local[Environment](_.num)
  52. _ <- fun3
  53. _ <- fun4
  54. } yield ()
  55. val io: IO[String, Unit] = app.run(Environment("hello", 50))
  56. io.attempt.runSyncUnsafe()
  57. }
  58. }