Skip to content

Commit 24fd2ff

Browse files
authored
Support custom print-methods in test reports (#683)
1 parent 22c3356 commit 24fd2ff

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## master (unreleased)
44

5+
### Bugs fixed
6+
7+
* [#683](https://github.com/clojure-emacs/cider-nrepl/pull/683): Support custom `print-method`s in test reports.
8+
59
## 0.25.4 (2020-10-08)
610

711
### Changes

src/cider/nrepl/middleware/test.clj

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,26 +63,34 @@
6363
(filter #(= (:class %) (.getName (class f))))
6464
(first)))
6565

66+
(defn- print-object [object]
67+
(let [print-fn (if (= (get-method print-method (:type (meta object)))
68+
(get-method print-method :default))
69+
pp/pprint
70+
println)]
71+
(with-out-str (print-fn object))))
72+
6673
(defn test-result
6774
"Transform the result of a test assertion. Append ns, var, assertion index,
68-
and 'testing' context. Retain any exception. Pretty-print expected/actual."
75+
and 'testing' context. Retain any exception. Pretty-print expected/actual or
76+
use its `print-method`, if applicable."
6977
[ns v m]
7078
(let [{:keys [actual diffs expected fault]
7179
t :type} m
7280
c (when (seq test/*testing-contexts*) (test/testing-contexts-str))
7381
i (count (get-in (@current-report :results) [ns (:name (meta v))]))
74-
gen-input (:gen-input @current-report)
75-
pprint-str #(with-out-str (pp/pprint %))]
82+
gen-input (:gen-input @current-report)]
83+
7684
;; Errors outside assertions (faults) do not return an :expected value.
7785
;; Type :fail returns :actual value. Type :error returns :error and :line.
7886
(merge (dissoc m :expected :actual)
7987
{:ns ns, :var (:name (meta v)), :index i, :context c}
8088
(when (and (#{:fail :error} t) (not fault))
81-
{:expected (pprint-str expected)})
89+
{:expected (print-object expected)})
8290
(when (and (#{:fail} t) gen-input)
83-
{:gen-input (pprint-str gen-input)})
91+
{:gen-input (print-object gen-input)})
8492
(when (#{:fail} t)
85-
{:actual (pprint-str actual)})
93+
{:actual (print-object actual)})
8694
(when diffs
8795
{:diffs (extensions/diffs-result diffs)})
8896
(when (#{:error} t)

test/clj/cider/nrepl/middleware/test_test.clj

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,14 @@
140140
:test-with-map-as-message
141141
first
142142
:message))))))
143+
144+
(defmethod clojure.core/print-method ::custom [_ out]
145+
(binding [*out* out]
146+
(print "custom-output")))
147+
148+
(deftest print-object-test
149+
(testing "use an object's print-method when applicable, otherwise invoke pprint"
150+
(is (= "custom-output\n"
151+
(#'test/print-object (with-meta {:not :printed} {:type ::custom}))))
152+
(is (= "{:a :b, :c :d}\n"
153+
(#'test/print-object {:a :b :c :d})))))

0 commit comments

Comments
 (0)