packer-cn/template/interpolate/render_test.go

93 lines
1.6 KiB
Go
Raw Normal View History

package interpolate
import (
"reflect"
"testing"
)
func TestRenderMap(t *testing.T) {
cases := map[string]struct {
Input interface{}
Output interface{}
Filter *RenderFilter
}{
"basic": {
map[string]interface{}{
"foo": "{{upper `bar`}}",
},
map[string]interface{}{
"foo": "BAR",
},
nil,
},
"map keys shouldn't be interpolated": {
map[string]interface{}{
"{{foo}}": "{{upper `bar`}}",
},
map[string]interface{}{
"{{foo}}": "BAR",
},
nil,
},
"nested values": {
map[string]interface{}{
"foo": map[string]string{
"bar": "{{upper `baz`}}",
},
},
map[string]interface{}{
"foo": map[string]string{
"bar": "BAZ",
},
},
nil,
},
"nested value keys": {
map[string]interface{}{
"foo": map[string]string{
"{{upper `bar`}}": "{{upper `baz`}}",
},
},
map[string]interface{}{
"foo": map[string]string{
"BAR": "BAZ",
},
},
nil,
},
"filter": {
map[string]interface{}{
"bar": "{{upper `baz`}}",
"foo": map[string]string{
"{{upper `bar`}}": "{{upper `baz`}}",
},
},
map[string]interface{}{
"bar": "BAZ",
"foo": map[string]string{
"{{upper `bar`}}": "{{upper `baz`}}",
},
},
&RenderFilter{
Include: []string{"bar"},
},
},
}
ctx := &Context{}
for k, tc := range cases {
actual, err := RenderMap(tc.Input, ctx, tc.Filter)
if err != nil {
t.Fatalf("err: %s\n\n%s", k, err)
}
if !reflect.DeepEqual(actual, tc.Output) {
t.Fatalf("err: %s\n\n%#v\n\n%#v", k, actual, tc.Output)
}
}
}