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

Issue 1063 qregex cljs #14

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
81 changes: 47 additions & 34 deletions src/malli/instrument.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -26,53 +26,66 @@
(defn -filter-var [f] (fn [n s d] (f (Var. (constantly (-find-var n s)) (symbol n s) d))))
(defn -filter-schema [f] (fn [_ _ {:keys [schema]}] (f schema)))

(defn -arity->schema
[fn-schema]
(into {} (map (fn [schema] [(:arity (m/-function-info (m/schema schema))) schema])
(rest fn-schema))))

(defn -variadic? [f] (g/get f "cljs$core$IFn$_invoke$arity$variadic"))
(defn -max-fixed-arity [f] (g/get f "cljs$lang$maxFixedArity"))
(defn -pure-variadic? [f]
(let [max-fixed-arity (-max-fixed-arity f)]
(and max-fixed-arity (-variadic? f)
(every? #(not (fn? (g/get f (str "cljs$core$IFn$_invoke$arity$" %)))) (range 20)))))
(not-any? #(fn? (g/get f (str "cljs$core$IFn$_invoke$arity$" %))) (range 20)))))

(defn -replace-variadic-fn [original-fn n s opts]
(let [accessor "cljs$core$IFn$_invoke$arity$variadic"
arity-fn (g/get original-fn accessor)]
(when arity-fn
(defn -replace-variadic-arity [original-fn min-fixed n s opts]
(let [accessor "cljs$core$IFn$_invoke$arity$variadic"]
(when-some [arity-fn (g/get original-fn accessor)]
(g/set original-fn "malli$instrument$instrumented?" true)
;; the shape of the argument in the following apply calls are needed to match the call style of the cljs compiler
;; so the user's function gets the arguments as expected
(let [max-fixed-arity (-max-fixed-arity original-fn)
instrumented-variadic-fn (m/-instrument opts (fn [& args]
(let [[fixed-args rest-args] (split-at max-fixed-arity (vec args))
final-args (into (vec fixed-args) [(not-empty rest-args)])]
(apply arity-fn final-args))))
instrumented-wrapper (fn [& args]
(let [[fixed-args rest-args] (split-at max-fixed-arity (vec args))
final-args (vec (apply list* (into (vec fixed-args) (not-empty rest-args))))]
(apply instrumented-variadic-fn final-args)))]
(g/set instrumented-wrapper "malli$instrument$original" arity-fn)
(g/set (-get-prop n s) "malli$instrument$instrumented?" true)
(g/set (-get-prop n s) accessor instrumented-wrapper)
(g/set (-get-ns n) s (meta-fn original-fn {:instrumented-symbol (symbol n s)}))))))
(let [max-fixed-arity (-max-fixed-arity original-fn)]
(when (<= min-fixed max-fixed-arity)
(let [instrumented-variadic-fn (m/-instrument opts (fn [& args]
(let [[fixed-args rest-args] (split-at max-fixed-arity (vec args))
final-args (into (vec fixed-args) [(not-empty rest-args)])]
(apply arity-fn final-args))))
instrumented-wrapper (fn [& args]
(let [[fixed-args rest-args] (split-at max-fixed-arity (vec args))
final-args (vec (apply list* (into (vec fixed-args) (not-empty rest-args))))]
(apply instrumented-variadic-fn final-args)))]
(g/set instrumented-wrapper "malli$instrument$original" arity-fn)
(g/set (-get-prop n s) "malli$instrument$instrumented?" true)
(g/set (-get-prop n s) accessor instrumented-wrapper)
(g/set (-get-ns n) s (meta-fn original-fn {:instrumented-symbol (symbol n s)}))
:varargs))))))

(defn -replace-variadic-fn [original-fn n s opts]
(-replace-variadic-arity original-fn n s opts))

(defn -replace-fixed-arity [original-fn arity n s opts]
(let [accessor (str "cljs$core$IFn$_invoke$arity$" arity)]
(when-some [arity-fn (g/get original-fn accessor)]
(let [instrumented-fn (m/-instrument (assoc opts :schema f-schema) arity-fn)]
(g/set instrumented-fn "malli$instrument$original" arity-fn)
(g/set instrumented-fn "malli$instrument$instrumented?" true)
(g/set (-get-prop n s) accessor instrumented-fn))
arity)))

(defn -replace-multi-arity [original-fn n s opts]
(let [schema (:schema opts)]
(let [schema (:schema opts)
replaced (atom #{})]
(g/set original-fn "malli$instrument$instrumented?" true)
(g/set (-get-ns n) s (meta-fn original-fn {:instrumented-symbol (symbol n s)}))
(doseq [[arity f-schema] (-arity->schema schema)]
(if (= arity :varargs)
(-replace-variadic-fn original-fn n s opts)
(let [accessor (str "cljs$core$IFn$_invoke$arity$" arity)
arity-fn (g/get original-fn accessor)]
(when arity-fn
(let [instrumented-fn (m/-instrument (assoc opts :schema f-schema) arity-fn)]
(g/set instrumented-fn "malli$instrument$original" arity-fn)
(g/set instrumented-fn "malli$instrument$instrumented?" true)
(g/set (-get-prop n s) accessor instrumented-fn))))))))
(doseq [f-schema (reverse (rest schema)) ;; instrument most general :=> specs first
{:keys [arity min max]} (m/-function-info (m/schema f-schema))]
;; not quite right. technically a fixed arity schema can be implemented with a varargs arity.
(when-some [max (or max (-max-fixed-arity original-fn))]
(doseq [arity (range min (inc max))]
(when-not (@replaced arity)
(when-some [arity (or (-replace-fixed-arity original-fn arity n s opts)
(-replace-variadic-arity original-fn n s opts))]
(swap! replaced conj arity)))))
(when (and (= arity :varargs)
(not max))
(when-not (@replaced arity)
(when (-replace-variadic-arity original-fn n s opts)
(swap! replaced conj arity)))))))

(defn -replace-fn [original-fn n s opts]
(try
Expand Down
3 changes: 2 additions & 1 deletion test/malli/generator_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -1051,7 +1051,8 @@
{:size 100}))))
(is (empty? (remove #(<= 2 (count %) 3)
(mg/sample [:map-of {:min 2 :max 3} [:enum 1 2 3] :any]
{:size 100})))))
{:size 250
:seed 0})))))

