Skip to content

Commit abc6c2b

Browse files
committed
Return cider-test result keys more specifically
* Override `clojure.test` implementation of `test-var` to differentiate between errors occurring in assertions and test faults that do not. * Set the same fault flag on errors occurring in test fixtures. * Update the `report` function to return only keys that properly describe the test event: Failed tests return `actual`; errors return `error`. Test assertions have and `expected` value; non-assertion faults do not.
1 parent fbbd303 commit abc6c2b

File tree

1 file changed

+35
-15
lines changed

1 file changed

+35
-15
lines changed

src/cider/nrepl/middleware/test.clj

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,20 @@
6464
[ns v m]
6565
(let [c (when (seq test/*testing-contexts*) (test/testing-contexts-str))
6666
i (count (get (@current-report :results) (:name (meta v))))
67-
f (or (:test (meta v)) @v)] ; test fn or deref'ed fixture
68-
(merge {:ns ns, :var (:name (meta v)), :index i, :context c}
69-
(if (#{:fail :error} (:type m))
70-
(-> (if-let [e (when (= :error (:type m)) (:actual m))]
71-
(assoc m :error e, :line (:line (stack-frame e f)))
72-
m)
73-
(update-in [:expected] #(with-out-str (pp/pprint %)))
74-
(update-in [:actual] #(with-out-str (pp/pprint %))))
75-
(dissoc m :expected :actual)))))
67+
t (:type m)]
68+
;; Errors outside assertions (faults) do not return an :expected value.
69+
;; Type :fail returns :actual value. Type :error returns :error and :line.
70+
(merge (dissoc m :expected :actual)
71+
{:ns ns, :var (:name (meta v)), :index i, :context c}
72+
(when (and (#{:fail :error} t) (not (:fault m)))
73+
{:expected (with-out-str (pp/pprint (:expected m)))})
74+
(when (#{:fail} t)
75+
{:actual (with-out-str (pp/pprint (:actual m)))})
76+
(when (#{:error} t)
77+
(let [e (:actual m)
78+
f (or (:test (meta v)) @v)] ; test fn or deref'ed fixture
79+
{:error e
80+
:line (:line (stack-frame e f))})))))
7681

7782
(defn report
7883
"Handle reporting for test events. This takes a test event map as an argument
@@ -104,24 +109,39 @@
104109
fixture (resolve (symbol (:var frame)))]
105110
(swap! current-report update-in [:summary :test] dec)
106111
(binding [test/*testing-vars* (list fixture)]
107-
(report {:type :error, :expected nil, :actual e
108-
:message "Unhandled exception in test fixture"}))))
112+
(report {:type :error, :fault true, :expected nil, :actual e
113+
:message "Uncaught exception in test fixture"}))))
109114

110115
;;; ## Test Execution
111116
;; These functions are based on the ones in `clojure.test`, updated to accept
112-
;; a list of vars to test, and use the report implementation above.
117+
;; a list of vars to test, use the report implementation above, and distinguish
118+
;; between test errors and faults outside of assertions.
119+
120+
(defn test-var
121+
"If var `v` has a function in its `:test` metadata, call that function,
122+
with `clojure.test/*testing-vars*` bound to append `v`."
123+
[v]
124+
(when-let [t (:test (meta v))]
125+
(binding [test/*testing-vars* (conj test/*testing-vars* v)]
126+
(test/do-report {:type :begin-test-var :var v})
127+
(test/inc-report-counter :test)
128+
(try (t)
129+
(catch Throwable e
130+
(test/do-report {:type :error, :fault true, :expected nil, :actual e
131+
:message "Uncaught exception, not in assertion"})))
132+
(test/do-report {:type :end-test-var :var v}))))
113133

114134
(defn test-vars
115-
"Call `clojure.test/test-var` on each var, with the fixtures defined for
116-
namespace object `ns`."
135+
"Call `test-var` on each var, with the fixtures defined for namespace
136+
object `ns`."
117137
[ns vars]
118138
(let [once-fixture-fn (test/join-fixtures (::test/once-fixtures (meta ns)))
119139
each-fixture-fn (test/join-fixtures (::test/each-fixtures (meta ns)))]
120140
(try (once-fixture-fn
121141
(fn []
122142
(doseq [v vars]
123143
(when (:test (meta v))
124-
(each-fixture-fn (fn [] (test/test-var v)))))))
144+
(each-fixture-fn (fn [] (test-var v)))))))
125145
(catch Throwable e
126146
(report-fixture-error ns e)))))
127147

0 commit comments

Comments
 (0)