Skip to content

Instantly share code, notes, and snippets.

@colinkahn
Last active August 18, 2018 19:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save colinkahn/dff9f01107fdd7e92ea3191653e628d2 to your computer and use it in GitHub Desktop.
Save colinkahn/dff9f01107fdd7e92ea3191653e628d2 to your computer and use it in GitHub Desktop.
(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