summaryrefslogtreecommitdiff
path: root/spew/dump_test.go
diff options
context:
space:
mode:
authorPatrick Mezard <[email protected]>2013-10-27 23:44:49 +0100
committerPatrick Mezard <[email protected]>2013-10-27 23:44:49 +0100
commit1fdf49f1b9ce7ab182b1b38bbc847b9807f66f3a (patch)
tree3bdd47a30426d70c79c8b9c66f16e42a620eff00 /spew/dump_test.go
parent1fe9f5ca4b46a8247ae5939097f5cfc5d8370729 (diff)
config: add SortKeys option to sort native map keys before display
If ConfigState.SortKeys is true, then dump and format will sort map keys before displaying them. Only native types (bool, ints, uint, uintptr, string) are supported, other slices are left unchanged. The motivation is to have more diffable output, mostly for test purpose.
Diffstat (limited to 'spew/dump_test.go')
-rw-r--r--spew/dump_test.go42
1 files changed, 42 insertions, 0 deletions
diff --git a/spew/dump_test.go b/spew/dump_test.go
index 31b7b6b..354c71c 100644
--- a/spew/dump_test.go
+++ b/spew/dump_test.go
@@ -65,6 +65,7 @@ import (
"bytes"
"fmt"
"github.com/davecgh/go-spew/spew"
+ "reflect"
"testing"
"unsafe"
)
@@ -896,3 +897,44 @@ func TestDump(t *testing.T) {
}
}
}
+
+func TestSortValues(t *testing.T) {
+ v := reflect.ValueOf
+
+ a := v("a")
+ b := v("b")
+ c := v("c")
+ tests := []struct {
+ input []reflect.Value
+ expected []reflect.Value
+ }{
+ {[]reflect.Value{v(2), v(1), v(3)},
+ []reflect.Value{v(1), v(2), v(3)}},
+ {[]reflect.Value{v(2.), v(1.), v(3.)},
+ []reflect.Value{v(1.), v(2.), v(3.)}},
+ {[]reflect.Value{v(false), v(true), v(false)},
+ []reflect.Value{v(false), v(false), v(true)}},
+ {[]reflect.Value{b, a, c},
+ []reflect.Value{a, b, c}},
+ }
+ for _, test := range tests {
+ spew.SortValues(test.input)
+ if !reflect.DeepEqual(test.input, test.expected) {
+ t.Errorf("Sort mismatch:\n %v != %v", test.input, test.expected)
+ }
+ }
+}
+
+func TestDumpSortedKeys(t *testing.T) {
+ cfg := spew.ConfigState{SortKeys: true}
+ s := cfg.Sdump(map[int]string{1: "1", 3: "3", 2: "2"})
+ expected := `(map[int]string) {
+(int) 1: (string) "1",
+(int) 2: (string) "2",
+(int) 3: (string) "3"
+}
+`
+ if s != expected {
+ t.Errorf("Sorted keys mismatch:\n %v %v", s, expected)
+ }
+}