aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/data/maybe.lux
diff options
context:
space:
mode:
Diffstat (limited to 'stdlib/source/test/lux/data/maybe.lux')
-rw-r--r--stdlib/source/test/lux/data/maybe.lux69
1 files changed, 69 insertions, 0 deletions
diff --git a/stdlib/source/test/lux/data/maybe.lux b/stdlib/source/test/lux/data/maybe.lux
new file mode 100644
index 000000000..eb09491a1
--- /dev/null
+++ b/stdlib/source/test/lux/data/maybe.lux
@@ -0,0 +1,69 @@
+(.module:
+ [lux #*
+ [control
+ ["M" monad (#+ Monad do)]
+ pipe]
+ [data
+ ["&" maybe ("&/." monoid)]
+ ["." text ("text/." monoid)]]
+ ["." io ("io/." monad)]]
+ lux/test)
+
+(context: "Maybe"
+ (let [(^open "&/.") &.apply
+ (^open "&/.") &.monad
+ (^open "&/.") (&.equivalence text.equivalence)]
+ ($_ seq
+ (test "Can compare Maybe values."
+ (and (&/= #.None #.None)
+ (&/= (#.Some "yolo") (#.Some "yolo"))
+ (not (&/= (#.Some "yolo") (#.Some "lol")))
+ (not (&/= (#.Some "yolo") #.None))))
+
+ (test "Monoid respects Maybe."
+ (and (&/= #.None &/identity)
+ (&/= (#.Some "yolo") (&/compose (#.Some "yolo") (#.Some "lol")))
+ (&/= (#.Some "yolo") (&/compose (#.Some "yolo") #.None))
+ (&/= (#.Some "lol") (&/compose #.None (#.Some "lol")))
+ (&/= #.None (: (Maybe Text) (&/compose #.None #.None)))))
+
+ (test "Functor respects Maybe."
+ (and (&/= #.None (&/map (text/compose "yolo") #.None))
+ (&/= (#.Some "yololol") (&/map (text/compose "yolo") (#.Some "lol")))))
+
+ (test "Apply respects Maybe."
+ (and (&/= (#.Some "yolo") (&/wrap "yolo"))
+ (&/= (#.Some "yololol")
+ (&/apply (&/wrap (text/compose "yolo")) (&/wrap "lol")))))
+
+ (test "Monad respects Maybe."
+ (&/= (#.Some "yololol")
+ (do &.monad
+ [f (wrap text/compose)
+ a (wrap "yolo")
+ b (wrap "lol")]
+ (wrap (f a b)))))
+
+ (do r.monad
+ [default r.nat
+ maybe r.nat]
+ (_.test "Can have defaults for Maybe values."
+ (and (is? default (maybe.default default
+ #.None))
+
+ (is? maybe (maybe.default default
+ (#.Some maybe))))))
+ )))
+
+(context: "Monad transformer"
+ (let [lift (&.lift io.monad)]
+ (test "Can add maybe functionality to any monad."
+ (|> (io.run (do (&.MaybeT io.monad)
+ [a (lift (io/wrap +123))
+ b (wrap +456)]
+ (wrap (i/+ a b))))
+ (case> (#.Some +579)
+ #1
+
+ _
+ #0)))))