github.com/arvindram03/terraform@v0.3.7-0.20150212015210-408f838db36d/config/lang/parse_test.go (about)

     1  package lang
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/config/lang/ast"
     8  )
     9  
    10  func TestParse(t *testing.T) {
    11  	cases := []struct {
    12  		Input  string
    13  		Error  bool
    14  		Result ast.Node
    15  	}{
    16  		{
    17  			"",
    18  			false,
    19  			&ast.LiteralNode{
    20  				Value: "",
    21  				Typex: ast.TypeString,
    22  				Posx:  ast.Pos{Column: 1, Line: 1},
    23  			},
    24  		},
    25  
    26  		{
    27  			"foo",
    28  			false,
    29  			&ast.LiteralNode{
    30  				Value: "foo",
    31  				Typex: ast.TypeString,
    32  				Posx:  ast.Pos{Column: 1, Line: 1},
    33  			},
    34  		},
    35  
    36  		{
    37  			"$${var.foo}",
    38  			false,
    39  			&ast.LiteralNode{
    40  				Value: "${var.foo}",
    41  				Typex: ast.TypeString,
    42  				Posx:  ast.Pos{Column: 1, Line: 1},
    43  			},
    44  		},
    45  
    46  		{
    47  			"foo ${var.bar}",
    48  			false,
    49  			&ast.Concat{
    50  				Posx: ast.Pos{Column: 1, Line: 1},
    51  				Exprs: []ast.Node{
    52  					&ast.LiteralNode{
    53  						Value: "foo ",
    54  						Typex: ast.TypeString,
    55  						Posx:  ast.Pos{Column: 1, Line: 1},
    56  					},
    57  					&ast.VariableAccess{
    58  						Name: "var.bar",
    59  						Posx: ast.Pos{Column: 7, Line: 1},
    60  					},
    61  				},
    62  			},
    63  		},
    64  
    65  		{
    66  			"foo ${var.bar} baz",
    67  			false,
    68  			&ast.Concat{
    69  				Posx: ast.Pos{Column: 1, Line: 1},
    70  				Exprs: []ast.Node{
    71  					&ast.LiteralNode{
    72  						Value: "foo ",
    73  						Typex: ast.TypeString,
    74  						Posx:  ast.Pos{Column: 1, Line: 1},
    75  					},
    76  					&ast.VariableAccess{
    77  						Name: "var.bar",
    78  						Posx: ast.Pos{Column: 7, Line: 1},
    79  					},
    80  					&ast.LiteralNode{
    81  						Value: " baz",
    82  						Typex: ast.TypeString,
    83  						Posx:  ast.Pos{Column: 15, Line: 1},
    84  					},
    85  				},
    86  			},
    87  		},
    88  
    89  		{
    90  			"foo ${\"bar\"}",
    91  			false,
    92  			&ast.Concat{
    93  				Posx: ast.Pos{Column: 1, Line: 1},
    94  				Exprs: []ast.Node{
    95  					&ast.LiteralNode{
    96  						Value: "foo ",
    97  						Typex: ast.TypeString,
    98  						Posx:  ast.Pos{Column: 1, Line: 1},
    99  					},
   100  					&ast.LiteralNode{
   101  						Value: "bar",
   102  						Typex: ast.TypeString,
   103  						Posx:  ast.Pos{Column: 7, Line: 1},
   104  					},
   105  				},
   106  			},
   107  		},
   108  
   109  		{
   110  			`foo ${func('baz')}`,
   111  			true,
   112  			nil,
   113  		},
   114  
   115  		{
   116  			"foo ${42}",
   117  			false,
   118  			&ast.Concat{
   119  				Posx: ast.Pos{Column: 1, Line: 1},
   120  				Exprs: []ast.Node{
   121  					&ast.LiteralNode{
   122  						Value: "foo ",
   123  						Typex: ast.TypeString,
   124  						Posx:  ast.Pos{Column: 1, Line: 1},
   125  					},
   126  					&ast.LiteralNode{
   127  						Value: 42,
   128  						Typex: ast.TypeInt,
   129  						Posx:  ast.Pos{Column: 7, Line: 1},
   130  					},
   131  				},
   132  			},
   133  		},
   134  
   135  		{
   136  			"foo ${3.14159}",
   137  			false,
   138  			&ast.Concat{
   139  				Posx: ast.Pos{Column: 1, Line: 1},
   140  				Exprs: []ast.Node{
   141  					&ast.LiteralNode{
   142  						Value: "foo ",
   143  						Typex: ast.TypeString,
   144  						Posx:  ast.Pos{Column: 1, Line: 1},
   145  					},
   146  					&ast.LiteralNode{
   147  						Value: 3.14159,
   148  						Typex: ast.TypeFloat,
   149  						Posx:  ast.Pos{Column: 7, Line: 1},
   150  					},
   151  				},
   152  			},
   153  		},
   154  
   155  		{
   156  			"${foo()}",
   157  			false,
   158  			&ast.Concat{
   159  				Posx: ast.Pos{Column: 3, Line: 1},
   160  				Exprs: []ast.Node{
   161  					&ast.Call{
   162  						Func: "foo",
   163  						Args: nil,
   164  						Posx: ast.Pos{Column: 3, Line: 1},
   165  					},
   166  				},
   167  			},
   168  		},
   169  
   170  		{
   171  			"${foo(bar)}",
   172  			false,
   173  			&ast.Concat{
   174  				Posx: ast.Pos{Column: 3, Line: 1},
   175  				Exprs: []ast.Node{
   176  					&ast.Call{
   177  						Func: "foo",
   178  						Posx: ast.Pos{Column: 3, Line: 1},
   179  						Args: []ast.Node{
   180  							&ast.VariableAccess{
   181  								Name: "bar",
   182  								Posx: ast.Pos{Column: 7, Line: 1},
   183  							},
   184  						},
   185  					},
   186  				},
   187  			},
   188  		},
   189  
   190  		{
   191  			"${foo(bar, baz)}",
   192  			false,
   193  			&ast.Concat{
   194  				Posx: ast.Pos{Column: 3, Line: 1},
   195  				Exprs: []ast.Node{
   196  					&ast.Call{
   197  						Func: "foo",
   198  						Posx: ast.Pos{Column: 3, Line: 1},
   199  						Args: []ast.Node{
   200  							&ast.VariableAccess{
   201  								Name: "bar",
   202  								Posx: ast.Pos{Column: 7, Line: 1},
   203  							},
   204  							&ast.VariableAccess{
   205  								Name: "baz",
   206  								Posx: ast.Pos{Column: 11, Line: 1},
   207  							},
   208  						},
   209  					},
   210  				},
   211  			},
   212  		},
   213  
   214  		{
   215  			"${foo(bar(baz))}",
   216  			false,
   217  			&ast.Concat{
   218  				Posx: ast.Pos{Column: 3, Line: 1},
   219  				Exprs: []ast.Node{
   220  					&ast.Call{
   221  						Func: "foo",
   222  						Posx: ast.Pos{Column: 3, Line: 1},
   223  						Args: []ast.Node{
   224  							&ast.Call{
   225  								Func: "bar",
   226  								Posx: ast.Pos{Column: 7, Line: 1},
   227  								Args: []ast.Node{
   228  									&ast.VariableAccess{
   229  										Name: "baz",
   230  										Posx: ast.Pos{Column: 11, Line: 1},
   231  									},
   232  								},
   233  							},
   234  						},
   235  					},
   236  				},
   237  			},
   238  		},
   239  
   240  		{
   241  			`foo ${"bar ${baz}"}`,
   242  			false,
   243  			&ast.Concat{
   244  				Posx: ast.Pos{Column: 1, Line: 1},
   245  				Exprs: []ast.Node{
   246  					&ast.LiteralNode{
   247  						Value: "foo ",
   248  						Typex: ast.TypeString,
   249  						Posx:  ast.Pos{Column: 1, Line: 1},
   250  					},
   251  					&ast.Concat{
   252  						Posx: ast.Pos{Column: 7, Line: 1},
   253  						Exprs: []ast.Node{
   254  							&ast.LiteralNode{
   255  								Value: "bar ",
   256  								Typex: ast.TypeString,
   257  								Posx:  ast.Pos{Column: 7, Line: 1},
   258  							},
   259  							&ast.VariableAccess{
   260  								Name: "baz",
   261  								Posx: ast.Pos{Column: 14, Line: 1},
   262  							},
   263  						},
   264  					},
   265  				},
   266  			},
   267  		},
   268  
   269  		{
   270  			`foo ${bar ${baz}}`,
   271  			true,
   272  			nil,
   273  		},
   274  
   275  		{
   276  			`foo ${${baz}}`,
   277  			true,
   278  			nil,
   279  		},
   280  
   281  		{
   282  			"${var",
   283  			true,
   284  			nil,
   285  		},
   286  	}
   287  
   288  	for _, tc := range cases {
   289  		actual, err := Parse(tc.Input)
   290  		if (err != nil) != tc.Error {
   291  			t.Fatalf("Error: %s\n\nInput: %s", err, tc.Input)
   292  		}
   293  		if !reflect.DeepEqual(actual, tc.Result) {
   294  			t.Fatalf("Bad: %#v\n\nInput: %s", actual, tc.Input)
   295  		}
   296  	}
   297  }