Add a test for monitor!

This commit is contained in:
Dan Holmsand 2015-09-14 18:14:13 +02:00
parent 9e9cf93016
commit 8e7624ea45
2 changed files with 38 additions and 0 deletions

View File

@ -152,6 +152,9 @@
{:pre [(ifn? f)]} {:pre [(ifn? f)]}
(Monitor. #(apply f args) [f args] nil)) (Monitor. #(apply f args) [f args] nil))
(defn monitor! [f & args]
(make-reaction #(deref (apply monitor f args))
:auto-run :async))
;;; cursor ;;; cursor

View File

@ -14,6 +14,8 @@
(defn dispose [v] (defn dispose [v]
(rv/dispose! v)) (rv/dispose! v))
(defn sync [] (r/flush))
(enable-console-print!) (enable-console-print!)
@ -41,6 +43,39 @@
(dispose const) (dispose const)
(is (= (running) runs)))) (is (= (running) runs))))
(deftest test-monitor!
(let [runs (running)
start (rv/atom 0)
svf (fn [] @start)
sv (monitor svf)
compf (fn [x] @sv (+ x @sv))
comp (monitor compf 2)
c2f (fn [] (inc @comp))
count (rv/atom 0)
out (rv/atom 0)
resf (fn []
(swap! count inc)
(+ @sv @(monitor c2f) @comp))
res (monitor resf)
const (rv/monitor!
#(reset! out @res))]
(is (= @count 0))
(sync)
(is (= @count 1) "constrain ran")
(is (= @out 5))
(reset! start 1)
(is (= @count 1))
(sync)
(is (= @out 8))
(is (= @count 2))
(dispose const)
(swap! start inc)
(sync)
(is (= @count 2))
(is (= @const 11))
(is (= @count 3))
(is (= (running) runs))))
(deftest double-dependency (deftest double-dependency
(let [runs (running) (let [runs (running)
start (rv/atom 0) start (rv/atom 0)