package lang import ( "testing" "github.com/hashicorp/terraform/config/lang/ast" ) func TestIdentifierCheck(t *testing.T) { cases := []struct { Input string Scope ast.Scope Error bool }{ { "foo", &ast.BasicScope{}, false, }, { "foo ${bar} success", &ast.BasicScope{ VarMap: map[string]ast.Variable{ "bar": ast.Variable{ Value: "baz", Type: ast.TypeString, }, }, }, false, }, { "foo ${bar}", &ast.BasicScope{}, true, }, { "foo ${rand()} success", &ast.BasicScope{ FuncMap: map[string]ast.Function{ "rand": ast.Function{ ReturnType: ast.TypeString, Callback: func([]interface{}) (interface{}, error) { return "42", nil }, }, }, }, false, }, { "foo ${rand()}", &ast.BasicScope{}, true, }, { "foo ${rand(42)} ", &ast.BasicScope{ FuncMap: map[string]ast.Function{ "rand": ast.Function{ ReturnType: ast.TypeString, Callback: func([]interface{}) (interface{}, error) { return "42", nil }, }, }, }, true, }, { "foo ${rand()} ", &ast.BasicScope{ FuncMap: map[string]ast.Function{ "rand": ast.Function{ ReturnType: ast.TypeString, Variadic: true, VariadicType: ast.TypeInt, Callback: func([]interface{}) (interface{}, error) { return "42", nil }, }, }, }, false, }, { "foo ${rand(42)} ", &ast.BasicScope{ FuncMap: map[string]ast.Function{ "rand": ast.Function{ ReturnType: ast.TypeString, Variadic: true, VariadicType: ast.TypeInt, Callback: func([]interface{}) (interface{}, error) { return "42", nil }, }, }, }, false, }, { "foo ${rand(\"foo\", 42)} ", &ast.BasicScope{ FuncMap: map[string]ast.Function{ "rand": ast.Function{ 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) if (err != nil) != tc.Error { t.Fatalf("Error: %s\n\nInput: %s", err, tc.Input) } } }