Skip to content

Commit

Permalink
Fix a regression in test results formatting.
Browse files Browse the repository at this point in the history
The changes introduced in #683 resulted in test results always being
printed via println instead of clojure.pprint/pprint.

BetterThanTomorrow/calva#1280 describes the degraded user experience
caused by this issue.

This PR limits println usage to matchers-combinators results.
  • Loading branch information
temochka authored and bbatsov committed Oct 21, 2021
1 parent ebe962a commit dff71aa
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
14 changes: 9 additions & 5 deletions src/cider/nrepl/middleware/test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,16 @@
first))))

(defn- print-object
"Print `object` using pprint or a custom print-method, if available."
"Print `object` using println for matcher-combinators results and pprint
otherwise. The matcher-combinators library uses a custom print-method
which doesn't get picked up by pprint since it uses a different dispatch
mechanism."
[object]
(let [print-fn (if (= (get-method print-method (:type (meta object)))
(get-method print-method :default))
pp/pprint
println)]
(let [matcher-combinators-result? (= (:type (meta object))
:matcher-combinators.clj-test/mismatch)
print-fn (if matcher-combinators-result?
println
pp/pprint)]
(with-out-str (print-fn object))))

(def ^:dynamic *test-error-handler*
Expand Down
19 changes: 10 additions & 9 deletions test/clj/cider/nrepl/middleware/test_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -159,16 +159,17 @@
first
:message))))))

(defmethod clojure.core/print-method ::custom [_ out]
(binding [*out* out]
(print "custom-output")))

(deftest print-object-test
(testing "use an object's print-method when applicable, otherwise invoke pprint"
(is (= "custom-output\n"
(#'test/print-object (with-meta {:not :printed} {:type ::custom}))))
(is (= "{:a :b, :c :d}\n"
(#'test/print-object {:a :b :c :d})))))
(testing "uses println for matcher-combinators results, otherwise invokes pprint"
(is (= "{no quotes}\n"
(#'test/print-object (with-meta {"no" "quotes"} {:type :matcher-combinators.clj-test/mismatch})))
"println is chosen, as indicated by strings printed without quotes")
(is (= "{:a\n (\"a-sufficiently-long-string\"\n \"a-sufficiently-long-string\"\n \"a-sufficiently-long-string\")}\n"
(#'test/print-object {:a (repeat 3 "a-sufficiently-long-string")}))
"pprint is chosen, as indicated by quoted strings and newlines")
(is (= "{:a \"b\", :c \"42\"}\n"
(#'test/print-object (with-meta {:a "b" :c "42"} {:type ::mismatch})))
"pprint is chosen, because :type does not match matchers-combinators keyword")))

(deftest test-result-test
(testing "It passes `:error`s to `test/*test-error-handler*`"
Expand Down

0 comments on commit dff71aa

Please sign in to comment.