Skip to content

Commit 731be5e

Browse files
borkdudeswannodette
authored andcommitted
CLJS-2964: Requiring spec.test.alpha loads clojure.test.check
Patch fixes lazy loading of clojure.test.check. Unifies key for passing options to clojure.test.check with Clojure.
1 parent e0d499c commit 731be5e

File tree

3 files changed

+31
-15
lines changed

3 files changed

+31
-15
lines changed

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

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ is not specified, check all checkable vars. If a symbol identifies a
255255
namespace then all symbols in that namespace will be enumerated.
256256
257257
The opts map includes the following optional keys, where stc
258-
aliases clojure.test.check:
258+
aliases clojure.spec.test.check:
259259
260260
::stc/opts opts to flow through test.check/quick-check
261261
:gen map from spec names to generator overrides
@@ -289,12 +289,17 @@ spec itself will have an ::s/failure value in ex-data:
289289
([sym-or-syms opts]
290290
(let [syms (sym-or-syms->syms (form->sym-or-syms sym-or-syms))
291291
opts-sym (gensym "opts")]
292-
`(let [~opts-sym ~opts]
293-
[~@(->> syms
294-
(filter (checkable-syms* opts))
295-
(map
296-
(fn [sym]
297-
(do `(check-1 '~sym nil nil ~opts-sym)))))]))))
292+
`(if (and (cljs.core/exists? clojure.test.check)
293+
(cljs.core/exists? clojure.test.check.properties))
294+
(let [~opts-sym ~opts]
295+
[~@(->> syms
296+
(filter (checkable-syms* opts))
297+
(map
298+
(fn [sym]
299+
(do `(check-1 '~sym nil nil ~opts-sym)))))])
300+
(throw
301+
(js/Error. (str "Require clojure.test.check and "
302+
"clojure.test.check.properties before calling check.")))))))
298303

299304
(defmacro ^:private maybe-setup-static-dispatch [f ret conform! arity]
300305
(let [arity-accessor (symbol (str ".-cljs$core$IFn$_invoke$arity$" arity))

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@
1515
[cljs.stacktrace :as st]
1616
[cljs.pprint :as pp]
1717
[cljs.spec.alpha :as s]
18-
[cljs.spec.gen.alpha :as gen]
19-
[clojure.test.check :as stc]
20-
[clojure.test.check.properties]))
18+
[cljs.spec.gen.alpha :as gen]))
2119

2220
(defn distinct-by
2321
([f coll]
@@ -228,7 +226,7 @@ with explain-data + ::s/failure."
228226
true))))))
229227

230228
(defn- quick-check
231-
[f specs {gen :gen opts ::stc/opts}]
229+
[f specs {gen :gen opts :clojure.spec.test.check/opts}]
232230
(let [{:keys [num-tests] :or {num-tests 1000}} opts
233231
g (try (s/gen (:args specs) gen) (catch js/Error t t))]
234232
(if (instance? js/Error g)
@@ -240,7 +238,7 @@ with explain-data + ::s/failure."
240238
"Builds spec result map."
241239
[check-sym spec test-check-ret]
242240
(merge {:spec spec
243-
::stc/ret test-check-ret}
241+
:clojure.spec.test.check/ret test-check-ret}
244242
(when check-sym
245243
{:sym check-sym})
246244
(when-let [result (-> test-check-ret :result)]
@@ -282,10 +280,10 @@ with explain-data + ::s/failure."
282280
suitable for summary use."
283281
[x]
284282
(if (:failure x)
285-
(-> (dissoc x ::stc/ret)
283+
(-> (dissoc x :clojure.spec.test.check/ret)
286284
(update :spec s/describe)
287285
(update :failure unwrap-failure))
288-
(dissoc x :spec ::stc/ret)))
286+
(dissoc x :spec :clojure.spec.test.check/opts)))
289287

290288
(defn summarize-results
291289
"Given a collection of check-results, e.g. from 'check', pretty

src/test/cljs/cljs/spec/test_test.cljs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
(is (= (stest/unstrument `h-cljs-1812)
2121
[]))
2222

23-
(stest/check `h-cljs-1812 {:clojure.test.check/opts {:num-tests 1}})
23+
(stest/check `h-cljs-1812 {:clojure.spec.test.check/opts {:num-tests 1}})
2424

2525
; Calling h-cljs-1812 with an argument of the wrong type shouldn't throw,
2626
; because the function should not have been instrumented by stest/check.
@@ -151,3 +151,16 @@
151151
(is (= [0 1] (fn-2995 0)))
152152
(is (= [0 1] (fn-2995 0)))
153153
(is (thrown? js/Error (fn-2995 "not a number")))))
154+
155+
(defn cljs-2964 [x] true)
156+
(s/fdef cljs-2964 :args (s/cat :x int?) :ret true?)
157+
158+
(deftest test-cljs-2964
159+
(let [check-res
160+
(stest/check `cljs-2964 {:clojure.spec.test.check/opts {:num-tests 1}})]
161+
(is (seq check-res))
162+
(is (every? (fn [res]
163+
(= 1 (-> res
164+
:clojure.spec.test.check/ret
165+
:num-tests)))
166+
check-res))))

0 commit comments

Comments
 (0)