From b0e7d64b723daa371d1fd87edd74310f06d0bbfe Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Wed, 16 Sep 2020 21:58:52 +0200 Subject: [PATCH] [#406] Tests for multi-arity protocols and multimethods --- test/sci/multimethods_test.cljc | 21 +++++++++++++++++++++ test/sci/protocols_test.cljc | 12 +++++++++--- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/test/sci/multimethods_test.cljc b/test/sci/multimethods_test.cljc index abcef680..789be35b 100644 --- a/test/sci/multimethods_test.cljc +++ b/test/sci/multimethods_test.cljc @@ -31,3 +31,24 @@ (defmethod bar [::shape ::rect] [x y] :shape-rect) (prefer-method bar [::rect ::shape] [::shape ::rect]) (bar ::rect ::rect)")))) + +(deftest multi-arity-test + (is (= [:default :one :two :three :more] + (eval* " +(defmulti foo (fn [x & _] x)) + +(defmethod foo :default [_ & _] :default) + +;; Like a standar multi-arity function +(defmethod foo :bar + ([_ _] :one) + ([_ _ _] :two) + ([_ _ _ _] :three) + ([_ _ _ _ & more] :more)) + +[(foo :baz 1) + (foo :bar 1) + (foo :bar 1 2) + (foo :bar 1 2 3) + (foo :bar 1 2 3 4)] +")))) diff --git a/test/sci/protocols_test.cljc b/test/sci/protocols_test.cljc index 0e925507..fc19a8f9 100644 --- a/test/sci/protocols_test.cljc +++ b/test/sci/protocols_test.cljc @@ -117,9 +117,15 @@ (let [prog " (defprotocol IFruit (subtotal [item] [item subtotal])) (defrecord Apple [price] IFruit (subtotal [_] price) (subtotal [_ discount] (- price discount))) -[(subtotal (->Apple 100)) (subtotal (->Apple 100) 5)] -"] - (is (= [100 95] (tu/eval* prog {}))))) +(extend-type String IFruit (subtotal ([s] (count s)) ([s discount] (- (count s) discount)))) +[(subtotal (->Apple 100)) (subtotal (->Apple 100) 5) (subtotal \"foo\") (subtotal \"foo\" 2)] +" + prog #?(:clj prog + :cljs (-> prog + (str/replace "String" "js/String")))] + (is (= [100 95 3 1] (tu/eval* prog #?(:clj {} + :cljs {:classes {:allow :all + 'js #js {:String js/String}}})))))) #?(:clj (deftest import-test