2014-07-10 01:19:14 +00:00
|
|
|
(defmodule unit-calrissian-state-transformer-tests
|
2014-08-22 02:40:41 +00:00
|
|
|
(behaviour ltest-unit)
|
2014-05-12 04:12:26 +00:00
|
|
|
(export all)
|
|
|
|
(import
|
2014-08-22 02:40:41 +00:00
|
|
|
(from ltest
|
2014-05-12 04:12:26 +00:00
|
|
|
(check-failed-assert 2)
|
|
|
|
(check-wrong-assert-exception 2))))
|
|
|
|
|
2014-08-22 02:40:41 +00:00
|
|
|
(include-lib "deps/ltest/include/ltest-macros.lfe")
|
2014-05-12 04:12:26 +00:00
|
|
|
(include-lib "include/monads.lfe")
|
2014-07-09 05:35:18 +00:00
|
|
|
(include-lib "include/monad-tests.lfe")
|
|
|
|
|
2014-07-10 02:28:06 +00:00
|
|
|
(test-monad (transformer 'state 'identity))
|
2014-05-12 04:12:26 +00:00
|
|
|
|
|
|
|
(deftest eval
|
|
|
|
(is-equal 5
|
2014-07-10 02:28:06 +00:00
|
|
|
(let* ((m (transformer 'state 'identity))
|
2014-05-12 04:12:26 +00:00
|
|
|
(mval (call m 'return 5)))
|
|
|
|
(call m 'eval mval 'undefined))))
|
|
|
|
|
|
|
|
(deftest exec-unchanged
|
|
|
|
(is-equal 'foo
|
2014-07-10 02:28:06 +00:00
|
|
|
(let* ((m (transformer 'state 'identity))
|
2014-05-12 04:12:26 +00:00
|
|
|
(mval (call m 'return 5)))
|
|
|
|
(call m 'exec mval 'foo))))
|
|
|
|
|
2014-05-13 23:31:15 +00:00
|
|
|
(deftest exec-modify
|
2014-05-12 04:12:26 +00:00
|
|
|
(is-equal 10
|
2014-07-10 02:28:06 +00:00
|
|
|
(let ((m (transformer 'state 'identity)))
|
2014-05-12 04:12:26 +00:00
|
|
|
(call m 'exec
|
|
|
|
(do-m m
|
|
|
|
(call m 'modify (lambda (x) (* x 2))))
|
|
|
|
5))))
|
|
|
|
|
2014-05-13 23:40:37 +00:00
|
|
|
(deftest exec-put-and-modify
|
2014-05-13 23:31:15 +00:00
|
|
|
(is-equal 30
|
2014-07-10 02:28:06 +00:00
|
|
|
(let ((m (transformer 'state 'identity)))
|
2014-05-13 23:31:15 +00:00
|
|
|
(call m 'exec
|
|
|
|
(do-m m
|
2014-05-13 23:40:37 +00:00
|
|
|
(call m 'put 10)
|
2014-05-13 23:31:15 +00:00
|
|
|
(call m 'modify (lambda (x) (+ x 5)))
|
|
|
|
(call m 'modify (lambda (x) (* x 2)))
|
|
|
|
(call m 'return 123))
|
2014-05-13 23:40:37 +00:00
|
|
|
3))))
|
|
|
|
|
|
|
|
(deftest exec-bind-and-modify
|
|
|
|
(is-equal 16
|
2014-07-10 02:28:06 +00:00
|
|
|
(let ((m (transformer 'state 'identity)))
|
2014-05-13 23:40:37 +00:00
|
|
|
(call m 'exec
|
|
|
|
(do-m m
|
|
|
|
(a <- (call m 'modify-and-return (lambda (x) (+ x 5))))
|
|
|
|
(call m 'modify (lambda (x) (+ x a))))
|
|
|
|
3))))
|
2014-05-13 23:31:15 +00:00
|
|
|
|
2014-05-12 04:12:26 +00:00
|
|
|
(deftest exec-fail
|
|
|
|
(is-throw #(error value)
|
2014-07-10 02:28:06 +00:00
|
|
|
(let ((m (transformer 'state 'identity)))
|
2014-05-12 04:12:26 +00:00
|
|
|
(call m 'exec
|
|
|
|
(call m 'fail 'value)
|
|
|
|
'undefined))))
|