Skip to content

Instantly share code, notes, and snippets.



Created Jun 30, 2017
What would you like to do?
Modified spec/assert for improved error reporting
(require '[clojure.spec.alpha :as spec])
;;; Spec does not report the name of the predicate when not wrapped in a spec.
(spec/assert number? nil)
;; Spec assertion failed val: nil fails predicate:
;; :clojure.spec.alpha/unknown :clojure.spec.alpha/failure
;; :assertion-failed
;;; But we can wrap predicates in specs so that their names are reported.
(defmacro assert'
[spec x]
(if spec/*compile-asserts*
`(let [spec# (if (fn? ~spec)
(spec/spec ~spec)
(if clojure.lang.RT/checkSpecAsserts
(spec/assert* (spec/specize* spec#) ~x)
(assert' number? 'a)
;; Spec assertion failed val: a fails predicate: number?
;; :clojure.spec.alpha/failure :assertion-failed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment