storage/yaml_test.go

63 lines
1.4 KiB
Go

package storage
import (
"fmt"
"testing"
)
func TestKeysDFS(t *testing.T) {
cases := map[string]struct {
input map[string]interface{}
want [][]string
}{
"empty": {
input: map[string]interface{}{},
want: [][]string{},
},
"top level non map keys": {
input: map[string]interface{}{"a": "b", "c": "d"},
want: [][]string{},
},
"top level non map keys and map key": {
input: map[string]interface{}{"a": "b", "c": "d", "e": map[string]interface{}{"f": "g"}},
want: [][]string{[]string{"e"}},
},
"top level non map keys and map key and nested, ignore empty nested": {
input: map[string]interface{}{
"a": "b",
"c": "d",
"e": map[string]interface{}{
"f": map[string]interface{}{"g": "h"},
},
"i": map[string]interface{}{},
"j": map[string]interface{}{"k": "l"},
},
want: [][]string{[]string{"e", "f"}, []string{"j"}},
},
}
for name, d := range cases {
c := d
t.Run(name, func(t *testing.T) {
got, err := keysDFS(c.input)
if err != nil {
t.Fatal(err)
}
for j := range c.want {
found := false
for i := range got {
if fmt.Sprint(got[i]) == fmt.Sprint(c.want[j]) {
found = true
}
}
if !found {
t.Errorf("want %+v among %+v", c.want[j], got)
}
}
if fmt.Sprintf("%+v", got) != fmt.Sprintf("%+v", c.want) {
t.Fatalf("want: %+v\ngot: %+v", c.want, got)
}
})
}
}