calrissian/test/unit/unit-maybe-monad-tests.lfe

33 lines
983 B
Text
Raw Normal View History

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-25 04:20:10 +00:00
(include-lib "include/monad-tests.lfe")
2014-04-24 04:47:45 +00:00
2014-04-25 05:06:19 +00:00
(test-monad maybe-monad)
2014-04-25 04:20:10 +00:00
(deftest nothing-short-circuits-value
2014-04-24 04:47:45 +00:00
(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 nothing-short-circuits-error
2014-04-24 04:47:45 +00:00
(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 fold-increment-value
2014-04-24 04:47:45 +00:00
(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)))))