terraform/config/lang/check_types_test.go

177 lines
3.0 KiB
Go
Raw Normal View History

package lang
import (
"testing"
"github.com/hashicorp/terraform/config/lang/ast"
)
2015-01-13 20:25:46 +01:00
func TestTypeCheck(t *testing.T) {
cases := []struct {
Input string
Scope *Scope
Error bool
}{
{
"foo",
&Scope{},
false,
},
{
"foo ${bar}",
&Scope{
VarMap: map[string]Variable{
"bar": Variable{
Value: "baz",
Type: ast.TypeString,
},
},
},
false,
},
{
"foo ${rand()}",
&Scope{
FuncMap: map[string]Function{
"rand": Function{
ReturnType: ast.TypeString,
Callback: func([]interface{}) (interface{}, error) {
return "42", nil
},
},
},
},
false,
},
2015-01-12 09:35:43 +01:00
{
`foo ${rand("42")}`,
&Scope{
2015-01-12 09:35:43 +01:00
FuncMap: map[string]Function{
"rand": Function{
ArgTypes: []ast.Type{ast.TypeString},
ReturnType: ast.TypeString,
Callback: func([]interface{}) (interface{}, error) {
return "42", nil
},
},
},
},
false,
},
{
`foo ${rand(42)}`,
&Scope{
2015-01-12 09:35:43 +01:00
FuncMap: map[string]Function{
"rand": Function{
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()}`,
&Scope{
FuncMap: map[string]Function{
"rand": Function{
ArgTypes: nil,
ReturnType: ast.TypeString,
Variadic: true,
VariadicType: ast.TypeString,
Callback: func([]interface{}) (interface{}, error) {
return "42", nil
},
},
},
},
false,
},
{
`foo ${rand("42")}`,
&Scope{
FuncMap: map[string]Function{
"rand": Function{
ArgTypes: nil,
ReturnType: ast.TypeString,
Variadic: true,
VariadicType: ast.TypeString,
Callback: func([]interface{}) (interface{}, error) {
return "42", nil
},
},
},
},
false,
},
{
`foo ${rand("42", 42)}`,
&Scope{
FuncMap: map[string]Function{
"rand": Function{
ArgTypes: nil,
ReturnType: ast.TypeString,
Variadic: true,
VariadicType: ast.TypeString,
Callback: func([]interface{}) (interface{}, error) {
return "42", nil
},
},
},
},
true,
},
{
"foo ${bar}",
&Scope{
VarMap: map[string]Variable{
"bar": Variable{
Value: 42,
Type: ast.TypeInt,
},
},
},
true,
},
2015-01-12 09:35:43 +01:00
{
"foo ${rand()}",
&Scope{
2015-01-12 09:35:43 +01:00
FuncMap: map[string]Function{
"rand": Function{
ReturnType: ast.TypeInt,
Callback: func([]interface{}) (interface{}, error) {
return 42, nil
},
},
},
},
true,
},
}
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}
err = visitor.Visit(node)
if (err != nil) != tc.Error {
t.Fatalf("Error: %s\n\nInput: %s", err, tc.Input)
}
}
}