I have an assertion that checks key correctness at compile time:
(defn assert-keys [check-map valid-keys]
(let [k (set (keys check-map))]
(when-not (set/subset? k valid-keys)
(throw (ex-info "Function call contains invalid key." {:submitted-keys k
:valid-keys valid-keys})))))
It is invoked in a macro:
(defmacro with-hooks [{:keys [read write] :as options} & body]
(assert-keys options #{:read :write})
`(binding [*read-hook* (or ~read *read-hook*)
*write-hook* (or ~write *write-hook*)]
~@body))
In this case it will check that the options map may contain the keys :read
and :write
, and nothing more.
The thing is, I’d like to write a test for this. The code will cause a compiler error on macro expand if there’s an invalid key. Is there any way of catching this error in a test?