Created
June 30, 2017 08:08
-
-
Save Rovanion/10b5af393498500472c84cab53b1ff02 to your computer and use it in GitHub Desktop.
Modified spec/assert for improved error reporting
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.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