Skip to content

Commit

Permalink
[#406] Tests for multi-arity protocols and multimethods
Browse files Browse the repository at this point in the history
  • Loading branch information
borkdude authored Sep 16, 2020
1 parent 76a0d67 commit b0e7d64
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
21 changes: 21 additions & 0 deletions test/sci/multimethods_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
"))))
12 changes: 9 additions & 3 deletions test/sci/protocols_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b0e7d64

Please sign in to comment.