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

Sorted maps in printed test results #916

Open
sashton opened this issue Mar 5, 2025 · 2 comments · May be fixed by #917
Open

Sorted maps in printed test results #916

sashton opened this issue Mar 5, 2025 · 2 comments · May be fixed by #917

Comments

@sashton
Copy link

sashton commented Mar 5, 2025

Currently printed test results (:expected, actual, and gen-input) are printed with unsorted maps. Some editors/tools could take advantage of providing a useful diff (beyond what :diffs provides) if the printed results included sorted maps.

The sorting can't be done on the IDE side, because it is a printed string at that point, not clojure data anymore.

The following is an example using VSCode, where the VSCode api is called with:

vscode.TestMessage.diff(cider.shortMessage(result), result.expected, result.actual)

Current diff (unsorted maps):

Image

Improved diff (with sorted maps):

Image

@sashton sashton linked a pull request Mar 5, 2025 that will close this issue
5 tasks
@sashton
Copy link
Author

sashton commented Mar 5, 2025

I'm seeing some unexpectedly deterministic (thought not sorted) results when using a released version of CIDER. Which, if true, would mean this issue is not relevant. 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 as completed Mar 5, 2025
@sashton
Copy link
Author

sashton commented Mar 5, 2025

Ok, it looks like I've run into some internal implementation of pprint, where if a map is big enough, it will be printed in a deterministic (but not sorted) order. But for small enough maps, the printed order is random.

Therefore, this issue is still needed to make test output diffable (for smaller maps, anyway).


Description of my findings

For large enough maps, shuffling the keys and reinserting into a map has no effect on the final sort:

(->>
 {:n 10000001,
  :d 10000004,
  :z 10000000,
  :o 10000007,
  :c 10000008,
  :j 10000005,
  :f 10000006,
  :p 10000003,
  :a 10000002}
 (vec)
 (shuffle)
 (into {})
 (clojure.pprint/pprint))

This will always print in this order (on my machine anyway):

{:o 10000007,
 :n 10000001,
 :z 10000000,
 :c 10000008,
 :j 10000005,
 :d 10000004,
 :f 10000006,
 :p 10000003,
 :a 10000002}

Even changing the order of the original map data has no effect on the output.

But for a map of just one size smaller:

(->>
 {:n 10000001,
  :d 10000004,
  :z 10000000,
  ;; :o 10000007,
  :c 10000008,
  :j 10000005,
  :f 10000006,
  :p 10000003,
  :a 10000002}
 (vec)
 (shuffle)
 (into {})
 (clojure.pprint/pprint))

The output is shuffled every time:

=> 
{:p 10000003,
 :c 10000008,
 :d 10000004,
 :a 10000002,
 :n 10000001,
 :f 10000006,
 :z 10000000,
 :j 10000005}

=> 
{:f 10000006,
 :z 10000000,
 :p 10000003,
 :n 10000001,
 :d 10000004,
 :a 10000002,
 :j 10000005,
 :c 10000008}
 
=>
{:a 10000002,
 :j 10000005,
 :d 10000004,
 :z 10000000,
 :c 10000008,
 :f 10000006,
 :n 10000001,
 :p 10000003}

@sashton sashton reopened this Mar 5, 2025
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 a pull request may close this issue.

1 participant