Skip to content

Commit

Permalink
v/handle-fn and test watching.
Browse files Browse the repository at this point in the history
  • Loading branch information
hanjos committed Nov 30, 2023
1 parent b07424e commit cc1441b
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 26 deletions.
42 changes: 25 additions & 17 deletions deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,38 @@
:deps {}

:aliases
{:test {:extra-paths ["test"]}
{:test {:extra-paths ["test"]}

;; clojure -X:test:runner
;; clojure -X:test:runner :fail-fast? false
:runner {:extra-deps {lambdaisland/kaocha {:mvn/version "1.87.1366"}
lambdaisland/kaocha-cloverage {:mvn/version "1.1.89"}}
:exec-fn kaocha.runner/exec-fn
:exec-args {:kaocha/reporter [kaocha.report/documentation]
:kaocha/plugins [:kaocha.plugin/profiling
:kaocha.plugin/cloverage]}
:jvm-opts ["-XX:-OmitStackTraceInFastThrow"]}
:runner {:extra-deps {lambdaisland/kaocha {:mvn/version "1.87.1366"}
lambdaisland/kaocha-cloverage {:mvn/version "1.1.89"}}
:exec-fn kaocha.runner/exec-fn
:exec-args {:kaocha/reporter [kaocha.report/documentation]
:kaocha/plugins [:kaocha.plugin/profiling
:kaocha.plugin/cloverage]}
:jvm-opts ["-XX:-OmitStackTraceInFastThrow"]}

;; clojure -X:test:runner:watch
:watch {:exec-args {:watch? true
:skip-meta :slow
:fail-fast? true
:kaocha/reporter [kaocha.report/documentation]
:kaocha/plugins [:kaocha.plugin/profiling
:kaocha.plugin/cloverage]}}

;; clojure -T:build <fn> <opts>
:build {:extra-paths ["src"] ; XXX required for codox
:deps {io.github.clojure/tools.build {:mvn/version "0.9.6"}
codox/codox {:mvn/version "0.10.8"}}
:ns-default build}
:build {:extra-paths ["src"] ; XXX required for codox
:deps {io.github.clojure/tools.build {:mvn/version "0.9.6"}
codox/codox {:mvn/version "0.10.8"}}
:ns-default build}

;; clojure -M:build:test:repl
:repl {:extra-deps {nrepl/nrepl {:mvn/version "1.0.0"}
cider/cider-nrepl {:mvn/version "0.40.0"}}
:main-opts ["-m" "nrepl.cmdline"
"--color" "--interactive"
"--middleware" "[cider.nrepl/cider-middleware]"]}}
:repl {:extra-deps {nrepl/nrepl {:mvn/version "1.0.0"}
cider/cider-nrepl {:mvn/version "0.40.0"}}
:main-opts ["-m" "nrepl.cmdline"
"--color" "--interactive"
"--middleware" "[cider.nrepl/cider-middleware]"]}}

;; XXX required for GitHub Packages
:mvn/repos {"github" {:url "https://maven.pkg.github.com/hanjos/conditio-clj"
Expand Down
37 changes: 28 additions & 9 deletions src/org/sbrubbles/conditio/vars.clj
Original file line number Diff line number Diff line change
Expand Up @@ -101,17 +101,35 @@
(recur (conj (conj ret `(var ~(first vvs))) (second vvs))
(next (next vvs)))
(seq ret))))]
`(let [merge-bindings# (fn [chain-map# bindings#]
(reduce-kv (fn [acc# k# v#]
(assoc acc# k# (if (contains? acc# k#)
(conj (get acc# k#) v#)
(list v#))))
chain-map#
bindings#))]
(binding [*handlers* (merge-bindings# *handlers*
(hash-map ~@(var-ize bindings)))]
`(let [merge# (fn [chain-map# bindings#]
(reduce-kv (fn [acc# k# v#]
(assoc acc# k# (if (contains? acc# k#)
(conj (get acc# k#) v#)
(list v#))))
chain-map#
bindings#))]
(binding [*handlers* (merge# *handlers*
(hash-map ~@(var-ize bindings)))]
~@body))))

(defn handle-fn
"Returns a function, which will install the given handlers (in a
var-function map) and then run `f`.
This may be used to define a helper function which runs on a different
thread, but needs the given handlers in place."
[binding-map f]
(let [merge-bindings (fn [chain-map# bindings#]
(reduce-kv (fn [acc# k# v#]
(assoc acc# k# (if (contains? acc# k#)
(conj (get acc# k#) v#)
(list v#))))
chain-map#
bindings#))]
(with-bindings*
{#'*handlers* (merge-bindings *handlers* binding-map)}
(fn [] (bound-fn* f)))))

(defn restart
"Searches for a restart mapped to `option`, and then runs it with `args`.
Expand Down Expand Up @@ -146,6 +164,7 @@

(defn with-fn
"Returns a function, which will install the given restarts and then run `f`.
This may be used to define a helper function which runs on a different
thread, but needs the given restarts in place."
[binding-map f]
Expand Down
6 changes: 6 additions & 0 deletions test/org/sbrubbles/conditio/vars_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@
(c 1 2 3)
(list 1 2 3))))

(let [c-prime (v/handle-fn {#'c list}
(fn [& args]
(apply c args)))]
(is (= (c-prime 1 2 3)
(list 1 2 3))))

(is (thrown? ExceptionInfo (c 1 2 3))))

(deftest restarting
Expand Down

0 comments on commit cc1441b

Please sign in to comment.