terraform/config/lang/check_identifier_test.go

142 lines
2.4 KiB
Go
Raw Normal View History

package lang
import (
"testing"
"github.com/hashicorp/terraform/config/lang/ast"
)
func TestIdentifierCheck(t *testing.T) {
cases := []struct {
Input string
2015-01-15 05:58:46 +01:00
Scope ast.Scope
Error bool
}{
{
"foo",
2015-01-15 05:58:46 +01:00
&ast.BasicScope{},
false,
},
{
"foo ${bar} success",
2015-01-15 05:58:46 +01:00
&ast.BasicScope{
VarMap: map[string]ast.Variable{
"bar": ast.Variable{
Value: "baz",
Type: ast.TypeString,
},
},
},
false,
},
{
"foo ${bar}",
2015-01-15 05:58:46 +01:00
&ast.BasicScope{},
true,
},
{
"foo ${rand()} success",
2015-01-15 05:58:46 +01:00
&ast.BasicScope{
FuncMap: map[string]ast.Function{
"rand": ast.Function{
ReturnType: ast.TypeString,
Callback: func([]interface{}) (interface{}, error) {
return "42", nil
},
},
},
},
false,
},
{
"foo ${rand()}",
2015-01-15 05:58:46 +01:00
&ast.BasicScope{},
true,
},
{
"foo ${rand(42)} ",
2015-01-15 05:58:46 +01:00
&ast.BasicScope{
FuncMap: map[string]ast.Function{
"rand": ast.Function{
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
ReturnType: ast.TypeString,
Variadic: true,
VariadicType: ast.TypeInt,
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
ReturnType: ast.TypeString,
Variadic: true,
VariadicType: ast.TypeInt,
Callback: func([]interface{}) (interface{}, error) {
return "42", nil
},
},
},
},
false,
},
{
"foo ${rand(\"foo\", 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: []ast.Type{ast.TypeString},
ReturnType: ast.TypeString,
Variadic: true,
VariadicType: ast.TypeInt,
Callback: func([]interface{}) (interface{}, error) {
return "42", nil
},
},
},
},
false,
},
}
for _, tc := range cases {
node, err := Parse(tc.Input)
if err != nil {
t.Fatalf("Error: %s\n\nInput: %s", err, tc.Input)
}
visitor := &IdentifierCheck{Scope: tc.Scope}
err = visitor.Visit(node)
2015-10-08 14:48:04 +02:00
if err != nil != tc.Error {
t.Fatalf("Error: %s\n\nInput: %s", err, tc.Input)
}
}
}