Allow unmarshaling to top level maps (#273)

This commit is contained in:
Gregory Oschwald
2019-04-24 23:15:40 -07:00
committed by Thomas Pelletier
parent 65b27e6823
commit 1d8903f1d0
2 changed files with 46 additions and 8 deletions
+35 -5
View File
@@ -1103,12 +1103,42 @@ func TestUnmarshalCustomTag(t *testing.T) {
}
func TestUnmarshalMap(t *testing.T) {
m := make(map[string]int)
m["a"] = 1
testToml := []byte(`
a = 1
b = 2
c = 3
`)
var result map[string]int
err := Unmarshal(testToml, &result)
if err != nil {
t.Errorf("Received unexpected error: %s", err)
return
}
err := Unmarshal(basicTestToml, m)
if err.Error() != "Only a pointer to struct can be unmarshaled from TOML" {
t.Fail()
expected := map[string]int{
"a": 1,
"b": 2,
"c": 3,
}
if !reflect.DeepEqual(result, expected) {
t.Errorf("Bad unmarshal: expected %v, got %v", expected, result)
}
}
func TestUnmarshalNonPointer(t *testing.T) {
a := 1
err := Unmarshal([]byte{}, a)
if err == nil {
t.Fatal("unmarshal should err when given a non pointer")
}
}
func TestUnmarshalInvalidPointerKind(t *testing.T) {
a := 1
err := Unmarshal([]byte{}, &a)
if err == nil {
t.Fatal("unmarshal should err when given an invalid pointer type")
}
}