diff --git a/src/mount/core.cljc b/src/mount/core.cljc index ea125cc..95549dd 100644 --- a/src/mount/core.cljc +++ b/src/mount/core.cljc @@ -1,6 +1,7 @@ (ns mount.core #?(:clj (:require [mount.tools.macro :refer [on-error throw-runtime] :as macro] [mount.tools.logger :refer [log]] + [clojure.set :refer [intersection]] [clojure.string :as s]) :cljs (:require [mount.tools.macro :as macro] [mount.tools.logger :refer [log]])) @@ -250,11 +251,16 @@ ;; composable set of states +(defn- mapset [f xs] + (-> (map f xs) + set)) + (defn only ([states] - states) + (only (find-all-states) states)) ([states these] - (filter these states))) + (intersection (mapset var-to-str these) + (mapset var-to-str states)))) (defn with-args ([args] @@ -267,7 +273,8 @@ ([states] (except (find-all-states) states)) ([states these] - (remove these states))) + (remove (mapset var-to-str these) + (mapset var-to-str states)))) (defn swap ([with] @@ -287,13 +294,6 @@ (var-to-str to) :state)) states)) -#_(-> (only #{1 2 3 4}) - (with-args {}) - (except #{2 1}) - (swap {2 42 1 34}) - (swap-states {4 "#'foo.bar/42"}) - (start)) - ;; explicit, not composable (subject to depreciate?) (defn stop-except [& states] diff --git a/test/core/mount/test/composable_fns.cljc b/test/core/mount/test/composable_fns.cljc new file mode 100644 index 0000000..43703a0 --- /dev/null +++ b/test/core/mount/test/composable_fns.cljc @@ -0,0 +1,90 @@ +(ns mount.test.composable-fns + (:require + #?@(:cljs [[cljs.test :as t :refer-macros [is are deftest testing use-fixtures]] + [clojure.set :refer [intersection]] + [mount.core :refer [only except swap swap-states with-args] :as mount :refer-macros [defstate]] + [tapp.websockets :refer [system-a]] + [tapp.conf :refer [config]] + [tapp.audit-log :refer [log]]] + :clj [[clojure.test :as t :refer [is are deftest testing use-fixtures]] + [clojure.set :refer [intersection]] + [mount.core :as mount :refer [defstate only except swap swap-states with-args]] + [tapp.conf :refer [config]] + [tapp.nyse :refer [conn]] + [tapp.example :refer [nrepl]]]) + [mount.test.helper :refer [dval helper]])) + +#?(:clj (alter-meta! *ns* assoc ::load false)) + +(defstate test-conn :start 42 + :stop (constantly 0)) + +(defstate test-nrepl :start []) + +#?(:clj + (deftest only-states + + (testing "only should only return given states. + if source set of states is not provided, it should use all the states to select from" + (is (= #{"#'mount.test.composable-fns/test-conn" "#'tapp.example/nrepl" "#'tapp.nyse/conn"} + (only #{"#'is.not/here" #'mount.test.composable-fns/test-conn #'tapp.example/nrepl #'tapp.nyse/conn})))) + + (testing "only should only return given states" + (is (= #{"#'mount.test.composable-fns/test-conn" "#'tapp.example/nrepl"} + (only [#'mount.test.composable-fns/test-conn #'tapp.example/nrepl #'tapp.nyse/conn] + #{"#'is.not/here" #'mount.test.composable-fns/test-conn #'tapp.example/nrepl})))))) + +#?(:clj + (deftest except-states + + (testing "except should exclude given states. + if source set of states is not provided, it should use all the states to exclude from" + (let [states (except #{"#'is.not/here" #'tapp.example/nrepl #'tapp.nyse/conn})] + (is (coll? states)) + (is (pos? (count states))) + (is (zero? (count (intersection (set states) + #{"#'tapp.example/nrepl" "#'tapp.nyse/conn" "#'is.not/here"})))))) + + (testing "except should exclude given states" + (is (= #{"#'tapp.conf/config" "#'mount.test.composable-fns/test-conn"} + (set (except #{#'tapp.example/nrepl #'tapp.conf/config #'mount.test.composable-fns/test-conn} + #{"#'is.not/here" #'tapp.example/nrepl #'tapp.nyse/conn}))))))) + +#?(:clj + (deftest states-with-args + + (testing "with-args should set args and return all states if none provided" + (let [states (with-args {:a 42})] + (is (= {:a 42} (mount/args))) + (is (= states (#'mount.core/find-all-states))))) + + (testing "with-args should set args and thread states if provided" + (let [t-states #{"#'is.not/here" #'mount.test.composable-fns/test-conn #'tapp.example/nrepl #'tapp.nyse/conn} + states (with-args t-states {:a 42})] + (is (= {:a 42} (mount/args))) + (is (= states t-states)))))) + +#?(:clj + (deftest swap-states-with-values + + (testing "swap should swap states with values and return all states if none is given" + (let [states (swap {#'tapp.nyse/conn "conn-sub" + #'tapp.example/nrepl :nrepl-sub})] + (is (= states (#'mount.core/find-all-states))) + (mount/start) + (is (map? (dval config))) + (is (= :nrepl-sub (dval nrepl))) + (is (= "conn-sub" (dval conn))) + (mount/stop))) + + (testing "swap should swap states with values and return only states that it is given" + (let [t-states #{"#'is.not/here" #'mount.test.composable-fns/test-conn #'tapp.example/nrepl #'tapp.nyse/conn} + states (swap t-states {#'tapp.nyse/conn "conn-sub" + #'tapp.example/nrepl :nrepl-sub})] + (is (= states t-states)) + (mount/start) + (is (map? (dval config))) + (is (= :nrepl-sub (dval nrepl))) + (is (= "conn-sub" (dval conn))) + (is (= 42 (dval test-conn))) + (mount/stop)))))