-
Notifications
You must be signed in to change notification settings - Fork 20
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
bypass fn #24
base: master
Are you sure you want to change the base?
bypass fn #24
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 |
---|---|---|
|
@@ -21,10 +21,12 @@ | |
true) | ||
|
||
(defn ^:private build-wrapped-fn | ||
[record-fn {:keys [var arg-transformer arg-key-fn recordable? return-transformer] | ||
[record-fn {:keys [var arg-transformer arg-key-fn recordable? return-transformer middleware] | ||
:or {arg-transformer vector | ||
arg-key-fn vector | ||
recordable? (constantly true) | ||
middleware (fn [f] | ||
(fn [& args] (apply f args))) | ||
return-transformer identity}}] | ||
(let [orig-fn (deref var) | ||
the-var-name (var-name var) | ||
|
@@ -33,7 +35,7 @@ | |
(if-not (and *recording?* (apply recordable? args*)) | ||
(apply orig-fn args*) | ||
(let [res (binding [*recording?* false] | ||
(return-transformer (apply orig-fn args*))) | ||
(return-transformer (apply (middleware orig-fn) args*))) | ||
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. If you want to preview a response body in a web browser, I'm thinking you'll need the middleware to be applied after the 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. having the middleware see the transformed return value should also make it more consistent with the way it's called during playback. |
||
call {:var-name the-var-name | ||
:arg-key (apply arg-key-fn args*) | ||
:return res}] | ||
|
@@ -94,19 +96,22 @@ | |
(let [the-playbacker (playbacker cassette :key) | ||
redeffings | ||
(into {} | ||
(for [{:keys [var arg-transformer arg-key-fn recordable?] | ||
(for [{:keys [var arg-transformer arg-key-fn recordable? middleware] | ||
:or {arg-transformer vector | ||
arg-key-fn vector | ||
middleware (fn [f] | ||
(fn [& args] (apply f args))) | ||
recordable? (constantly true)}} | ||
specs | ||
:let [orig (deref var) | ||
the-var-name (var-name var) | ||
wrapped (fn [& args] | ||
(let [args* (apply arg-transformer args)] | ||
(if (apply recordable? args*) | ||
(let [k (apply arg-key-fn args*)] | ||
(:return (the-playbacker the-var-name k))) | ||
(apply orig args*))))]] | ||
(let [k (apply arg-key-fn args*) | ||
res (:return (the-playbacker the-var-name k))] | ||
(apply (middleware (fn [& args] res)) args*)) | ||
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 the middleware could be applied to the real playback function rather than an artificial function via something like (apply (middleware (fn [& args] (:return (the-playbacker the-var-name (apply arg-key-fn args)))))) |
||
(apply (middleware orig) args*))))]] | ||
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'm not sure I would expect the |
||
[var (add-meta-from wrapped orig)]))] | ||
(with-redefs-fn redeffings func))) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -155,3 +155,32 @@ | |
(.join)) | ||
(is (= 2 (count (calls increment)))) | ||
(is (= 100 @p)))))) | ||
|
||
|
||
(deftest middleware-test | ||
(defn args-test [args] | ||
(is (= '(2 3) args))) | ||
|
||
(defn res-test [res] | ||
(is (= 5 res))) | ||
|
||
(defn sample-middleware [vcr-clj-fn] | ||
(fn [& args] | ||
(args-test args) | ||
(let [res (apply vcr-clj-fn args)] | ||
(res-test res) | ||
res))) | ||
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. could you either move these |
||
|
||
(with-spy [plus sample-middleware args-test res-test] | ||
(with-cassette :middlewared [{:var #'plus :middleware sample-middleware}] | ||
(is (= 5 (plus 2 3))) | ||
(is (= 1 (count (calls plus)))) | ||
(is (= 1 (count (calls sample-middleware)))) | ||
(is (= 1 (count (calls args-test)))) | ||
(is (= 1 (count (calls res-test))))) | ||
(with-cassette :middlewared [{:var #'plus :middleware sample-middleware}] | ||
(is (= 5 (plus 2 3))) | ||
(is (= 1 (count (calls plus)))) | ||
(is (= 2 (count (calls sample-middleware)))) | ||
(is (= 2 (count (calls args-test)))) | ||
(is (= 2 (count (calls res-test))))))) |
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 can be simplified to
identity