/
OptionTSpec.scala
60 lines (43 loc) · 1.62 KB
/
OptionTSpec.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package com.github.niqdev.cats
import cats.data.OptionT
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpecLike
final class OptionTSpec extends AnyWordSpecLike with Matchers {
"OptionT" should {
"verify examples" in {
// monad: flatMap, map
import cats.instances.list.catsStdInstancesForList
// pure
import cats.syntax.applicative.catsSyntaxApplicativeId
type ListOption[A] = OptionT[List, A]
val result = for {
x <- OptionT(List(Option(10)))
y <- 32.pure[ListOption]
} yield x + y
result.value shouldBe List(Some(42))
}
"verify Future" in {
// monad: flatMap, map
import java.util.concurrent.TimeUnit
import cats.instances.future.catsStdInstancesForFuture
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration.Duration
import scala.concurrent.{ Await, Future }
val futureOption: Future[Option[String]] = Future.successful(Some("hello"))
val future: Future[String] = Future.successful("monad")
val option: Option[String] = Some("transformer")
val result: OptionT[Future, String] = for {
fo <- OptionT(futureOption)
f <- OptionT.liftF(future)
o <- OptionT.fromOption[Future](option)
} yield s"$fo $f $o"
result.value.isInstanceOf[Future[Option[String]]] shouldBe true
Await.result(result.value, Duration(1, TimeUnit.SECONDS)) match {
case Some(value) =>
value shouldBe "hello monad transformer"
case None =>
fail("unexpected failure")
}
}
}
}