pkg/eval/vals: Make Repr sort map keys before printing.

This addresses #1495.
This commit is contained in:
Qi Xiao 2023-05-20 10:52:17 +01:00
parent 7c0c3eb82d
commit 2fe4512556
3 changed files with 34 additions and 0 deletions

View File

@ -4,6 +4,8 @@ Draft release notes for Elvish 0.20.0.
- A new `os:` module providing access to operating system functionality.
- Maps now have their keys sorted when printed.
- The `peach` command now has a `&num-workers` option
([#648](https://github.com/elves/elvish/issues/648)).

View File

@ -5,6 +5,7 @@ import (
"math"
"math/big"
"reflect"
"sort"
"strconv"
"src.elv.sh/pkg/parse"
@ -67,8 +68,19 @@ func Repr(v any, indent int) string {
return b.String()
case Map:
builder := NewMapReprBuilder(indent)
// Collect all the key-value pairs.
pairs := make([][2]any, 0, v.Len())
for it := v.Iterator(); it.HasElem(); it.Next() {
k, v := it.Elem()
pairs = append(pairs, [2]any{k, v})
}
// Sort the pairs. See the godoc of CmpTotal for the sorting algorithm.
sort.Slice(pairs, func(i, j int) bool {
return CmpTotal(pairs[i][0], pairs[j][0]) == CmpLess
})
// Print the pairs.
for _, pair := range pairs {
k, v := pair[0], pair[1]
builder.WritePair(Repr(k, indent+1), indent+2, Repr(v, indent+2))
}
return builder.String()

View File

@ -38,8 +38,28 @@ func TestReprPlain(t *testing.T) {
Args(EmptyMap).Rets("[&]"),
Args(MakeMap("foo", "bar")).Rets("[&foo=bar]"),
// Keys of the same type are sorted.
Args(MakeMap("b", "second", "a", "first", "c", "third")).
Rets("[&a=first &b=second &c=third]"),
Args(MakeMap(2, "second", 1, "first", 3, "third")).
Rets("[&(num 1)=first &(num 2)=second &(num 3)=third]"),
// Keys of mixed types tested in a different test.
Args(reprer{}).Rets("<reprer>"),
Args(nonReprer{}).Rets("<unknown {}>"),
})
}
func TestReprPlain_MapWithKeysOfMixedTypes(t *testing.T) {
m := MakeMap(
"b", "second", "a", "first", "c", "third",
2, "second", 1, "first", 3, "third")
strPart := "&a=first &b=second &c=third"
numPart := "&(num 1)=first &(num 2)=second &(num 3)=third"
want1 := "[" + strPart + " " + numPart + "]"
want2 := "[" + numPart + " " + strPart + "]"
got := ReprPlain(m)
if got != want1 && got != want2 {
t.Errorf("got %q, want %q or %q", got, want1, want2)
}
}