Add more tests for unmarshal array tables
This commit is contained in:
+116
-71
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user