Skip to content

Commit 14e878e

Browse files
author
dnolen
committed
fix instrument behavior if no syms provided, unstrument wip
1 parent aa9fd3d commit 14e878e

3 files changed

Lines changed: 43 additions & 47 deletions

File tree

src/main/cljs/cljs/spec.cljc

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -356,26 +356,8 @@
356356

357357
(def ^:private _speced_vars (atom #{}))
358358

359-
(defn speced-vars*
360-
([]
361-
(speced-vars* nil))
362-
([ns-syms]
363-
(let [ns-match? (if (seq ns-syms)
364-
(set (map second ns-syms))
365-
(constantly true))]
366-
(reduce
367-
(fn [ret sym]
368-
(if (ns-match? (symbol (namespace sym)))
369-
(conj ret (list 'var sym))
370-
ret))
371-
#{} @_speced_vars))))
372-
373-
(defmacro speced-vars
374-
"Returns the set of vars whose namespace is in ns-syms AND
375-
whose vars have been speced with fdef. If no ns-syms are
376-
specified, return speced vars from all namespaces."
377-
[& ns-syms]
378-
(speced-vars* ns-syms))
359+
(defn speced-vars []
360+
@_speced_vars)
379361

380362
(defmacro fdef
381363
"Takes a symbol naming a function, and one or more of the following:

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

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
[cljs.spec :as s]
1414
[cljs.spec.impl.gen :as gen]))
1515

16+
(defonce ^:private instrumented-vars (atom #{}))
17+
1618
(defn- collectionize
1719
[x]
1820
(if (symbol? x)
@@ -44,6 +46,7 @@ returns the set of all symbols naming vars in those nses."
4446
[[quote s] opts]
4547
(let [v (ana-api/resolve &env s)]
4648
(when v
49+
(swap! instrumented-vars conj v)
4750
`(let [checked# (instrument-1* ~s (var ~s) ~opts)]
4851
(when checked# (set! ~s checked#))
4952
'~(:name v)))))
@@ -52,6 +55,7 @@ returns the set of all symbols naming vars in those nses."
5255
[[quote s]]
5356
(let [v (ana-api/resolve &env s)]
5457
(when v
58+
(swap! instrumented-vars disj v)
5559
`(let [raw# (unstrument-1* ~s (var ~s))]
5660
(when raw# (set! ~s raw#))
5761
'~(:name v)))))
@@ -94,7 +98,7 @@ invokes the fn you provide, enabling arbitrary stubbing and mocking.
9498
9599
Returns a collection of syms naming the vars instrumented."
96100
([]
97-
`(instrument (instrumentable-syms)))
101+
`(instrument '[~@(s/speced-vars)]))
98102
([xs]
99103
`(instrument ~xs nil))
100104
([[quote sym-or-syms] opts]
@@ -119,30 +123,35 @@ Returns a collection of syms naming the vars instrumented."
119123
as in instrument. With no args, unstruments all instrumented vars.
120124
Returns a collection of syms naming the vars unstrumented."
121125
([]
122-
`(unstrument (map ->sym (keys @instrumented-vars))))
123-
([sym-or-syms]
124-
`(into
126+
`(unstrument '[~@(deref instrumented-vars)]))
127+
([[quote sym-or-syms]]
128+
`(reduce
129+
(fn [ret# f#]
130+
(let [sym# (f#)]
131+
(cond-> ret# sym# (conj sym#))))
125132
[]
126-
(comp (filter symbol?)
127-
(map unstrument-1*)
128-
(remove nil?))
129-
(collectionize ~sym-or-syms))))
130-
131-
(defmacro run-tests
132-
"Like run-all-tests, but scoped to specific namespaces, or to
133-
*ns* if no ns-sym are specified."
134-
([]
135-
`(cljs.spec.test/run-tests '~ana/*cljs-ns*))
136-
([& ns-syms]
137-
`(cljs.spec.test/run-var-tests
138-
(->> #?(:clj ~(s/speced-vars* ns-syms)
139-
:cljs ~(cljs.spec$macros/speced-vars* ns-syms))
140-
(filter (fn [v#] (:args (cljs.spec/get-spec v#))))))))
141-
142-
(defmacro run-all-tests
143-
"Like clojure.test/run-all-tests, but runs test.check tests
144-
for all speced vars. Prints per-test results to *out*, and
145-
returns a map with :test,:pass,:fail, and :error counts."
146-
[]
147-
`(cljs.spec.test/run-var-tests #?(:clj ~(s/speced-vars*)
148-
:cljs ~(cljs.spec$macros/speced-vars*))))
133+
[~@(->> (collectionize sym-or-syms)
134+
(map
135+
(fn [sym]
136+
(when (symbol? symbol)
137+
`(fn [] (unstrument-1 ~'sym)))))
138+
(remove nil?))])))
139+
140+
;(defmacro run-tests
141+
; "Like run-all-tests, but scoped to specific namespaces, or to
142+
;*ns* if no ns-sym are specified."
143+
; ([]
144+
; `(cljs.spec.test/run-tests '~ana/*cljs-ns*))
145+
; ([& ns-syms]
146+
; `(cljs.spec.test/run-var-tests
147+
; (->> #?(:clj ~(s/speced-vars* ns-syms)
148+
; :cljs ~(cljs.spec$macros/speced-vars* ns-syms))
149+
; (filter (fn [v#] (:args (cljs.spec/get-spec v#))))))))
150+
;
151+
;(defmacro run-all-tests
152+
; "Like clojure.test/run-all-tests, but runs test.check tests
153+
;for all speced vars. Prints per-test results to *out*, and
154+
;returns a map with :test,:pass,:fail, and :error counts."
155+
; []
156+
; `(cljs.spec.test/run-var-tests #?(:clj ~(s/speced-vars)
157+
; :cljs ~(cljs.spec$macros/speced-vars*))))

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,9 +295,14 @@ Returns a map as quick-check, with :explain-data added if
295295

296296
(m/instrument-1 `ranged-rand {})
297297

298+
(m/instrument)
298299
(m/instrument `ranged-rand)
299300
(m/instrument `[ranged-rand])
300301

302+
(m/unstrument)
303+
(m/unstrument `ranged-rand)
304+
(m/unstrument `[ranged-rand])
305+
301306
(ranged-rand 8 5)
302307
(defn foo
303308
([a])

0 commit comments

Comments
 (0)