-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
with-redefs + wrap-with macros #123
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,19 @@ | ||||||
(ns state-flow.labs.state | ||||||
(:refer-clojure :exclude [with-redefs]) | ||||||
(:require [cats.core :as m] | ||||||
[state-flow.state :as state] | ||||||
[state-flow.core :as state-flow])) | ||||||
|
||||||
(def ^:dynamic *enable-with-redefs-macro* nil) | ||||||
|
||||||
(defmacro with-redefs | ||||||
[bindings flow] | ||||||
(assert *enable-with-redefs-macro* | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure about the usage of this variable. Just a warning in the comments is fine. The flag looks like a handicap to me. |
||||||
"`with-redefs` usage is not recommended. If you know what you're doing and really want to continue, set `*enable-with-redefs-macro*` to true") | ||||||
`(m/do-let | ||||||
[world# (state/get) | ||||||
runner# (state-flow/runner) | ||||||
:let [[ret# state#] (clojure.core/with-redefs ~bindings | ||||||
(runner# ~flow world#))]] | ||||||
(state/put state#) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
(state/return ret#))) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
(ns state-flow.labs.state-test | ||
(:require [clojure.string :as str] | ||
[clojure.test :as t :refer [deftest is testing]] | ||
[state-flow.labs.state :as labs.state])) | ||
|
||
(defn- set-redefs-macro-feature-flag [v f] | ||
(let [old-v labs.state/*enable-with-redefs-macro* | ||
_ (alter-var-root #'labs.state/*enable-with-redefs-macro* (constantly v)) | ||
result (f)] | ||
result)) | ||
|
||
(def with-redefs-macro-enabled (partial set-redefs-macro-feature-flag true)) | ||
(def with-redefs-macro-disabled (partial set-redefs-macro-feature-flag false)) | ||
|
||
(def flow-form `(labs.state/with-redefs [a 1, b 2] some-flow)) | ||
|
||
(deftest with-redefs-feature-flag-test | ||
(testing "if disabled, macroexpansion throws assertion warning user and giving instructions" | ||
(is (thrown? clojure.lang.Compiler$CompilerException | ||
(with-redefs-macro-disabled #(macroexpand flow-form)))) | ||
(is (try (with-redefs-macro-disabled #(macroexpand flow-form)) | ||
(catch clojure.lang.Compiler$CompilerException ex | ||
(is (instance? java.lang.AssertionError (ex-cause ex))) | ||
(is (-> ex ex-cause ex-message | ||
(str/includes? "`with-redefs` usage is not recommended. If you know what you're doing and really want to continue, set `*enable-with-redefs-macro*` to true"))))))) | ||
(testing "if enabled, macroexpansion works as expected" | ||
(is (seq? (with-redefs-macro-enabled #(macroexpand flow-form)))))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would probably go better in another namespace. Maybe
labs.helpers
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought it'd go along with the other state fns, like
wrap-fn
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's fine here, but we should have a docstring in this namespace that indicates that it is experimental and subject to change.