706
706
(swap! instrumented-vars assoc v {:raw to-wrap :wrapped checked}))
707
707
(->sym v)))
708
708
709
+ (defn- unstrument-1
710
+ [s]
711
+ (when-let [v (resolve s)]
712
+ (when-let [{:keys [raw wrapped]} (get @instrumented-vars v)]
713
+ (let [current @v]
714
+ (when (= wrapped current)
715
+ (alter-var-root v (constantly raw))))
716
+ (swap! instrumented-vars dissoc v))
717
+ (->sym v)))
718
+
719
+ (defn- opt-syms
720
+ " Returns set of symbols referenced by 'instrument' opts map"
721
+ [opts]
722
+ (reduce into #{} [(:stub opts) (c/keys (:replace opts)) (c/keys (:spec opts))]))
723
+
724
+ (defn- sym-matcher
725
+ " Returns a fn that matches symbols that are either in syms,
726
+ or whose namespace is in syms."
727
+ [syms]
728
+ (let [names (into #{} (map str) syms)]
729
+ (fn [s]
730
+ (c/or (contains? names (namespace s))
731
+ (contains? names (str s))))))
732
+
733
+ (defn- validate-opts
734
+ [opts]
735
+ (assert (every? ident? (c/keys (:gen opts))) " instrument :gen expects ident keys" ))
736
+
709
737
(defn instrument
710
- " Instruments the vars named by sym-or-syms, a symbol or a
711
- collection of symbols. Idempotent.
738
+ " Instruments the vars matched by ns-or-names, a symbol or a
739
+ collection of symbols. Instruments the current namespace if
740
+ ns-or-names not specified. Idempotent.
741
+
742
+ A var matches ns-or-names if ns-or-names includes either the var's
743
+ fully qualified name or the var's namespace.
712
744
713
745
If a var has an :args fn-spec, sets the var's root binding to a
714
746
fn that checks arg conformance (throwing an exception on failure)
715
747
before delegating to the original fn.
716
748
717
749
The opts map can be used to override registered specs, and/or to
718
- replace fn implementations entirely. Opts for symbols not named by
719
- sym -or-syms are ignored. This facilitates sharing a common options map
720
- across many different calls to instrument.
750
+ replace fn implementations entirely. Opts for symbols not matched
751
+ by ns -or-names are ignored. This facilitates sharing a common
752
+ options map across many different calls to instrument.
721
753
722
754
The opts map may have the following keys:
723
755
@@ -742,84 +774,43 @@ invokes the fn you provide, enabling arbitrary stubbing and mocking.
742
774
:spec can be used in combination with :stub or :replace.
743
775
744
776
Returns a collection of syms naming the vars instrumented."
745
- ([sym-or-syms] (instrument sym-or-syms nil ))
746
- ([sym-or-syms opts]
747
- (assert (every? ident? (c/keys (:gen opts))) " instrument :gen expects ident keys" )
748
- (locking instrumented-vars
749
- (into
750
- []
751
- (comp (map #(instrument-1 % opts))
752
- (remove nil?))
753
- (collectionize sym-or-syms)))))
754
-
755
- (defn- unstrument-1
756
- [s]
757
- (when-let [v (resolve s)]
758
- (when-let [{:keys [raw wrapped]} (get @instrumented-vars v)]
759
- (let [current @v]
760
- (when (= wrapped current)
761
- (alter-var-root v (constantly raw))))
762
- (swap! instrumented-vars dissoc v))
763
- (->sym v)))
764
-
765
- (defn unstrument
766
- " Undoes instrument on the vars named by sym-or-syms. Idempotent.
767
- Returns a collection of syms naming the vars unstrumented."
768
- [sym-or-syms]
769
- (locking instrumented-vars
770
- (into
771
- []
772
- (comp (map #(unstrument-1 %))
773
- (remove nil?))
774
- (collectionize sym-or-syms))))
775
-
776
- (defn- opt-syms
777
- " Returns set of symbols referenced by 'instrument' opts map"
778
- [opts]
779
- (reduce into #{} [(:stub opts) (c/keys (:replace opts)) (c/keys (:spec opts))]))
780
-
781
- (defn- ns-matcher
782
- [ns-syms]
783
- (let [ns-names (into #{} (map str) ns-syms)]
784
- (fn [s]
785
- (contains? ns-names (namespace s)))))
786
-
787
- (defn instrument-ns
788
- " Like instrument, but works on all symbols whose namespace name is
789
- in ns-or-nses, a symbol or a collection of symbols."
790
- ([] (instrument-ns (.name ^clojure.lang.Namespace *ns*)))
791
- ([ns-or-nses] (instrument-ns ns-or-nses nil ))
792
- ([ns-or-nses opts]
793
- (let [ns-match? (ns-matcher (collectionize ns-or-nses))]
777
+ ([] (instrument (.name ^clojure.lang.Namespace *ns*)))
778
+ ([ns-or-names] (instrument ns-or-names nil ))
779
+ ([ns-or-names opts]
780
+ (validate-opts opts)
781
+ (let [match? (sym-matcher (collectionize ns-or-names))]
794
782
(locking instrumented-vars
795
783
(into
796
784
[]
797
785
(comp c/cat
798
786
(filter symbol?)
799
- (filter ns- match?)
787
+ (filter match?)
800
788
(distinct )
801
789
(map #(instrument-1 % opts))
802
790
(remove nil?))
803
791
[(c/keys (registry )) (opt-syms opts)])))))
804
792
805
- (defn unstrument-ns
806
- " Like unstrument, but works on all symbols whose namespace name is
807
- in ns-or-nses, a symbol or a collection of symbols."
808
- [ns-or-nses]
809
- (let [ns-match? (ns-matcher (collectionize ns-or-nses))]
810
- (locking instrumented-vars
811
- (into
812
- []
813
- (comp (map ->sym)
814
- (filter ns-match?)
815
- (map unstrument-1)
816
- (remove nil?))
817
- (c/keys @instrumented-vars)))))
793
+ (defn unstrument
794
+ " Undoes instrument on the vars matched by ns-or-names, specified
795
+ as in instrument. Returns a collection of syms naming the vars
796
+ unstrumented."
797
+ ([] (unstrument (.name ^clojure.lang.Namespace *ns*)))
798
+ ([ns-or-names]
799
+ (let [match? (sym-matcher (collectionize ns-or-names))]
800
+ (locking instrumented-vars
801
+ (into
802
+ []
803
+ (comp (map ->sym)
804
+ (filter match?)
805
+ (map unstrument-1)
806
+ (remove nil?))
807
+ (c/keys @instrumented-vars))))))
818
808
819
809
(defn instrument-all
820
810
" Like instrument, but works on all vars."
821
811
([] (instrument-all nil ))
822
812
([opts]
813
+ (validate-opts opts)
823
814
(locking instrumented-vars
824
815
(into
825
816
[]
0 commit comments