Skip to content

Commit

Permalink
Add before flow hook (#144)
Browse files Browse the repository at this point in the history
* Add before flow hook
  • Loading branch information
sovelten authored Nov 3, 2020
1 parent fbfd046 commit 0c715ee
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 12 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## [5.8.0]

* enables the user to add a hook that is executed before flow execution and after description is updated, via the `before-flow-hook` option

## [5.7.0]

* add `state-flow.api/for` macro [#142](https://github.com/nubank/state-flow/pull/142)
Expand Down
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(defproject nubank/state-flow "5.7.0"
(defproject nubank/state-flow "5.8.0"
:description "Integration testing with composable flows"
:url "https://github.com/nubank/state-flow"
:license {:name "MIT"}
Expand Down
30 changes: 19 additions & 11 deletions src/state_flow/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@
(second pair)]
pair))

(defn apply-before-flow-hook
[]
(m/do-let
[hook (state/gets (comp :before-flow-hook meta))]
(state/modify (or hook identity))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Public API

Expand All @@ -103,6 +109,7 @@
flows' (or flows `[(m/return nil)])]
`(m/do-let
(push-meta ~description ~flow-meta)
(apply-before-flow-hook)
[ret# (m/do-let ~@flows')]
(pop-meta)
(m/return ret#))))
Expand Down Expand Up @@ -230,21 +237,21 @@
Supported keys in the first argument are:
`:fail-fast?` optional, default `false`, when set to `true`, the flow stops running after the first failing assertion
`:init` optional, default (constantly {}), function of no arguments that returns the initial state
`:cleanup` optional, default identity, function of the final state used to perform cleanup, if necessary
`:runner` optional, default `run`, function of a flow and an initial state which will execute the flow
`:on-error` optional, function of the final result pair to be invoked when the first value in the pair
represents an error, default:
`(comp throw-error!
log-error
(filter-stack-trace default-strack-trace-exclusions))`"
[{:keys [init cleanup runner on-error fail-fast?]
`:fail-fast?` optional, default `false`, when set to `true`, the flow stops running after the first failing assertion
`:init` optional, default (constantly {}), function of no arguments that returns the initial state
`:cleanup` optional, default `identity`, function of the final state used to perform cleanup, if necessary
`:runner` optional, default `run`, function of a flow and an initial state which will execute the flow
`:before-flow-hook` optional, default `identity`, function from state to new-state that is applied before excuting a flow, after flow description is updated.
`:on-error` optional, function of the final result pair to be invoked when the first value in the pair represents an error, default:
`(comp throw-error!
log-error
(filter-stack-trace default-strack-trace-exclusions))`"
[{:keys [init cleanup runner on-error fail-fast? before-flow-hook]
:or {init (constantly {})
cleanup identity
runner run
fail-fast? false
before-flow-hook identity
on-error (comp throw-error!
log-error
(filter-stack-trace default-stack-trace-exclusions))}
Expand All @@ -253,6 +260,7 @@
(let [init-state+meta (vary-meta (init)
assoc
:runner runner
:before-flow-hook before-flow-hook
:fail-fast? fail-fast?)
pair (-> flow
(runner init-state+meta)
Expand Down
10 changes: 10 additions & 0 deletions test/state_flow/core_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -311,3 +311,13 @@
(is (empty? (->> (rest frames)
(map #(.getClassName %))
(filter #(re-find #"^clojure.lang" %))))))))

(deftest before-flow-hook
(testing "add a custom before-flow-hook that gets a description and changes the state with a modified version of it"
(let [result (->> (state-flow/run*
{:before-flow-hook (fn [s] (assoc s :my-thing (str (first (clojure.string/split (#'state-flow/state->current-description s)
#" "))
" world!")))}
(state-flow/flow "hello" (state/gets :my-thing)))
first)]
(is (= result "hello world!")))))

0 comments on commit 0c715ee

Please sign in to comment.