Skip to content
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

Merged
merged 6 commits into from
May 21, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/state_flow/labs/state.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
(ns state-flow.labs.state
Copy link
Collaborator

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

Copy link
Contributor Author

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

Copy link
Contributor

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.

(: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*
Copy link
Collaborator

Choose a reason for hiding this comment

The 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#)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
(state/put state#)
(state-flow.api/swap-state (constantly state#))

(state/return ret#)))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
(state/return ret#)))
(state-flow.api/return ret#)))

27 changes: 27 additions & 0 deletions test/state_flow/labs/state_test.clj
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))))))