Skip to content

Commit

Permalink
Fix parse-defmethod-args not throwing error as expected (#136)
Browse files Browse the repository at this point in the history
* Fix `parse-defmethod-args` not throwing error as expected

* Test fix
  • Loading branch information
camsaul authored Oct 6, 2022
1 parent 927af15 commit 687a0ac
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 16 deletions.
6 changes: 1 addition & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -534,11 +534,7 @@ match *all* of the specified `:defmethod-arities`, and all of its arities must b
;; => error: {:arities {:disallowed #{2}}}

(m/defmethod mf :x [x y] x y)
;; => error: {:arities {:required #{1}}}


(m/defmethod mf :x [x y] x)
;; => error: {:arities {:required #{1 [:>= 3]}, :disallowed #{2}}}
;; => error: {:required #{1}, :disallowed #{2}}
```

`:defmethod-arities` must be a set of either integers or `[:> n]` forms to represent arities with `&` rest
Expand Down
2 changes: 1 addition & 1 deletion VERSION.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.15.0
0.15.0.1
2 changes: 1 addition & 1 deletion src/methodical/macros.clj
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@
(let [spec (defmethod-args-spec multifn)
conformed (s/conform spec args)]
(if (s/invalid? conformed)
(s/explain-str spec args)
(throw (ex-info (s/explain-str spec args) (s/explain-data spec args)))
(let [{[method-type type-args] :args-for-method-type} conformed]
(-> (merge conformed {:method-type method-type} type-args)
(dissoc :args-for-method-type))))))
Expand Down
20 changes: 11 additions & 9 deletions test/methodical/macros_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -198,12 +198,6 @@
:dispatch-value ({:a 1} [2 3])
:fn-tail [([x] {:x x, :y y})
([x y] {:x x, :y y})]}

;; despite looking a little ambiguous, this is actually invalid because neither `:before` nor `([x] x)` are allowed
;; as dispatch values; see [[methodical.macros/default-dispatch-value-spec]] for reasons why.
[:before ([x] x) ([x y] x)]
"([x] x) - failed: dispatch-value-spec in: [1] at: [:args-for-method-type :aux :dispatch-value]\n"

;; here's another ambiguous one. Is this an `:after` aux method with dispatch value `"str"`, or a primary
;; method with dispatch value `:after` and a docstring?
;;
Expand All @@ -213,7 +207,15 @@
{:method-type :aux
:qualifier :after
:dispatch-value "str"
:fn-tail [[_x]]}))
:fn-tail [[_x]]})

(t/testing "Errors"
;; despite looking a little ambiguous, this is actually invalid because neither `:before` nor `([x] x)` are allowed
;; as dispatch values; see [[methodical.macros/default-dispatch-value-spec]] for reasons why.
(t/is (thrown-with-msg?
clojure.lang.ExceptionInfo
(re-quote "([x] x) - failed: dispatch-value-spec in: [1] at: [:args-for-method-type :aux :dispatch-value]\n")
(#'macros/parse-defmethod-args mf1 '[:before ([x] x) ([x y] x)])))))

(macros/defmethod mf1 :x
[m]
Expand Down Expand Up @@ -580,7 +582,7 @@
(t/is (= 1
(num-primary-methods))))
(let [original-hash (::macros/defmulti-hash (meta (resolve 'methodical.macros-test/metadata-updates-mf)))
expected-doc ["metadata-updates-mf is defined in [[methodical.macros-test]] (methodical/macros_test.clj:572)."
expected-doc ["metadata-updates-mf is defined in [[methodical.macros-test]] (methodical/macros_test.clj:574)."
""
"It caches methods using a [[methodical.impl.cache.watching.WatchingCache]]."
""
Expand All @@ -597,7 +599,7 @@
""
"These primary methods are known:"
""
"* `:default`, defined in [[methodical.macros-test]] (methodical/macros_test.clj:575) "]]
"* `:default`, defined in [[methodical.macros-test]] (methodical/macros_test.clj:577) "]]
(t/is (integer? original-hash))
(letfn [(relevant-metadata [metadata]
(let [metadata (select-keys metadata [:name :private :amazing? :doc ::macros/defmulti-hash])]
Expand Down

0 comments on commit 687a0ac

Please sign in to comment.