(deftest such-that-generator-failure-test
(is (thrown-with-msg?
Expand Down
23 changes: 21 additions & 2 deletions test/malli/instrument_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@

(defn ->minus [] minus)

(defn myfn-b
{:malli/schema [:=> [:cat :int [:? :int] :string] :keyword]}
([a b] :res3)
([a b c] :res4))

(mi/collect!)

(deftest collect!-test
Expand All @@ -48,7 +53,11 @@
(is (thrown?
ClassCastException
((->minus) "2")))
(is (= 5 ((->minus) 6))))
(is (= 5 ((->minus) 6)))
(is (= :res3 (myfn-b 1 2)))
(is (= :res3 (myfn-b 1 "a")))
(is (= :res4 (myfn-b 1 2 3)))
(is (= :res4 (myfn-b 1 2 "a"))))

(testing "with instrumentation"
(instrument!)
Expand All @@ -59,7 +68,17 @@
(is (thrown-with-msg?
Exception
#":malli.core/invalid-output"
((->minus) 6)))))
((->minus) 6)))
(is (= :res3 (myfn-b 1 "a")))
(is (thrown-with-msg?
Exception
#":malli.core/invalid-input"
(myfn-b 1 2)))
(is (= :res4 (myfn-b 1 2 "a")))
(is (thrown-with-msg?
Exception
#":malli.core/invalid-input"
(myfn-b 1 2 3)))))

(defn f1
"accumulated schema from arities"
Expand Down
38 changes: 36 additions & 2 deletions test/malli/instrument_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@
:malli/scope #{:input :output}}
[x] (dec x))

(defn myfn-b
{:malli/schema [:=> [:cat :int [:? :int] :string] :keyword]}
([a b] :res3)
([a b c] :res4))

(defn fixed-with-variadic
{:malli/schema [:function
[:=> [:cat :int] :keyword]
[:=> [:cat :int :int] :keyword]
[:=> [:cat :int :int :int] :keyword]
[:=> [:cat :int :int :int [:* :int]] :keyword]]}
[& xs] :fixed-with-variadic)

(defn multi-arity-fn
{:malli/schema
[:function
Expand Down Expand Up @@ -107,7 +120,23 @@
(is (thrown-with-msg? js/Error #":malli.core/invalid-output" (schemas/power-full 6)))

(is (thrown-with-msg? js/Error #":malli.core/invalid-input" (schemas/power-int? "2")))
(is (thrown-with-msg? js/Error #":malli.core/invalid-output" (schemas/power-int? 6))))
(is (thrown-with-msg? js/Error #":malli.core/invalid-output" (schemas/power-int? 6)))

(is (= :res3 (myfn-b 1 "a")))
(is (thrown-with-msg? js/Error #":malli.core/invalid-input" (myfn-b 1 2)))
(is (= :res4 (myfn-b 1 2 "a")))
(is (thrown-with-msg? js/Error #":malli.core/invalid-input" (myfn-b 1 2 3)))

(is (= :fixed-with-variadic (fixed-with-variadic 1)))
(is (thrown-with-msg? js/Error #":malli.core/invalid-input" (fixed-with-variadic "a")))
(is (= :fixed-with-variadic (fixed-with-variadic 1 2)))
(is (thrown-with-msg? js/Error #":malli.core/invalid-input" (fixed-with-variadic 1 "a")))
(is (= :fixed-with-variadic (fixed-with-variadic 1 2 3)))
(is (thrown-with-msg? js/Error #":malli.core/invalid-input" (fixed-with-variadic 1 2 "a")))
(is (= :fixed-with-variadic (fixed-with-variadic 1 2 3 4)))
(is (thrown-with-msg? js/Error #":malli.core/invalid-input" (fixed-with-variadic 1 2 3 "a")))
(is (= :fixed-with-variadic (fixed-with-variadic 1 2 3 4 5)))
(is (thrown-with-msg? js/Error #":malli.core/invalid-input" (fixed-with-variadic 1 2 3 4 "a"))))

(testing "without instrumentation"
(mi/unstrument! {:filters [(mi/-filter-ns 'malli.instrument-test 'malli.instrument.fn-schemas)]})
Expand Down Expand Up @@ -140,7 +169,12 @@
(is (= 36 (schemas/power-arg-ns 6)))

(is (= 4 (schemas/power-full "2")))
(is (= 36 (schemas/power-full 6)))))
(is (= 36 (schemas/power-full 6)))

(is (= :res3 (myfn-b 1 "a")))
(is (= :res3 (myfn-b 1 2)))
(is (= :res4 (myfn-b 1 2 "a")))
(is (= :res4 (myfn-b 1 2 3)))))

(deftest ^:simple collect!-test

Expand Down
Loading