-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtest_mtransf.nim
86 lines (78 loc) · 3.02 KB
/
test_mtransf.nim
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import unittest,
sugar,
fp/option,
fp/either,
fp/list,
fp/forcomp,
fp/mtransf
suite "Monad transformers":
test "OptionTOption":
let v = optionT(1.some.some)
check: v.getOrElse(2) == 1.some
check: v.map(v => $v).getOrElse("") == "1".some
check: v.flatMapF((v: int) => ($v).some).getOrElse("") == "1".some
check: v.flatMap((v: int) => optionT(($v).some.some)).getOrElse("") == "1".some
proc getArticle(id: int): Option[Option[string]] =
if id == 0:
none(Option[string])
else:
($id).some.some
let articles = act:
a1 <- optionT(getArticle(1))
a2 <- optionT(getArticle(2))
a3 <- optionT(getArticle(3))
optionT((a1, a2, a3).some.some)
check: articles.run == ("1", "2", "3").some.some
let badArticles = act:
a <- articles
bad <- optionT(getArticle(0))
(a[0], a[1], a[2], bad).some.some.optionT
check: badArticles.run == none(Option[(string, string, string, string)])
test "OptionTEither":
let v = optionT(1.some.rightS)
check: v.getOrElse(2) == 1.rightS
check: v.map(v => $v).getOrElse("") == "1".rightS
check: v.flatMapF((v: int) => ($v).rightS).getOrElse("") == "1".rightS
check: v.flatMap((v: int) => optionT(($v).some.rightS)).getOrElse("") == "1".rightS
proc getArticle(id: int): EitherS[Option[string]] =
if id == 0:
"Not found".left(Option[string])
else:
($id).some.rightS
let articles = act:
a1 <- optionT(getArticle(1))
a2 <- optionT(getArticle(2))
a3 <- optionT(getArticle(3))
optionT((a1, a2, a3).some.rightS)
check: articles.run == ("1", "2", "3").some.rightS
let badArticles = act:
a <- articles
bad <- optionT(getArticle(0))
(a[0], a[1], a[2], bad).some.rightS.optionT
check: badArticles.run == "Not found".left(Option[(string, string, string, string)])
test "OptionTList":
let v = optionT([1.some].asList)
check: v.getOrElse(2) == [1].asList
check: v.map(v => $v).getOrElse("") == ["1"].asList
check: v.flatMapF((v: int) => [$v].asList).getOrElse("") == ["1"].asList
check: v.flatMap((v: int) => optionT([($v).some].asList)).getOrElse("") == ["1"].asList
proc getArticle(id: int): List[Option[string]] =
if id == 0:
Nil[Option[string]]()
else:
($id).some.point(List)
let articles = act:
a1 <- optionT(getArticle(1))
a2 <- optionT(getArticle(2))
a3 <- optionT(getArticle(3))
optionT([(a1, a2, a3).some].asList)
check: articles.run == [("1", "2", "3").some].asList
let badArticles = act:
a <- articles
bad <- optionT(getArticle(0))
[[a[0], a[1], a[2], bad].asList.some].asList.optionT
check: badArticles.run == Nil[Option[List[string]]]()
test "Misc functions":
check: string.none.some.optionT.getOrElse("1") == "1".some
check: string.none.some.optionT.getOrElse(() => "1") == "1".some
check: string.none.rightS.optionT.getOrElseF(() => "Error".left(string)) == "Error".left(string)