add get tpl e2e test

Signed-off-by: yxxhero <aiopsclub@163.com>
This commit is contained in:
yxxhero 2022-05-21 15:45:43 +08:00 committed by yxxhero
parent 23b6429f33
commit c34c7c87a1
1 changed files with 72 additions and 3 deletions

View File

@ -111,7 +111,6 @@ var readFileTestCases = []tmplTestCase{
} }
var toYamlTestCases = []tmplTestCase{ var toYamlTestCases = []tmplTestCase{
{ {
data: map[string]string{ data: map[string]string{
"test": "test", "test": "test",
@ -123,15 +122,82 @@ var toYamlTestCases = []tmplTestCase{
} }
var fromYamlTestCases = []tmplTestCase{ var fromYamlTestCases = []tmplTestCase{
{ {
name: "fromYaml", name: "fromYaml",
tmplString: `{{ $value := "test: test" | fromYaml }} tmplString: `{{ $value := "test: test" | fromYaml }}
{{- $value.test }}`, {{- $value.test }}`,
output: "test", output: "test",
}, },
} }
var setValueAtPathTestCases = []tmplTestCase{
{
data: map[string]interface{}{
"root": map[string]interface{}{
"testKey": map[string]interface{}{
"testKey2": "test",
},
},
},
name: "setValueAtPath",
tmplString: `{{- $newValues := . | setValueAtPath "root.testKey.testKey2" "testNewValue" }}
{{- $newValues.root.testKey.testKey2 }}`,
output: "testNewValue",
},
{
data: map[string]interface{}{
"root": "test",
},
wantErr: true,
name: "setValueAtPathWithInvalidPath",
tmplString: `{{ . | setValueAtPath "root.nokey" "testNewValue" }}`,
},
}
var getTestCases = []tmplTestCase{
{
data: map[string]interface{}{
"root": map[string]interface{}{
"testGetKey": map[string]interface{}{
"testGetKey2": "test",
},
},
},
name: "get",
tmplString: `{{- . | get "root.testGetKey.testGetKey2" "notfound" }}`,
output: "test",
},
{
data: map[string]interface{}{
"root": map[string]interface{}{
"testGetKey": map[string]interface{}{
"testGetKey2": "test",
},
},
},
name: "getNotExistWithDefault",
tmplString: `{{- . | get "root.testGetKey.testGetKey3" "notfound" }}`,
output: "notfound",
},
}
var tplTestCases = []tmplTestCase{
{
data: map[string]interface{}{
"root": "tplvalue",
},
name: "tpl",
tmplString: `{{ . | tpl "{{ .root }}" }}`,
output: "tplvalue",
},
{
data: map[string]interface{}{},
name: "tplInvalidTemplate",
wantErr: true,
tmplString: `{{ . | tpl "{{ .root }}" }}`,
},
}
// tmplTestCases are the test cases for the template tests // tmplTestCases are the test cases for the template tests
type tmplE2e struct { type tmplE2e struct {
tcs []tmplTestCase tcs []tmplTestCase
@ -151,6 +217,9 @@ func (t *tmplE2e) load() {
t.append(readFileTestCases...) t.append(readFileTestCases...)
t.append(toYamlTestCases...) t.append(toYamlTestCases...)
t.append(fromYamlTestCases...) t.append(fromYamlTestCases...)
t.append(setValueAtPathTestCases...)
t.append(getTestCases...)
t.append(tplTestCases...)
} }
var tmplE2eTest = tmplE2e{} var tmplE2eTest = tmplE2e{}