From 502b8eec23019ca01f229d0d5a73f416e5565415 Mon Sep 17 00:00:00 2001 From: yassinebenaid Date: Mon, 10 Jun 2024 20:26:44 +0100 Subject: [PATCH] added maps tests --- dumper.go | 9 ++++++++- dumper_test.go | 25 +++++++++++++++++++++++++ testdata/maps.txt | 16 ++++++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 testdata/maps.txt diff --git a/dumper.go b/dumper.go index 359efe2..28352b8 100644 --- a/dumper.go +++ b/dumper.go @@ -87,7 +87,14 @@ func (d *dumper) dumpSlice(v reflect.Value) { func (d *dumper) dumpMap(v reflect.Value) { keys := v.MapKeys() - d.write(d.theme.VarType.apply(fmt.Sprintf("%s:%d {", v.Type(), len(keys)))) + + var tag string + if d.ptrTag != 0 { + tag = d.theme.PointerCounter.apply(fmt.Sprintf("#%d", d.ptrTag)) + d.ptrTag = 0 + } + + d.write(d.theme.VarType.apply(fmt.Sprintf("%s:%d {%s", v.Type(), len(keys), tag))) d.depth++ for _, key := range keys { diff --git a/dumper_test.go b/dumper_test.go index 1cc5d1b..0fa57e6 100644 --- a/dumper_test.go +++ b/dumper_test.go @@ -590,6 +590,31 @@ func TestCanDumpSlices(t *testing.T) { checkFromFeed(t, d.buf, "./testdata/slices.txt") } +func TestCanDumpMaps(t *testing.T) { + + type SomeMap map[*SomeMap]*SomeMap + var sm = &SomeMap{} + + var m = map[any]any{12: 34} + maps := []any{ + make(map[string]string), + map[any]int{ + &m: 123, + }, + map[string]any{ + "cyclic": &m, + }, + SomeMap{ + &SomeMap{}: &SomeMap{sm: sm}, + }, + } + + var d dumper + d.dump(reflect.ValueOf(maps)) + + checkFromFeed(t, d.buf, "./testdata/maps.txt") +} + func checkFromFeed(t *testing.T, result []byte, feed_path string) { t.Helper() diff --git a/testdata/maps.txt b/testdata/maps.txt new file mode 100644 index 0000000..1c05ba5 --- /dev/null +++ b/testdata/maps.txt @@ -0,0 +1,16 @@ +[]interface {}:4:4 { + map[string]string:0 {}, + map[interface {}]int:1 { + &map[interface {}]interface {}:1 {#1 + 12: 34, + }: 123, + }, + map[string]interface {}:1 { + "cyclic": &@1, + }, + godump.SomeMap:1 { + &godump.SomeMap:0 {#2}: &godump.SomeMap:1 {#3 + &godump.SomeMap:0 {#4}: &@4, + }, + }, +} \ No newline at end of file