-
-
Save colinkahn/dff9f01107fdd7e92ea3191653e628d2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(require '[clojure.spec.test.alpha :as st]) | |
(require '[clojure.spec.alpha :as s]) | |
(require '[expound.alpha :as expound]) | |
;; Two macros, one that quotes tmp-sym in the call to `st/check`... | |
(defmacro check1 [sym tmp-sym & spec-body] | |
`(do | |
(def ~tmp-sym ~sym) | |
(s/fdef ~tmp-sym ~@spec-body) | |
(st/check (quote ~tmp-sym)) | |
)) | |
;; ...the other that gets the syntax quoted symbol passed in. | |
(defmacro check2 [sym tmp-sym quoted-tmp-sym & spec-body] | |
`(do | |
(def ~tmp-sym ~sym) | |
(s/fdef ~tmp-sym ~@spec-body) | |
(st/check ~quoted-tmp-sym) | |
)) | |
;; Expanding them gives the same results | |
(= (macroexpand-1 `(check1 foo foo2 :args (s/cat) :ret false?)) | |
(macroexpand-1 `(check2 foo foo2 `foo2 :args (s/cat) :ret false?))) | |
;; The function foo returns true, and we'll check it with a spec that should fail | |
(defn foo [] true) | |
;; But only `check2` actually works | |
(expound/explain-results (check1 foo foo2 :args (s/cat) :ret false?)) | |
(expound/explain-results (check2 foo foo2 `foo2 :args (s/cat) :ret false?)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment