Skip to content

Commit

Permalink
Merge pull request #245 from grammati/instrument-deftest
Browse files Browse the repository at this point in the history
Instrument deftest for debugging
  • Loading branch information
Malabarba committed Aug 20, 2015
2 parents 57b1caf + 855d3c0 commit 8ea1963
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
26 changes: 20 additions & 6 deletions src/cider/nrepl/middleware/util/instrument.clj
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,14 @@
'#{.} `(~(instrument (first args))
~(second args)
~@(instrument-coll (rest (rest args))))
'#{def set!} (list* (with-meta-safe (first args) {:cider-instrumented true})
(map instrument (rest args)))
'#{def} (let [sym (first args)]
(list* (if (meta sym)
(with-meta-safe sym (assoc (instrument (meta sym))
:cider-instrumented true))
sym)
(map instrument (rest args))))
'#{set!} (list (with-meta-safe (first args) {:cider-instrumented true})
(instrument (second args)))
'#{loop* let* letfn*} (cons (vec (map-indexed (fn [i x] (if (odd? i) (instrument x) x))
(first args)))
(instrument-coll (rest args)))
Expand Down Expand Up @@ -259,11 +265,19 @@
(f coor (with-meta-safe result (meta form))))))

(defn macroexpand-all
"Like clojure.walk/macroexpand-all, but preserve metadata."
"Like clojure.walk/macroexpand-all, but preserves and macroexpands
metadata."
[form]
(->> (if (seq? form) (macroexpand form) form)
(walk/walk macroexpand-all identity)
(#(with-meta-safe % (meta form)))))
(let [md (meta form)
expanded (walk/walk macroexpand-all
identity
(if (seq? form) (macroexpand form) form))]
(if md
;; Macroexpand the metadata too, because sometimes metadata
;; contains, for example, functions. This is the case for
;; deftest forms.
(with-meta-safe expanded (macroexpand-all md))
expanded)))

(defn instrument-tagged-code
"Return `form` instrumented with breakpoints.
Expand Down
7 changes: 6 additions & 1 deletion test/clj/cider/nrepl/middleware/util/instrument_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
(with-redefs [d/breakpoint-reader
(fn [x] (t/with-meta-safe x {:cider-breakfunction #'bp}))]
(reset! bp-tracker #{})
(walk/macroexpand-all (#'t/instrument-tagged-code (#'d/debug-reader form)))
(t/macroexpand-all (#'t/instrument-tagged-code (#'d/debug-reader form)))
;; Replace #'bp with 'bp for easier print and comparison.
(walk/postwalk #(if (= % #'bp) 'bp %) @bp-tracker)))

Expand Down Expand Up @@ -170,3 +170,8 @@
'#{[(set! (. inst field) (bp (bar) [2])) []] [(bar) [2]]}))
(is (= (breakpoint-tester '(set! (.field inst) (bar)))
'#{[(set! (. inst field) (bp (bar) [2])) []] [(bar) [2]]})))

(deftest instrument-deftest
(binding [*ns* (the-ns 'cider.nrepl.middleware.util.instrument-test)]
(is (= (breakpoint-tester '(deftest foo (bar)))
'#{[(bar) [2]]}))))

0 comments on commit 8ea1963

Please sign in to comment.