-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #123 from caioaao/devel
with-redefs + wrap-with macros
- Loading branch information
Showing
2 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
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#))) | ||
|
||
(defmacro with-redefs | ||
"WARNING: `with-redefs` usage is not recommended. Use only if you know what you're | ||
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {}))))) |