From 70d41bd75052b8b4ed8860b728f3dba2cdfc53e3 Mon Sep 17 00:00:00 2001 From: Thomas Pelletier Date: Mon, 8 Feb 2021 09:16:26 -0500 Subject: [PATCH] Add more tests for unmarshal array tables --- unmarshal_test.go | 187 ++++++++++++++++++++++++++++------------------ 1 file changed, 116 insertions(+), 71 deletions(-) diff --git a/unmarshal_test.go b/unmarshal_test.go index 1319027..883a2e6 100644 --- a/unmarshal_test.go +++ b/unmarshal_test.go @@ -116,75 +116,120 @@ Name = "Nail" assert.Equal(t, expected, x) } -//func TestUnmarshalArrayTablesMultiple(t *testing.T) { -// doc := ` -//[[Products]] -//Name = "Hammer" -//Sku = "738594937" -// -//[[Products]] # empty table within the array -// -//[[Products]] -//Name = "Nail" -//Sku = "284758393" -// -//Color = "gray" -//` -// -// type Product struct { -// Name string -// Sku string -// Color string -// } -// -// type Data struct { -// Products []Product -// } -// -// x := Data{} -// err := Unmarshal([]byte(doc), &x) -// -// require.NoError(t, err) -// -// expected := Data{ -// Products: []Product{ -// { -// Name: "Hammer", -// Sku: "738594937", -// }, -// {}, -// { -// Name: "Nail", -// Sku: "284758393", -// Color: "gray", -// }, -// }, -// } -// -// assert.Equal(t, expected, x) -//} +func TestUnmarshalArrayTablesMultiple(t *testing.T) { + doc := ` +[[Products]] +Name = "Hammer" +Sku = "738594937" -//func TestUnmarshalArrayTablesNested(t *testing.T) { -// doc := ` -//[[Fruits]] -//Name = "apple" -// -//[Fruits.Physical] # subtable -//Color = "red" -//Shape = "round" -// -//[[Fruits.Varieties]] # nested array of tables -//Name = "red delicious" -// -//[[fruits.varieties]] -//Name = "granny smith" -// -// -//[[fruits]] -//Name = "banana" -// -//[[fruits.varieties]] -//Name = "plantain" -//` -// -//} +[[Products]] # empty table within the array + +[[Products]] +Name = "Nail" +Sku = "284758393" + +Color = "gray" +` + + type Product struct { + Name string + Sku string + Color string + } + + type Data struct { + Products []Product + } + + x := Data{} + err := Unmarshal([]byte(doc), &x) + + require.NoError(t, err) + + expected := Data{ + Products: []Product{ + { + Name: "Hammer", + Sku: "738594937", + }, + {}, + { + Name: "Nail", + Sku: "284758393", + Color: "gray", + }, + }, + } + + assert.Equal(t, expected, x) +} + +func TestUnmarshalArrayTablesNested(t *testing.T) { + doc := ` +[[Fruits]] +Name = "apple" + +[Fruits.Physical] # subtable +Color = "red" +Shape = "round" + +[[Fruits.Varieties]] # nested array of tables +Name = "red delicious" + +[[Fruits.Varieties]] +Name = "granny smith" + + +[[Fruits]] +Name = "banana" + +[[Fruits.Varieties]] +Name = "plantain" +` + type Variety struct { + Name string + } + + type Physical struct { + Color string + Shape string + } + + type Fruit struct { + Name string + Physical Physical + Varieties []Variety + } + + type Doc struct { + Fruits []Fruit + } + + x := Doc{} + err := Unmarshal([]byte(doc), &x) + require.NoError(t, err) + + expected := Doc{ + Fruits: []Fruit{ + { + Name: "apple", + Physical: Physical{ + Color: "red", + Shape: "round", + }, + Varieties: []Variety{ + {Name: "red delicious"}, + {Name: "granny smith"}, + }, + }, + { + Name: "banana", + Varieties: []Variety{ + {Name: "plantain"}, + }, + }, + }, + } + + assert.Equal(t, expected, x) +}