Skip to content

Commit 2682a4a

Browse files
author
dnolen
committed
CLJS-1673: Can't spec instrument a multimethod
fix multimethods case. add tests for multimethod case and multi-arity fn case.
1 parent ceb307a commit 2682a4a

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

src/main/cljs/cljs/spec.cljs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@
291291
(str "Call to " (pr-str v) " did not conform to spec:\n" (with-out-str (explain-out ed)))
292292
ed)))
293293
conformed)))]
294-
(doto
294+
(cond->
295295
(c/fn
296296
[& args]
297297
(if *instrument-enabled*
@@ -305,7 +305,7 @@
305305
(conform! v :fn (:fn specs) {:args cargs :ret cret} args))
306306
ret)))
307307
(apply f args)))
308-
(gobj/extend f))))
308+
(not (instance? MultiFn f)) (doto (gobj/extend f)))))
309309

310310
(defn- macroexpand-check
311311
[v args]

src/test/cljs/cljs/spec_test.cljs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
(ns cljs.spec-test
22
(:require [cljs.spec :as s]
3-
[cljs.test :as test :refer-macros [deftest is]]))
3+
[cljs.test :as test :refer-macros [deftest is run-tests]]))
44

55
(s/def ::even? (s/and number? even?))
66
(s/def ::odd? (s/and number? odd?))
@@ -16,6 +16,33 @@
1616
(let [xs [42 11 13 15 {:a 1 :b 2 :c 3} 1 2 3 42 43 44 11]]
1717
(is (= xs (s/unform s2 (s/conform s2 xs))))))
1818

19+
(defn adder
20+
([a] a)
21+
([a b] (+ a b)))
22+
23+
(s/fdef adder
24+
:args (s/cat :a integer? :b (s/? integer?))
25+
:ret integer?)
26+
27+
(s/instrument #'adder)
28+
29+
(deftest test-multi-arity-instrument
30+
(is (= 1 (adder 1)))
31+
(is (= 3 (adder 1 2)))
32+
(is (thrown? js/Error (adder "foo"))))
33+
34+
(defmulti testmm :type)
35+
(defmethod testmm :default [_])
36+
(defmethod testmm :good [_] "good")
37+
38+
(s/fdef testmm :args (s/cat :m map?) :ret string?)
39+
40+
(s/instrument #'testmm)
41+
42+
(deftest test-multifn-instrument
43+
(is (= "good" (testmm {:type :good})))
44+
(is (thrown? js/Error (testmm "foo"))))
45+
1946
(comment
2047

2148
(s/conform s2 [42 11 13 15 {:a 1 :b 2 :c 3} 1 2 3 42 43 44 11])

0 commit comments

Comments
 (0)