-
Notifications
You must be signed in to change notification settings - Fork 179
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sort printed maps in test output #917
base: master
Are you sure you want to change the base?
Sort printed maps in test output #917
Conversation
(defn deep-sorted-maps | ||
"Recursively converts all nested maps to sorted maps." | ||
[m] | ||
(try |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a chance for this to throw an exception?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, I suppose not. I had that in there when I was first testing it out locally and never removed it. But I don't see it being necessary. I'll remove it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In general, this will throw if the keys of any nested map are not comparable.
user=> (deep-sorted-maps {{} 1})
Execution error (ClassCastException) at user/deep-sorted-maps$fn (REPL:7).
Default comparator requires nil, Number, or Comparable: {}
user=> (deep-sorted-maps {1 1 :a 1})
Execution error (ClassCastException) at user/deep-sorted-maps$fn (REPL:7).
class java.lang.Long cannot be cast to class clojure.lang.Keyword (java.lang.Long is in module java.base of loader 'bootstrap'; clojure.lang.Keyword is in unnamed module of loader 'app')
user=> (deep-sorted-maps {"a" 1 :a 1})
Execution error (ClassCastException) at user/deep-sorted-maps$fn (REPL:7).
class java.lang.String cannot be cast to class clojure.lang.Keyword (java.lang.String is in module java.base of loader 'bootstrap'; clojure.lang.Keyword is in unnamed module of loader 'app')
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@frenchy64 That's a great catch! (no pun intended) I'll add a comment explaining the rationale for the try/catch here.
@@ -91,7 +104,7 @@ | |||
print-fn (if matcher-combinators-result? | |||
println | |||
pp/pprint) | |||
result (with-out-str (print-fn object))] | |||
result (with-out-str (print-fn (deep-sorted-maps object)))] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd probably add a note here why we're sorting maps.
(try | ||
(walk/postwalk | ||
(fn [x] | ||
(if (map? x) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Won't it be better to check around the call site if something's map?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Which call site do you mean? If you are referring to the call in print-object
, we want to do a postwalk traversal to sort nested maps as well as top-level maps.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here is an example nested map in the test:
https://github.com/clojure-emacs/cider-nrepl/pull/917/files#diff-d6825defeb66863e777ca37cd35612c08feadc23a12cab7d9b7d7791f00d407dR285
Alright, I'm seeing some unexpectedly deterministic (thought not sorted) results when using a released version of CIDER. Which, if true, would mean this PR is not needed. So I'm going to close this for the moment while I check my setup and make sure I know what is going on. |
I determined that the deterministic behavior appears to be a factor of map size, and is probably some internal implementation of Details on the linked issue: #916 (comment) |
This reverts commit ec61b70.
Fixes #916
This PR performs a clojure.walk to covert all maps to sorted-maps when printing an object in the test results. See #916 for example before and after screenshots of the diffed data.
Before submitting a PR make sure the following things have been done:
cider.nrepl
ns which has all middleware documentation.lein docs
afterwards, and commit the results.Note: If you're just starting out to hack on
cider-nrepl
you might findnREPL's documentation and the
"Design" section of the README extremely useful.*
Thanks!