2014-07-10 01:19:14 +00:00
|
|
|
(defmodule unit-calrissian-maybe-monad-tests
|
2015-05-21 22:31:11 +00:00
|
|
|
(behaviour ltest-unit)
|
|
|
|
(export all))
|
2014-04-24 04:47:45 +00:00
|
|
|
|
2015-05-21 22:31:11 +00:00
|
|
|
(include-lib "ltest/include/ltest-macros.lfe")
|
|
|
|
(include-lib "calrissian/include/monads.lfe")
|
|
|
|
(include-lib "calrissian/include/monad-tests.lfe")
|
2014-04-24 04:47:45 +00:00
|
|
|
|
2014-07-10 02:28:06 +00:00
|
|
|
(test-monad (monad 'maybe))
|
2014-04-25 04:20:10 +00:00
|
|
|
|
2014-04-25 05:53:29 +00:00
|
|
|
(deftest nothing-short-circuits-value
|
2014-04-24 04:47:45 +00:00
|
|
|
(is-equal 'nothing
|
2014-07-10 02:28:06 +00:00
|
|
|
(>>= (monad 'maybe) 'nothing
|
2014-04-24 04:47:45 +00:00
|
|
|
(lambda (x) (+ 5 x)))))
|
|
|
|
|
2014-04-25 05:53:29 +00:00
|
|
|
(deftest nothing-short-circuits-error
|
2014-04-24 04:47:45 +00:00
|
|
|
(is-equal 'nothing
|
2014-07-10 02:28:06 +00:00
|
|
|
(>>= (monad 'maybe) 'nothing
|
2014-04-24 04:47:45 +00:00
|
|
|
(lambda (_) (error 'bad-func)))))
|
|
|
|
|
2014-04-25 05:53:29 +00:00
|
|
|
(deftest fold-increment-value
|
2014-04-24 04:47:45 +00:00
|
|
|
(is-equal #(just 3)
|
2014-07-10 02:28:06 +00:00
|
|
|
(let ((minc (lambda (x) (return (monad 'maybe) (+ 1 x))))
|
|
|
|
(bind (lambda (f m) (>>= (monad 'maybe) m f))))
|
2014-04-24 04:47:45 +00:00
|
|
|
(lists:foldr bind
|
|
|
|
#(just 0)
|
|
|
|
(list minc
|
|
|
|
minc
|
|
|
|
minc)))))
|