Skip to content

Instantly share code, notes, and snippets.

@Rovanion
Created June 30, 2017 08:08
Show Gist options
  • Save Rovanion/10b5af393498500472c84cab53b1ff02 to your computer and use it in GitHub Desktop.
Save Rovanion/10b5af393498500472c84cab53b1ff02 to your computer and use it in GitHub Desktop.
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