-
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 all 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,39 @@ | ||||||
(ns state-flow.labs.state | ||||||
"WARNING: This API is experimental and subject to changes." | ||||||
(:refer-clojure :exclude [with-redefs]) | ||||||
(:require [cats.core :as m] | ||||||
state-flow.api | ||||||
[state-flow.core :as state-flow] | ||||||
[state-flow.state :as state])) | ||||||
|
||||||
(defmacro wrap-with | ||||||
"WARNING: `wrap-with` usage is not recommended. Use only if you know what you're | ||||||
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. This warning is fine, but we should also say what it does :-D 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. I think we should say that this here to support helper macros like |
||||||
doing and you are sure you can't achieve the same result without it. | ||||||
|
||||||
Wraps the provided state-flow execution. `wrapper-fn` will be passed a | ||||||
function that will run the flow when called." | ||||||
[wrapper-fn flow] | ||||||
`(m/do-let | ||||||
[world# (state/get) | ||||||
runner# (state-flow/runner) | ||||||
:let [[ret# state#] (~wrapper-fn (fn [] (runner# ~flow world#)))]] | ||||||
(state-flow.api/swap-state (constantly state#)) | ||||||
(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
|
||||||
|
||||||
(defmacro with-redefs | ||||||
"WARNING: `with-redefs` usage is not recommended. Use only if you know what you're | ||||||
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. Same comment - docstring should also say what this does, and warn that |
||||||
doing and you are sure you can't achieve the same result without it. | ||||||
|
||||||
Wraps the provided state-flow execution with `clojure.core/with-redefs` | ||||||
macro, e.g. | ||||||
|
||||||
(defn now [] (java.util.Date.)) | ||||||
(def flow-with-trapped-time | ||||||
(labs.state/with-redefs | ||||||
[now (constantly #inst \"2018-01-01\")] | ||||||
(flow \"a flow in 2018\" | ||||||
...)))" | ||||||
[bindings flow] | ||||||
`(wrap-with | ||||||
(fn [f#] (clojure.core/with-redefs ~bindings (f#))) | ||||||
~flow)) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
(ns state-flow.labs.state-test | ||
(:require [clojure.test :as t :refer [deftest is testing]] | ||
[matcher-combinators.test :refer [match?]] | ||
[state-flow.core :as state-flow] | ||
[state-flow.labs.state :as labs.state] | ||
[state-flow.state :as state])) | ||
|
||
(defn put2 [w] (assoc w :value 2)) | ||
(defn put3 [w] (assoc w :value 3)) | ||
|
||
(defn wrap-with-redefs [f] | ||
(with-redefs [put2 put3] | ||
(f))) | ||
|
||
(defn called-it-callback [f] | ||
(print "called it") | ||
(f)) | ||
|
||
(deftest wrap-with-test | ||
(testing "wrapper is called" | ||
(is (= "called it" | ||
(with-out-str | ||
(-> (labs.state/wrap-with | ||
(fn [f] (print "called it") (f)) | ||
(state/modify put2)) | ||
(state-flow/run {})))))) | ||
(testing "flow runs successfully" | ||
(is (match? [{} {:value 2}] | ||
(-> (labs.state/wrap-with | ||
(fn [f] (f)) | ||
(state/modify put2)) | ||
(state-flow/run {})))))) | ||
|
||
(deftest with-redefs-test | ||
(is (match? | ||
[{} {:value 3}] | ||
(-> (labs.state/with-redefs [put2 put3] | ||
(state/modify put2)) | ||
(state-flow/run {}))))) |
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.