Added exta tests for GetArrayPath
This commit is contained in:
@@ -176,7 +176,6 @@ func (t *Tree) GetArrayPath(keys []string) interface{} {
|
|||||||
// if homogeneous array, then return slice type object over []interface{}
|
// if homogeneous array, then return slice type object over []interface{}
|
||||||
func getArray(n []interface{}) interface{} {
|
func getArray(n []interface{}) interface{} {
|
||||||
var s []string
|
var s []string
|
||||||
var b []byte
|
|
||||||
var i64 []int64
|
var i64 []int64
|
||||||
var f64 []float64
|
var f64 []float64
|
||||||
var bl []bool
|
var bl []bool
|
||||||
@@ -184,8 +183,6 @@ func getArray(n []interface{}) interface{} {
|
|||||||
switch v := value.(type) {
|
switch v := value.(type) {
|
||||||
case string:
|
case string:
|
||||||
s = append(s, v)
|
s = append(s, v)
|
||||||
case byte:
|
|
||||||
b = append(b, v)
|
|
||||||
case int64:
|
case int64:
|
||||||
i64 = append(i64, v)
|
i64 = append(i64, v)
|
||||||
case float64:
|
case float64:
|
||||||
@@ -198,8 +195,6 @@ func getArray(n []interface{}) interface{} {
|
|||||||
}
|
}
|
||||||
if len(s) == len(n) {
|
if len(s) == len(n) {
|
||||||
return s
|
return s
|
||||||
} else if len(b) == len(n) {
|
|
||||||
return b
|
|
||||||
} else if len(i64) == len(n) {
|
} else if len(i64) == len(n) {
|
||||||
return i64
|
return i64
|
||||||
} else if len(f64) == len(n) {
|
} else if len(f64) == len(n) {
|
||||||
|
|||||||
+32
-13
@@ -64,7 +64,7 @@ func TestTomlGetArray(t *testing.T) {
|
|||||||
t.Errorf("GetArray should return the []bool value")
|
t.Errorf("GetArray should return the []bool value")
|
||||||
}
|
}
|
||||||
|
|
||||||
expect3 := []float64{1.5,2.5}
|
expect3 := []float64{1.5, 2.5}
|
||||||
actual3 := tree.GetArray("test.key3").([]float64)
|
actual3 := tree.GetArray("test.key3").([]float64)
|
||||||
if !reflect.DeepEqual(actual3, expect3) {
|
if !reflect.DeepEqual(actual3, expect3) {
|
||||||
t.Errorf("GetArray should return the []float64 value")
|
t.Errorf("GetArray should return the []float64 value")
|
||||||
@@ -185,22 +185,41 @@ func TestTomlGetPath(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestTomlGetArrayPath(t *testing.T) {
|
func TestTomlGetArrayPath(t *testing.T) {
|
||||||
node := newTree()
|
|
||||||
//TODO: set other node data
|
|
||||||
|
|
||||||
for idx, item := range []struct {
|
for idx, item := range []struct {
|
||||||
Path []string
|
Name string
|
||||||
Expected *Tree
|
Path []string
|
||||||
|
Make func() (tree *Tree, expected interface{})
|
||||||
}{
|
}{
|
||||||
{ // empty path test
|
{
|
||||||
[]string{},
|
Name: "empty",
|
||||||
node,
|
Path: []string{},
|
||||||
|
Make: func() (tree *Tree, expected interface{}) {
|
||||||
|
tree = newTree()
|
||||||
|
expected = tree
|
||||||
|
return
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "int64",
|
||||||
|
Path: []string{"a"},
|
||||||
|
Make: func() (tree *Tree, expected interface{}) {
|
||||||
|
var err error
|
||||||
|
tree, err = Load(`a = [1,2,3]`)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
expected = []int64{1, 2, 3}
|
||||||
|
return
|
||||||
|
},
|
||||||
},
|
},
|
||||||
} {
|
} {
|
||||||
result := node.GetArrayPath(item.Path)
|
t.Run(item.Name, func(t *testing.T) {
|
||||||
if result != item.Expected {
|
tree, expected := item.Make()
|
||||||
t.Errorf("GetArrayPath[%d] %v - expected %v, got %v instead.", idx, item.Path, item.Expected, result)
|
result := tree.GetArrayPath(item.Path)
|
||||||
}
|
if !reflect.DeepEqual(result, expected) {
|
||||||
|
t.Errorf("GetArrayPath[%d] %v - expected %#v, got %#v instead.", idx, item.Path, expected, result)
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
tree, _ := Load("[foo.bar]\na=1\nb=2\n[baz.foo]\na=3\nb=4\n[gorf.foo]\na=5\nb=6")
|
tree, _ := Load("[foo.bar]\na=1\nb=2\n[baz.foo]\na=3\nb=4\n[gorf.foo]\na=5\nb=6")
|
||||||
|
|||||||
Reference in New Issue
Block a user