Create a gist now

Instantly share code, notes, and snippets.

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)
~spec)]
(if clojure.lang.RT/checkSpecAsserts
(spec/assert* (spec/specize* spec#) ~x)
~x))
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