cljs.spec/every
macro | removed v1.9.542 | added v1.9.85 | Edit |
(every pred & {:keys [into kind count max-count min-count distinct gen-max gen-into gen], :as opts})
Source docstring:
takes a pred and validates collection elements against that pred.
Note that 'every' does not do exhaustive checking, rather it samples
*coll-check-limit* elements. Nor (as a result) does it do any
conforming of elements. 'explain' will report at most *coll-error-limit*
problems. Thus 'every' should be suitable for potentially large
collections.
Takes several kwargs options that further constrain the collection:
:kind - a pred/spec that the collection type must satisfy, e.g. vector?
(default nil) Note that if :kind is specified and :into is
not, this pred must generate in order for every to generate.
:count - specifies coll has exactly this count (default nil)
:min-count, :max-count - coll has count (<= min-count count max-count) (defaults nil)
:distinct - all the elements are distinct (default nil)
And additional args that control gen
:gen-max - the maximum coll size to generate (default 20)
:into - one of [], (), {}, #{} - the default collection to generate into
(default same as :kind if supplied, else []
Optionally takes :gen generator-fn, which must be a fn of no args that
returns a test.check generator
See also - coll-of, every-kv
(defmacro every
[pred & {:keys [into kind count max-count min-count distinct gen-max gen-into gen] :as opts}]
(let [desc (::describe opts)
nopts (-> opts
(dissoc :gen ::describe)
(assoc ::kind-form `'~(res &env (:kind opts))
::describe (clojure.core/or desc `'(every ~(res &env pred) ~@(res-kind &env opts)))))
gx (gensym)
cpreds (cond-> [(list (clojure.core/or kind `coll?) gx)]
count (conj `(= ~count (c/bounded-count ~count ~gx)))
(clojure.core/or min-count max-count)
(conj `(<= (c/or ~min-count 0)
(c/bounded-count (if ~max-count (inc ~max-count) ~min-count) ~gx)
(c/or ~max-count MAX_INT)))
distinct
(conj `(c/or (empty? ~gx) (apply distinct? ~gx))))]
`(every-impl '~pred ~pred ~(assoc nopts ::cpred `(fn* [~gx] (c/and ~@cpreds))) ~gen)))