add test case

Signed-off-by: zwwhdls <zwwhdls@hotmail.com>
This commit is contained in:
zwwhdls 2019-12-08 11:30:11 +08:00
parent 4532485fd0
commit 1b39857ac3
2 changed files with 109 additions and 37 deletions

View File

@ -308,7 +308,10 @@ func (t *parser) listItem(list []interface{}, i int) ([]interface{}, error) {
var crtList []interface{} var crtList []interface{}
if len(list) > i { if len(list) > i {
// If nested list already exists, take the value of list to next cycle. // If nested list already exists, take the value of list to next cycle.
crtList = list[i].([]interface{}) existed := list[i]
if existed != nil {
crtList = list[i].([]interface{})
}
} }
// Now we need to get the value after the ]. // Now we need to get the value after the ].
<<<<<<< HEAD <<<<<<< HEAD

View File

@ -421,49 +421,118 @@ func TestParseSet(t *testing.T) {
} }
func TestParseInto(t *testing.T) { func TestParseInto(t *testing.T) {
got := map[string]interface{}{ tests := []struct {
"outer": map[string]interface{}{ input string
"inner1": "overwrite", input2 string
"inner2": "value2", got map[string]interface{}
}, expect map[string]interface{}
} err bool
input := "outer.inner1=value1,outer.inner3=value3,outer.inner4=4,listOuter[0][0].type=listValue" }{
expect := map[string]interface{}{ {
"outer": map[string]interface{}{ input: "outer.inner1=value1,outer.inner3=value3,outer.inner4=4",
"inner1": "value1", got: map[string]interface{}{
"inner2": "value2", "outer": map[string]interface{}{
"inner3": "value3", "inner1": "overwrite",
"inner4": 4, "inner2": "value2",
}, },
"listOuter": [][]interface{}{{map[string]string{ },
"type": "listValue", expect: map[string]interface{}{
"status": "alive", "outer": map[string]interface{}{
}}, "inner1": "value1",
"inner2": "value2",
"inner3": "value3",
"inner4": 4,
}},
err: false,
},
{
input: "listOuter[0][0].type=listValue",
input2: "listOuter[0][0].status=alive",
got: map[string]interface{}{},
expect: map[string]interface{}{
"listOuter": [][]interface{}{{map[string]string{
"type": "listValue",
"status": "alive",
}}},
},
err: false,
},
{
input: "listOuter[0][0].type=listValue",
input2: "listOuter[1][0].status=alive",
got: map[string]interface{}{},
expect: map[string]interface{}{
"listOuter": [][]interface{}{
{
map[string]string{"type": "listValue"},
},
{
map[string]string{"status": "alive"},
},
},
},
err: false,
},
{
input: "listOuter[0][1][0].type=listValue",
input2: "listOuter[0][0][1].status=alive",
got: map[string]interface{}{
"listOuter": []interface{}{
[]interface{}{
[]interface{}{
map[string]string{"exited": "old"},
},
},
},
},
expect: map[string]interface{}{
"listOuter": [][][]interface{}{
{
{
map[string]string{"exited": "old"},
map[string]string{"status": "alive"},
},
{
map[string]string{"type": "listValue"},
},
},
},
},
err: false,
}, },
} }
for _, tt := range tests {
if err := ParseInto(tt.input, tt.got); err != nil {
t.Fatal(err)
}
if tt.err {
t.Errorf("%s: Expected error. Got nil", tt.input)
}
if err := ParseInto(input, got); err != nil { if tt.input2 != "" {
t.Fatal(err) if err := ParseInto(tt.input2, tt.got); err != nil {
} t.Fatal(err)
}
if tt.err {
t.Errorf("%s: Expected error. Got nil", tt.input2)
}
}
input2 := "listOuter[0][0].status=alive" y1, err := yaml.Marshal(tt.expect)
if err := ParseInto(input2, got); err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
y2, err := yaml.Marshal(tt.got)
if err != nil {
t.Fatalf("Error serializing parsed value: %s", err)
}
y1, err := yaml.Marshal(expect) if string(y1) != string(y2) {
if err != nil { t.Errorf("%s: Expected:\n%s\nGot:\n%s", tt.input, y1, y2)
t.Fatal(err) }
}
y2, err := yaml.Marshal(got)
if err != nil {
t.Fatalf("Error serializing parsed value: %s", err)
}
if string(y1) != string(y2) {
t.Errorf("%s: Expected:\n%s\nGot:\n%s", input, y1, y2)
} }
} }
func TestParseIntoString(t *testing.T) { func TestParseIntoString(t *testing.T) {
got := map[string]interface{}{ got := map[string]interface{}{
"outer": map[string]interface{}{ "outer": map[string]interface{}{