Skip to content

Commit a244b87

Browse files
authored
Sort printed maps in test output (#917)
1 parent b70eeb7 commit a244b87

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
* [#913](https://github.com/clojure-emacs/cider-nrepl/pull/913): Disable background warmup of `orchard.java` cache.
2222
* [#913](https://github.com/clojure-emacs/cider-nrepl/pull/913): Enable background warmup of Compliment cache.
2323
* [#914](https://github.com/clojure-emacs/cider-nrepl/pull/914): Remove javadoc section from the inspector output.
24+
* [#917](https://github.com/clojure-emacs/cider-nrepl/pull/917): Sort printed maps in test output
2425

2526
## 0.52.1 (2025-02-24)
2627

src/cider/nrepl/middleware/test.clj

+20-1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,24 @@
7272
%))
7373
analyzed-trace))))
7474

75+
(defn deep-sorted-maps
76+
"Recursively converts all nested maps to sorted maps."
77+
[m]
78+
(try
79+
(walk/postwalk
80+
(fn [x]
81+
(if (map? x)
82+
(with-meta (into (sorted-map) x) (meta x))
83+
x))
84+
m)
85+
(catch Exception _
86+
;; Maps with non-comparable keys aren't sortable so they should be returned as-is.
87+
;; Examples:
88+
;; {{} 1}
89+
;; {1 1 :a 1}
90+
;; {"a" 1 :a 1}
91+
m)))
92+
7593
(defn- print-object
7694
"Print `object` using println for matcher-combinators results and pprint
7795
otherwise. The matcher-combinators library uses a custom print-method
@@ -83,7 +101,8 @@
83101
print-fn (if matcher-combinators-result?
84102
println
85103
pp/pprint)
86-
result (with-out-str (print-fn object))]
104+
;; The output will contain sorted maps for better readability and diff comparisons.
105+
result (with-out-str (print-fn (deep-sorted-maps object)))]
87106
;; Replace extra newlines at the end, as sometimes returned by matchers-combinators:
88107
(str/replace result #"\n\n+$" "\n")))
89108

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,10 @@
209209
"pprint is chosen, as indicated by quoted strings and newlines")
210210
(is (= "{:a \"b\", :c \"42\"}\n"
211211
(#'test/print-object (with-meta {:a "b" :c "42"} {:type ::mismatch})))
212-
"pprint is chosen, because :type does not match matchers-combinators keyword")))
212+
"pprint is chosen, because :type does not match matchers-combinators keyword"))
213+
(testing "maps are printed with sorted keys"
214+
(is (= "{:a 1, :b 2, :c 3, :d {x 1, y 2, z 3}}\n"
215+
(#'test/print-object {:b 2 :c 3 :a 1 :d {'z 3 'y 2 'x 1}})))))
213216

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

0 commit comments

Comments
 (0)