2014-04-24 19:33:28 +00:00
|
|
|
(defmodule unit-maybe-monad-tests
|
2014-04-24 04:47:45 +00:00
|
|
|
(export all)
|
|
|
|
(import
|
|
|
|
(from lfeunit-util
|
|
|
|
(check-failed-assert 2)
|
|
|
|
(check-wrong-assert-exception 2))))
|
|
|
|
|
|
|
|
(include-lib "deps/lfeunit/include/lfeunit-macros.lfe")
|
2014-04-24 19:33:28 +00:00
|
|
|
(include-lib "include/monads.lfe")
|
2014-04-24 04:47:45 +00:00
|
|
|
|
|
|
|
(deftest bind-nothing
|
|
|
|
(is-equal 'nothing
|
2014-04-24 19:33:28 +00:00
|
|
|
(>>= maybe-monad 'nothing
|
2014-04-24 04:47:45 +00:00
|
|
|
(lambda (x) (+ 5 x)))))
|
|
|
|
|
|
|
|
(deftest bind-nothing-error
|
|
|
|
(is-equal 'nothing
|
2014-04-24 19:33:28 +00:00
|
|
|
(>>= maybe-monad 'nothing
|
2014-04-24 04:47:45 +00:00
|
|
|
(lambda (_) (error 'bad-func)))))
|
|
|
|
|
|
|
|
(deftest bind-five
|
|
|
|
(is-equal 10
|
2014-04-24 19:33:28 +00:00
|
|
|
(>>= maybe-monad (tuple 'just 5)
|
2014-04-24 04:47:45 +00:00
|
|
|
(lambda (x) (+ 5 x)))))
|
|
|
|
|
|
|
|
(deftest bind-fold
|
|
|
|
(is-equal #(just 3)
|
2014-04-24 19:33:28 +00:00
|
|
|
(let ((minc (lambda (x) (return maybe-monad (+ 1 x))))
|
|
|
|
(bind (lambda (f m) (>>= maybe-monad m f))))
|
2014-04-24 04:47:45 +00:00
|
|
|
(lists:foldr bind
|
|
|
|
#(just 0)
|
|
|
|
(list minc
|
|
|
|
minc
|
|
|
|
minc)))))
|
|
|
|
|
|
|
|
(deftest >>
|
|
|
|
(is-equal #(just 3)
|
2014-04-24 19:33:28 +00:00
|
|
|
(>> maybe-monad #(just 5) #(just 3))))
|
2014-04-24 04:47:45 +00:00
|
|
|
|
2014-04-24 13:47:12 +00:00
|
|
|
(deftest do-bindings
|
2014-04-24 04:47:45 +00:00
|
|
|
(is-equal #(just 3)
|
2014-04-24 19:33:28 +00:00
|
|
|
(do maybe-monad
|
|
|
|
(a <- #(just 1))
|
|
|
|
(b <- #(just 2))
|
|
|
|
(return maybe-monad (+ a b)))))
|
2014-04-24 13:47:12 +00:00
|
|
|
|
|
|
|
(deftest do-nobindings
|
|
|
|
(is-equal #(just 3)
|
2014-04-24 19:33:28 +00:00
|
|
|
(do maybe-monad
|
|
|
|
#(just 5)
|
|
|
|
#(just 3))))
|