github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/config/lang/check_identifier_test.go (about)

     1  package lang
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/hashicorp/terraform/config/lang/ast"
     7  )
     8  
     9  func TestIdentifierCheck(t *testing.T) {
    10  	cases := []struct {
    11  		Input string
    12  		Scope ast.Scope
    13  		Error bool
    14  	}{
    15  		{
    16  			"foo",
    17  			&ast.BasicScope{},
    18  			false,
    19  		},
    20  
    21  		{
    22  			"foo ${bar} success",
    23  			&ast.BasicScope{
    24  				VarMap: map[string]ast.Variable{
    25  					"bar": ast.Variable{
    26  						Value: "baz",
    27  						Type:  ast.TypeString,
    28  					},
    29  				},
    30  			},
    31  			false,
    32  		},
    33  
    34  		{
    35  			"foo ${bar}",
    36  			&ast.BasicScope{},
    37  			true,
    38  		},
    39  
    40  		{
    41  			"foo ${rand()} success",
    42  			&ast.BasicScope{
    43  				FuncMap: map[string]ast.Function{
    44  					"rand": ast.Function{
    45  						ReturnType: ast.TypeString,
    46  						Callback: func([]interface{}) (interface{}, error) {
    47  							return "42", nil
    48  						},
    49  					},
    50  				},
    51  			},
    52  			false,
    53  		},
    54  
    55  		{
    56  			"foo ${rand()}",
    57  			&ast.BasicScope{},
    58  			true,
    59  		},
    60  
    61  		{
    62  			"foo ${rand(42)} ",
    63  			&ast.BasicScope{
    64  				FuncMap: map[string]ast.Function{
    65  					"rand": ast.Function{
    66  						ReturnType: ast.TypeString,
    67  						Callback: func([]interface{}) (interface{}, error) {
    68  							return "42", nil
    69  						},
    70  					},
    71  				},
    72  			},
    73  			true,
    74  		},
    75  
    76  		{
    77  			"foo ${rand()} ",
    78  			&ast.BasicScope{
    79  				FuncMap: map[string]ast.Function{
    80  					"rand": ast.Function{
    81  						ReturnType:   ast.TypeString,
    82  						Variadic:     true,
    83  						VariadicType: ast.TypeInt,
    84  						Callback: func([]interface{}) (interface{}, error) {
    85  							return "42", nil
    86  						},
    87  					},
    88  				},
    89  			},
    90  			false,
    91  		},
    92  
    93  		{
    94  			"foo ${rand(42)} ",
    95  			&ast.BasicScope{
    96  				FuncMap: map[string]ast.Function{
    97  					"rand": ast.Function{
    98  						ReturnType:   ast.TypeString,
    99  						Variadic:     true,
   100  						VariadicType: ast.TypeInt,
   101  						Callback: func([]interface{}) (interface{}, error) {
   102  							return "42", nil
   103  						},
   104  					},
   105  				},
   106  			},
   107  			false,
   108  		},
   109  
   110  		{
   111  			"foo ${rand(\"foo\", 42)} ",
   112  			&ast.BasicScope{
   113  				FuncMap: map[string]ast.Function{
   114  					"rand": ast.Function{
   115  						ArgTypes:     []ast.Type{ast.TypeString},
   116  						ReturnType:   ast.TypeString,
   117  						Variadic:     true,
   118  						VariadicType: ast.TypeInt,
   119  						Callback: func([]interface{}) (interface{}, error) {
   120  							return "42", nil
   121  						},
   122  					},
   123  				},
   124  			},
   125  			false,
   126  		},
   127  	}
   128  
   129  	for _, tc := range cases {
   130  		node, err := Parse(tc.Input)
   131  		if err != nil {
   132  			t.Fatalf("Error: %s\n\nInput: %s", err, tc.Input)
   133  		}
   134  
   135  		visitor := &IdentifierCheck{Scope: tc.Scope}
   136  		err = visitor.Visit(node)
   137  		if err != nil != tc.Error {
   138  			t.Fatalf("Error: %s\n\nInput: %s", err, tc.Input)
   139  		}
   140  	}
   141  }