From 56cab635190d568c4d858b0bdd415e7b0dd7364d Mon Sep 17 00:00:00 2001 From: Christian Johansen Date: Sat, 2 Feb 2019 00:17:26 +0100 Subject: [PATCH 1/2] Conform :ret into :fn Fixes #26 --- src/clj/orchestra/spec/test.cljc | 8 ++++---- src/cljs/orchestra_cljs/spec/test.cljs | 8 ++++---- test/cljc/orchestra/core_test.cljc | 12 ++++++++++++ 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/clj/orchestra/spec/test.cljc b/src/clj/orchestra/spec/test.cljc index fa684e9..0426843 100644 --- a/src/clj/orchestra/spec/test.cljc +++ b/src/clj/orchestra/spec/test.cljc @@ -124,13 +124,13 @@ failure in instrument." (if *instrument-enabled* (let [cargs (when-let [spec (:args fn-spec)] (conform! v :args spec args ::s/args)) - ret (.applyTo ^clojure.lang.IFn f args)] - (when-let [spec (:ret fn-spec)] - (conform! v :ret spec ret ::s/ret)) + ret (.applyTo ^clojure.lang.IFn f args) + cret (when-let [spec (:ret fn-spec)] + (conform! v :ret spec ret ::s/ret))] (when-let [spec (:fn fn-spec)] (if (nil? cargs) (throw (no-args-spec v fn-spec)) - (conform! v :fn spec {:ret ret :args cargs} ::s/fn))) + (conform! v :fn spec {:ret cret :args cargs} ::s/fn))) ret) (.applyTo ^clojure.lang.IFn f args))))) diff --git a/src/cljs/orchestra_cljs/spec/test.cljs b/src/cljs/orchestra_cljs/spec/test.cljs index 0f4b1e7..07f6fd5 100644 --- a/src/cljs/orchestra_cljs/spec/test.cljs +++ b/src/cljs/orchestra_cljs/spec/test.cljs @@ -121,13 +121,13 @@ (let [cargs (when (:args fn-spec) (conform! v :args (:args fn-spec) args ::s/args)) ret (binding [*instrument-enabled* true] - (apply' f args))] - (when (:ret fn-spec) - (conform! v :ret (:ret fn-spec) ret ::s/ret)) + (apply' f args)) + cret (when (:ret fn-spec) + (conform! v :ret (:ret fn-spec) ret ::s/ret))] (when-let [spec (:fn fn-spec)] (if (nil? cargs) (throw (no-args-spec v fn-spec)) - (conform! v :fn spec {:ret ret :args cargs} ::s/fn))) + (conform! v :fn spec {:ret cret :args cargs} ::s/fn))) ret)) (apply' f args)))] (when-not pure-variadic? diff --git a/test/cljc/orchestra/core_test.cljc b/test/cljc/orchestra/core_test.cljc index fd311b7..bb1e3d4 100644 --- a/test/cljc/orchestra/core_test.cljc +++ b/test/cljc/orchestra/core_test.cljc @@ -192,3 +192,15 @@ (st/instrument) (f)) (use-fixtures :each instrument-fixture) + +(defn-spec conform-ret-into-fn (s/or :error #{:error} + :success string?) + {:fn #(condp = (-> % :ret key) + :success (and (clojure.string/starts-with? (-> % :ret val) "your-") + (clojure.string/ends-with? (-> % :ret val) (-> % :args :string))) + true)} + [string string?] + (str "your-" string)) + +(deftest conform-ret-into-fn-test + (is (conform-ret-into-fn "Hello"))) From 2b0904cfb7d7b85c5fde25d3ac67395ed2aceed1 Mon Sep 17 00:00:00 2001 From: Christian Johansen Date: Sat, 2 Feb 2019 08:29:01 +0100 Subject: [PATCH 2/2] Ensure there is always a return value for :fn --- src/clj/orchestra/spec/test.cljc | 2 +- src/cljs/orchestra_cljs/spec/test.cljs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/clj/orchestra/spec/test.cljc b/src/clj/orchestra/spec/test.cljc index 0426843..b4f4f6c 100644 --- a/src/clj/orchestra/spec/test.cljc +++ b/src/clj/orchestra/spec/test.cljc @@ -130,7 +130,7 @@ failure in instrument." (when-let [spec (:fn fn-spec)] (if (nil? cargs) (throw (no-args-spec v fn-spec)) - (conform! v :fn spec {:ret cret :args cargs} ::s/fn))) + (conform! v :fn spec {:ret (or cret ret) :args cargs} ::s/fn))) ret) (.applyTo ^clojure.lang.IFn f args))))) diff --git a/src/cljs/orchestra_cljs/spec/test.cljs b/src/cljs/orchestra_cljs/spec/test.cljs index 07f6fd5..7a10ba2 100644 --- a/src/cljs/orchestra_cljs/spec/test.cljs +++ b/src/cljs/orchestra_cljs/spec/test.cljs @@ -127,7 +127,7 @@ (when-let [spec (:fn fn-spec)] (if (nil? cargs) (throw (no-args-spec v fn-spec)) - (conform! v :fn spec {:ret cret :args cargs} ::s/fn))) + (conform! v :fn spec {:ret (or cret ret) :args cargs} ::s/fn))) ret)) (apply' f args)))] (when-not pure-variadic?