Skip to content

Commit

Permalink
added maps tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yassinebenaid committed Jun 10, 2024
1 parent 2b57430 commit 502b8ee
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
9 changes: 8 additions & 1 deletion dumper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
25 changes: 25 additions & 0 deletions dumper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
16 changes: 16 additions & 0 deletions testdata/maps.txt
Original file line number Diff line number Diff line change
@@ -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,
},
},
}

0 comments on commit 502b8ee

Please sign in to comment.