2015-01-12 08:38:21 +01:00
|
|
|
package lang
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/config/lang/ast"
|
|
|
|
)
|
|
|
|
|
2015-01-13 20:25:46 +01:00
|
|
|
func TestTypeCheck(t *testing.T) {
|
2015-01-12 08:38:21 +01:00
|
|
|
cases := []struct {
|
2015-01-12 19:21:18 +01:00
|
|
|
Input string
|
2015-01-15 05:58:46 +01:00
|
|
|
Scope ast.Scope
|
2015-01-12 19:21:18 +01:00
|
|
|
Error bool
|
2015-01-12 08:38:21 +01:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
"foo",
|
2015-01-15 05:58:46 +01:00
|
|
|
&ast.BasicScope{},
|
2015-01-12 08:38:21 +01:00
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"foo ${bar}",
|
2015-01-15 05:58:46 +01:00
|
|
|
&ast.BasicScope{
|
|
|
|
VarMap: map[string]ast.Variable{
|
|
|
|
"bar": ast.Variable{
|
2015-01-12 08:38:21 +01:00
|
|
|
Value: "baz",
|
|
|
|
Type: ast.TypeString,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"foo ${rand()}",
|
2015-01-15 05:58:46 +01:00
|
|
|
&ast.BasicScope{
|
|
|
|
FuncMap: map[string]ast.Function{
|
|
|
|
"rand": ast.Function{
|
2015-01-12 08:38:21 +01:00
|
|
|
ReturnType: ast.TypeString,
|
|
|
|
Callback: func([]interface{}) (interface{}, error) {
|
|
|
|
return "42", nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
2015-01-12 09:35:43 +01:00
|
|
|
{
|
|
|
|
`foo ${rand("42")}`,
|
2015-01-15 05:58:46 +01:00
|
|
|
&ast.BasicScope{
|
|
|
|
FuncMap: map[string]ast.Function{
|
|
|
|
"rand": ast.Function{
|
2015-01-12 09:35:43 +01:00
|
|
|
ArgTypes: []ast.Type{ast.TypeString},
|
|
|
|
ReturnType: ast.TypeString,
|
|
|
|
Callback: func([]interface{}) (interface{}, error) {
|
|
|
|
return "42", nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
`foo ${rand(42)}`,
|
2015-01-15 05:58:46 +01:00
|
|
|
&ast.BasicScope{
|
|
|
|
FuncMap: map[string]ast.Function{
|
|
|
|
"rand": ast.Function{
|
2015-01-12 09:35:43 +01:00
|
|
|
ArgTypes: []ast.Type{ast.TypeString},
|
|
|
|
ReturnType: ast.TypeString,
|
|
|
|
Callback: func([]interface{}) (interface{}, error) {
|
|
|
|
return "42", nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
|
2015-01-13 21:40:47 +01:00
|
|
|
{
|
|
|
|
`foo ${rand()}`,
|
2015-01-15 05:58:46 +01:00
|
|
|
&ast.BasicScope{
|
|
|
|
FuncMap: map[string]ast.Function{
|
|
|
|
"rand": ast.Function{
|
2015-01-13 21:40:47 +01:00
|
|
|
ArgTypes: nil,
|
|
|
|
ReturnType: ast.TypeString,
|
|
|
|
Variadic: true,
|
|
|
|
VariadicType: ast.TypeString,
|
|
|
|
Callback: func([]interface{}) (interface{}, error) {
|
|
|
|
return "42", nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
`foo ${rand("42")}`,
|
2015-01-15 05:58:46 +01:00
|
|
|
&ast.BasicScope{
|
|
|
|
FuncMap: map[string]ast.Function{
|
|
|
|
"rand": ast.Function{
|
2015-01-13 21:40:47 +01:00
|
|
|
ArgTypes: nil,
|
|
|
|
ReturnType: ast.TypeString,
|
|
|
|
Variadic: true,
|
|
|
|
VariadicType: ast.TypeString,
|
|
|
|
Callback: func([]interface{}) (interface{}, error) {
|
|
|
|
return "42", nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
`foo ${rand("42", 42)}`,
|
2015-01-15 05:58:46 +01:00
|
|
|
&ast.BasicScope{
|
|
|
|
FuncMap: map[string]ast.Function{
|
|
|
|
"rand": ast.Function{
|
2015-01-13 21:40:47 +01:00
|
|
|
ArgTypes: nil,
|
|
|
|
ReturnType: ast.TypeString,
|
|
|
|
Variadic: true,
|
|
|
|
VariadicType: ast.TypeString,
|
|
|
|
Callback: func([]interface{}) (interface{}, error) {
|
|
|
|
return "42", nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
|
2015-01-12 08:38:21 +01:00
|
|
|
{
|
|
|
|
"foo ${bar}",
|
2015-01-15 05:58:46 +01:00
|
|
|
&ast.BasicScope{
|
|
|
|
VarMap: map[string]ast.Variable{
|
|
|
|
"bar": ast.Variable{
|
2015-01-12 08:38:21 +01:00
|
|
|
Value: 42,
|
|
|
|
Type: ast.TypeInt,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
2015-01-12 09:35:43 +01:00
|
|
|
|
|
|
|
{
|
|
|
|
"foo ${rand()}",
|
2015-01-15 05:58:46 +01:00
|
|
|
&ast.BasicScope{
|
|
|
|
FuncMap: map[string]ast.Function{
|
|
|
|
"rand": ast.Function{
|
2015-01-12 09:35:43 +01:00
|
|
|
ReturnType: ast.TypeInt,
|
|
|
|
Callback: func([]interface{}) (interface{}, error) {
|
|
|
|
return 42, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
2015-01-12 08:38:21 +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-13 20:25:46 +01:00
|
|
|
visitor := &TypeCheck{Scope: tc.Scope}
|
2015-01-12 19:21:18 +01:00
|
|
|
err = visitor.Visit(node)
|
2015-01-12 08:38:21 +01:00
|
|
|
if (err != nil) != tc.Error {
|
|
|
|
t.Fatalf("Error: %s\n\nInput: %s", err, tc.Input)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-01-14 20:47:20 +01:00
|
|
|
|
|
|
|
func TestTypeCheck_implicit(t *testing.T) {
|
|
|
|
implicitMap := map[ast.Type]map[ast.Type]string{
|
|
|
|
ast.TypeInt: {
|
|
|
|
ast.TypeString: "intToString",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
Input string
|
2015-01-15 05:58:46 +01:00
|
|
|
Scope *ast.BasicScope
|
2015-01-14 20:47:20 +01:00
|
|
|
Error bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"foo ${bar}",
|
2015-01-15 05:58:46 +01:00
|
|
|
&ast.BasicScope{
|
|
|
|
VarMap: map[string]ast.Variable{
|
|
|
|
"bar": ast.Variable{
|
2015-01-14 20:47:20 +01:00
|
|
|
Value: 42,
|
|
|
|
Type: ast.TypeInt,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"foo ${foo(42)}",
|
2015-01-15 05:58:46 +01:00
|
|
|
&ast.BasicScope{
|
|
|
|
FuncMap: map[string]ast.Function{
|
|
|
|
"foo": ast.Function{
|
2015-01-14 20:47:20 +01:00
|
|
|
ArgTypes: []ast.Type{ast.TypeString},
|
|
|
|
ReturnType: ast.TypeString,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
`foo ${foo("42", 42)}`,
|
2015-01-15 05:58:46 +01:00
|
|
|
&ast.BasicScope{
|
|
|
|
FuncMap: map[string]ast.Function{
|
|
|
|
"foo": ast.Function{
|
2015-01-14 20:47:20 +01:00
|
|
|
ArgTypes: []ast.Type{ast.TypeString},
|
|
|
|
Variadic: true,
|
|
|
|
VariadicType: ast.TypeString,
|
|
|
|
ReturnType: ast.TypeString,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
node, err := Parse(tc.Input)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error: %s\n\nInput: %s", err, tc.Input)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Modify the scope to add our conversion functions.
|
|
|
|
if tc.Scope.FuncMap == nil {
|
2015-01-15 05:58:46 +01:00
|
|
|
tc.Scope.FuncMap = make(map[string]ast.Function)
|
2015-01-14 20:47:20 +01:00
|
|
|
}
|
2015-01-15 05:58:46 +01:00
|
|
|
tc.Scope.FuncMap["intToString"] = ast.Function{
|
2015-01-14 20:47:20 +01:00
|
|
|
ArgTypes: []ast.Type{ast.TypeInt},
|
|
|
|
ReturnType: ast.TypeString,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do the first pass...
|
|
|
|
visitor := &TypeCheck{Scope: tc.Scope, Implicit: implicitMap}
|
|
|
|
err = visitor.Visit(node)
|
|
|
|
if (err != nil) != tc.Error {
|
|
|
|
t.Fatalf("Error: %s\n\nInput: %s", err, tc.Input)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we didn't error, then the next type check should not fail
|
|
|
|
// WITHOUT implicits.
|
|
|
|
visitor = &TypeCheck{Scope: tc.Scope}
|
|
|
|
err = visitor.Visit(node)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error: %s\n\nInput: %s", err, tc.Input)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|