Skip to content
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

Open
wants to merge 9 commits into
base: master
Choose a base branch
from

Conversation

sashton
Copy link

@sashton sashton commented Mar 5, 2025

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:

  • The commits are consistent with our contribution guidelines
  • You've added tests to cover your change(s)
  • All tests are passing
  • You've updated the README
  • Middleware documentation is up to date
    • Please check out and modify the cider.nrepl ns which has all middleware documentation.
    • Run lein docs afterwards, and commit the results.

Note: If you're just starting out to hack on cider-nrepl you might find
nREPL's documentation and the
"Design" section of the README extremely useful.*

Thanks!

(defn deep-sorted-maps
"Recursively converts all nested maps to sorted maps."
[m]
(try
Copy link
Member

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?

Copy link
Author

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.

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')

Copy link
Author

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)))]
Copy link
Member

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)
Copy link
Member

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?

Copy link
Author

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.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sashton
Copy link
Author

sashton commented Mar 5, 2025

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.

@sashton sashton closed this Mar 5, 2025
@sashton
Copy link
Author

sashton commented Mar 5, 2025

I determined that the deterministic behavior appears to be a factor of map size, and is probably some internal implementation of pprint. But that behavior does not help with printing test results, so this PR is still needed.

Details on the linked issue: #916 (comment)

@sashton sashton reopened this Mar 5, 2025
@sashton
Copy link
Author

sashton commented Mar 18, 2025

@bbatsov I'm not convinced that the test failure is related to my change, since the tests passed in a6dd36b when I first had the try/catch there. From the test output, it looks like it was an nrepl timeout. Does that happen intermittently with the tests?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Sorted maps in printed test results
3 participants