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 all 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
39 changes: 39 additions & 0 deletions src/state_flow/labs/state.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
(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.

"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
Copy link
Contributor

Choose a reason for hiding this comment

The 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

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 we should say that this here to support helper macros like with-redefs, and reference that as an implementation example.

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#)))
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#)))


(defmacro with-redefs
"WARNING: `with-redefs` usage is not recommended. Use only if you know what you're
Copy link
Contributor

Choose a reason for hiding this comment

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

Same comment - docstring should also say what this does, and warn that with-redefs is not threadsafe.

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