From e1585f6730e3693770b25b5d7142159dfdebc17c Mon Sep 17 00:00:00 2001 From: Dan Holmsand Date: Wed, 23 Sep 2015 19:40:19 +0200 Subject: [PATCH] Add more with-let tests, and fix dispose bug --- src/reagent/ratom.clj | 11 +++- src/reagent/ratom.cljs | 2 +- test/reagenttest/testwithlet.cljs | 92 ++++++++++++++++++++++++++++++- 3 files changed, 101 insertions(+), 4 deletions(-) diff --git a/src/reagent/ratom.clj b/src/reagent/ratom.clj index 2758871..3769a0e 100644 --- a/src/reagent/ratom.clj +++ b/src/reagent/ratom.clj @@ -45,5 +45,14 @@ res# (do ~@forms)] (if (reagent.ratom/reactive?) (set! (.-destroy destroy-obj#) destroy#) - (destroy#)) + (when (some? destroy#) + (destroy#))) res#))))) + + + + + + + + diff --git a/src/reagent/ratom.cljs b/src/reagent/ratom.cljs index 840ecd7..528d5e9 100644 --- a/src/reagent/ratom.cljs +++ b/src/reagent/ratom.cljs @@ -259,7 +259,7 @@ -;;; with-resource support +;;; with-let support (def reaction-counter 0) diff --git a/test/reagenttest/testwithlet.cljs b/test/reagenttest/testwithlet.cljs index 985e7ec..3ba08d9 100644 --- a/test/reagenttest/testwithlet.cljs +++ b/test/reagenttest/testwithlet.cljs @@ -3,7 +3,8 @@ [reagent.ratom :as rv :refer [track track! dispose!] :refer-macros [with-let]] [reagent.debug :refer-macros [dbg]] - [reagent.core :as r :refer [flush]])) + [reagent.core :as r :refer [flush]] + [clojure.walk :as w])) (defn running [] (r/flush) @@ -23,7 +24,8 @@ (finally (swap! n3 inc)))) r (atom nil) - t (track! (fn [] (reset! r @(track f1))))] + t (track! (fn [] + (reset! r @(track f1))))] (is (= [[10 1] 1 1 0] [@r @n1 @n2 @n3])) (swap! a inc) (is (= [[10 1] 1 1 0] [@r @n1 @n2 @n3])) @@ -41,3 +43,89 @@ (is (= [12 2] @t)) (is (= [[12 2] 2 3 2] [@r @n1 @n2 @n3])) (is (= runs (running))))) + + +(deftest test-with-let-args + (let [runs (running) + n1 (atom 0) + n2 (atom 0) + a (r/atom 0) + ran (fn [] + (swap! n2 inc) + @a) + f1 #(with-let [] + (ran) + []) + f2 #(with-let [x1 (swap! n1 inc)] + (ran) + [x1]) + f3 #(with-let [x1 (swap! n1 inc) + x2 (swap! n1 inc)] + (ran) + [x1 x2]) + f4 #(with-let [x1 (swap! n1 inc) + x2 (swap! n1 inc) + x3 (swap! n1 inc)] + (ran) + [x1 x2 x3]) + f5 #(with-let [x1 (swap! n1 inc) + x2 (swap! n1 inc) + x3 (swap! n1 inc) + x4 (swap! n1 inc)] + (ran) + [x1 x2 x3 x4]) + f6 #(with-let [x1 (swap! n1 inc) + x2 (swap! n1 inc) + x3 (swap! n1 inc) + x4 (swap! n1 inc) + x5 (swap! n1 inc)] + (ran) + [x1 x2 x3 x4 x5]) + f7 #(with-let [x1 (swap! n1 inc) + x2 (swap! n1 inc) + x3 (swap! n1 inc) + x4 (swap! n1 inc) + x5 (swap! n1 inc) + x6 (swap! n1 inc)] + (ran) + [x1 x2 x3 x4 x5 x6]) + r (atom nil) + all (fn [] {:f1 @(track f1) + :f2 @(track f2) + :f3 @(track f3) + :f4 @(track f4) + :f5 @(track f5) + :f6 @(track f6) + :f7 @(track f7)}) + t (track! (fn [] (reset! r (all)))) + expected {:f1 [] + :f2 [1] + :f3 [2 3] + :f4 [4 5 6] + :f5 [7 8 9 10] + :f6 [11 12 13 14 15] + :f7 [16 17 18 19 20 21]}] + (is (< runs (running))) + (is (= @n2 7)) + (is (= @r expected)) + (is (= (all) expected)) + (is (= @t expected)) + (swap! a inc) + (is (= @n2 7)) + (flush) + (is (= @n2 14)) + (is (= @r expected)) + (is (= (all) expected)) + (is (= @t expected)) + (is (= @n2 14)) + (dispose! t) + (is (= runs (running))) + (is (= @r expected)) + (is (= @n2 14)) + (is (= (all) (w/postwalk #(if (number? %) (+ 21 %) %) + expected))) + (is (= @n2 21)) + (is (= @t (w/postwalk #(if (number? %) (+ 42 %) %) + expected))) + (is (= @n2 28)) + (is (= runs (running)))))