terraform/config/lang/engine_test.go

101 lines
1.8 KiB
Go
Raw Normal View History

2015-01-12 00:26:54 +01:00
package lang
import (
"reflect"
"testing"
"github.com/hashicorp/terraform/config/lang/ast"
)
func TestEngineExecute(t *testing.T) {
cases := []struct {
Input string
2015-01-12 18:57:16 +01:00
Scope *Scope
2015-01-12 00:26:54 +01:00
Error bool
Result interface{}
ResultType ast.Type
}{
{
"foo",
2015-01-12 18:57:16 +01:00
nil,
2015-01-12 00:26:54 +01:00
false,
"foo",
ast.TypeString,
},
{
"foo ${bar}",
2015-01-12 18:57:16 +01:00
&Scope{
2015-01-12 00:26:54 +01:00
VarMap: map[string]Variable{
"bar": Variable{
Value: "baz",
Type: ast.TypeString,
},
},
},
false,
"foo baz",
ast.TypeString,
},
2015-01-12 00:33:24 +01:00
{
"foo ${rand()}",
2015-01-12 18:57:16 +01:00
&Scope{
2015-01-12 00:33:24 +01:00
FuncMap: map[string]Function{
"rand": Function{
ReturnType: ast.TypeString,
Callback: func([]interface{}) (interface{}, error) {
return "42", nil
},
},
},
},
false,
"foo 42",
ast.TypeString,
},
2015-01-13 21:40:47 +01:00
{
`foo ${rand("foo", "bar")}`,
&Scope{
FuncMap: map[string]Function{
"rand": Function{
ReturnType: ast.TypeString,
Variadic: true,
VariadicType: ast.TypeString,
Callback: func(args []interface{}) (interface{}, error) {
var result string
for _, a := range args {
result += a.(string)
}
return result, nil
},
},
},
},
false,
"foo foobar",
ast.TypeString,
},
2015-01-12 00:26:54 +01:00
}
for _, tc := range cases {
node, err := Parse(tc.Input)
if err != nil {
t.Fatalf("Error: %s\n\nInput: %s", err, tc.Input)
}
2015-01-12 18:57:16 +01:00
engine := &Engine{GlobalScope: tc.Scope}
out, outType, err := engine.Execute(node)
2015-01-12 00:26:54 +01:00
if (err != nil) != tc.Error {
t.Fatalf("Error: %s\n\nInput: %s", err, tc.Input)
}
if outType != tc.ResultType {
t.Fatalf("Bad: %s\n\nInput: %s", outType, tc.Input)
}
if !reflect.DeepEqual(out, tc.Result) {
t.Fatalf("Bad: %#v\n\nInput: %s", out, tc.Input)
}
}
}