Skip to content

Commit e2db5d9

Browse files
committed
implement check
1 parent f48d6ec commit e2db5d9

File tree

2 files changed

+55
-42
lines changed

2 files changed

+55
-42
lines changed

src/main/cljs/cljs/spec/test.cljc

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,11 @@ Returns a collection of syms naming the vars unstrumented."
181181
(validate-check-opts opts#)
182182
(check-1 nil ~f ~spec opts#))))
183183

184+
(defn checkable-syms* [opts]
185+
(reduce into #{}
186+
[(filter fn-spec-name? (keys @s/registry-ref))
187+
(keys (:spec opts))]))
188+
184189
(defmacro checkable-syms
185190
"Given an opts map as per check, returns the set of syms that
186191
can be checked."
@@ -191,4 +196,51 @@ can be checked."
191196
(validate-check-opts opts#)
192197
(reduce conj #{}
193198
'[~@(filter fn-spec-name? (keys @s/registry-ref))
194-
~@(keys (:spec opts))]))))
199+
~@(keys (:spec opts))]))))
200+
201+
(defmacro check
202+
"Run generative tests for spec conformance on vars named by
203+
sym-or-syms, a symbol or collection of symbols. If sym-or-syms
204+
is not specified, check all checkable vars.
205+
206+
The opts map includes the following optional keys, where stc
207+
aliases clojure.spec.test.check:
208+
209+
::stc/opts opts to flow through test.check/quick-check
210+
:gen map from spec names to generator overrides
211+
212+
The ::stc/opts include :num-tests in addition to the keys
213+
documented by test.check. Generator overrides are passed to
214+
spec/gen when generating function args.
215+
216+
Returns a lazy sequence of check result maps with the following
217+
keys
218+
219+
:spec the spec tested
220+
:sym optional symbol naming the var tested
221+
:failure optional test failure
222+
::stc/ret optional value returned by test.check/quick-check
223+
224+
The value for :failure can be any exception. Exceptions thrown by
225+
spec itself will have an ::s/failure value in ex-data:
226+
227+
:check-failed at least one checked return did not conform
228+
:no-args-spec no :args spec provided
229+
:no-fn no fn provided
230+
:no-fspec no fspec provided
231+
:no-gen unable to generate :args
232+
:instrument invalid args detected by instrument
233+
"
234+
([]
235+
`(check '~(checkable-syms*)))
236+
([sym-or-syms]
237+
`(check ~sym-or-syms nil))
238+
([sym-or-syms opts]
239+
(let [sym-or-syms (eval sym-or-syms)
240+
opts-sym (gensym "opts")]
241+
`(let [~opts-sym ~opts]
242+
[~@(->> (collectionize sym-or-syms)
243+
(filter (checkable-syms* opts))
244+
(map
245+
(fn [sym]
246+
(do `(check-1 '~sym nil nil ~opts-sym)))))]))))

src/main/cljs/cljs/spec/test.cljs

Lines changed: 2 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -226,47 +226,6 @@ with explain-data + ::s/failure."
226226
[opts]
227227
(assert (every? ident? (keys (:gen opts))) "check :gen expects ident keys"))
228228

229-
(defn check
230-
"Run generative tests for spec conformance on vars named by
231-
sym-or-syms, a symbol or collection of symbols. If sym-or-syms
232-
is not specified, check all checkable vars.
233-
234-
The opts map includes the following optional keys, where stc
235-
aliases clojure.spec.test.check:
236-
237-
::stc/opts opts to flow through test.check/quick-check
238-
:gen map from spec names to generator overrides
239-
240-
The ::stc/opts include :num-tests in addition to the keys
241-
documented by test.check. Generator overrides are passed to
242-
spec/gen when generating function args.
243-
244-
Returns a lazy sequence of check result maps with the following
245-
keys
246-
247-
:spec the spec tested
248-
:sym optional symbol naming the var tested
249-
:failure optional test failure
250-
::stc/ret optional value returned by test.check/quick-check
251-
252-
The value for :failure can be any exception. Exceptions thrown by
253-
spec itself will have an ::s/failure value in ex-data:
254-
255-
:check-failed at least one checked return did not conform
256-
:no-args-spec no :args spec provided
257-
:no-fn no fn provided
258-
:no-fspec no fspec provided
259-
:no-gen unable to generate :args
260-
:instrument invalid args detected by instrument
261-
"
262-
([] (check (checkable-syms)))
263-
([sym-or-syms] (check sym-or-syms nil))
264-
([sym-or-syms opts]
265-
(->> (collectionize sym-or-syms)
266-
(filter (checkable-syms opts))
267-
(map
268-
#(check-1 (sym->check-map %) opts)))))
269-
270229
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; check reporting ;;;;;;;;;;;;;;;;;;;;;;;;
271230

272231
(defn- failure-type
@@ -387,6 +346,8 @@ key with a count for each different :type of result."
387346
:ret int?))
388347

389348
(m/checkable-syms)
349+
350+
(m/check `ranged-rand)
390351
)
391352

392353

0 commit comments

Comments
 (0)