2015-05-27 13:07:51 -04:00
|
|
|
package interpolate
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2015-05-29 18:35:55 -04:00
|
|
|
func TestRenderInterface(t *testing.T) {
|
|
|
|
type Test struct {
|
|
|
|
Foo string
|
|
|
|
}
|
|
|
|
|
|
|
|
cases := map[string]struct {
|
|
|
|
Input interface{}
|
|
|
|
Output interface{}
|
|
|
|
}{
|
|
|
|
"basic": {
|
|
|
|
map[string]interface{}{
|
|
|
|
"foo": "{{upper `bar`}}",
|
|
|
|
},
|
|
|
|
map[string]interface{}{
|
|
|
|
"foo": "BAR",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
"struct": {
|
|
|
|
&Test{
|
|
|
|
Foo: "{{upper `bar`}}",
|
|
|
|
},
|
|
|
|
&Test{
|
|
|
|
Foo: "BAR",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := &Context{}
|
|
|
|
for k, tc := range cases {
|
|
|
|
actual, err := RenderInterface(tc.Input, ctx)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-27 13:07:51 -04:00
|
|
|
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,
|
|
|
|
},
|
|
|
|
|
2020-05-11 15:26:01 -04:00
|
|
|
// this test fails if you get github.com/mitchellh/reflectwalk@v1.0.1
|
|
|
|
// the fail is caused by
|
|
|
|
// https://github.com/mitchellh/reflectwalk/pull/22/commits/51d4c99fad9e9aa269e874bc3ad60313a574799f
|
|
|
|
// TODO: open a PR to fix it.
|
2015-05-27 13:07:51 -04:00
|
|
|
"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"},
|
|
|
|
},
|
|
|
|
},
|
2015-05-27 13:09:11 -04:00
|
|
|
|
|
|
|
"filter case-insensitive": {
|
|
|
|
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"},
|
|
|
|
},
|
|
|
|
},
|
2015-05-27 13:07:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|