spec / instrument arbitrary Clojure expressions!
(require '[spectacular.core :refer [with-spec]])
(let [a 100
b "2"]
(with-spec {a integer?
b string?}
string?
(str a b)))
Specs are great! Let's have more of them.
[spectacular "0.2.0"]
(require '[spectacular.core :as s])
Specs will be checked iff (s/check-asserts?)
evaluates to true
, which it doesn't by default.
This can be achieved in two ways:
(s/check-asserts true)
enables all spec asserts globallys/with-check-asserts
sets spec-checking on/off for its body:(s/with-check-asserts true
(let [a 100
b "2"]
(s/with-spec {a integer?
b string?}
string?
(str a b))))
We can just check existing bindings:
(s/with-check-asserts true
(let [a 100
b "2"]
(s/with-spec-in {a integer?
b string?}
(str a b))))
or just the returned value:
(s/with-check-asserts true
(let [a 100
b "2"]
(s/with-spec-out string?
(str a b))))
or even entire functions:
(require '[clojure.spec.alpha :as sa])
(let [palindrome? (fn [s]
(s/with-spec {s (sa/and string? not-empty)}
boolean?
(= (seq s) (reverse s))))]
(s/with-check-asserts true
(palindrome? "pop")))
Get better error messages with expound.
(require [expound.alpha :as e])
(set! sa/*explain-out* e/printer)
Copyright © 2018 Divyansh Prakash
Distributed under the Eclipse Public License either version 1.0.
Can you improve this documentation?Edit on GitHub
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close