github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/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 ${42+1}",
   157  			false,
   158  			&ast.Concat{
   159  				Posx: ast.Pos{Column: 1, Line: 1},
   160  				Exprs: []ast.Node{
   161  					&ast.LiteralNode{
   162  						Value: "foo ",
   163  						Typex: ast.TypeString,
   164  						Posx:  ast.Pos{Column: 1, Line: 1},
   165  					},
   166  					&ast.Arithmetic{
   167  						Op: ast.ArithmeticOpAdd,
   168  						Exprs: []ast.Node{
   169  							&ast.LiteralNode{
   170  								Value: 42,
   171  								Typex: ast.TypeInt,
   172  								Posx:  ast.Pos{Column: 7, Line: 1},
   173  							},
   174  							&ast.LiteralNode{
   175  								Value: 1,
   176  								Typex: ast.TypeInt,
   177  								Posx:  ast.Pos{Column: 10, Line: 1},
   178  							},
   179  						},
   180  						Posx: ast.Pos{Column: 7, Line: 1},
   181  					},
   182  				},
   183  			},
   184  		},
   185  
   186  		{
   187  			"foo ${var.bar*1} baz",
   188  			false,
   189  			&ast.Concat{
   190  				Posx: ast.Pos{Column: 1, Line: 1},
   191  				Exprs: []ast.Node{
   192  					&ast.LiteralNode{
   193  						Value: "foo ",
   194  						Typex: ast.TypeString,
   195  						Posx:  ast.Pos{Column: 1, Line: 1},
   196  					},
   197  					&ast.Arithmetic{
   198  						Op: ast.ArithmeticOpMul,
   199  						Exprs: []ast.Node{
   200  							&ast.VariableAccess{
   201  								Name: "var.bar",
   202  								Posx: ast.Pos{Column: 7, Line: 1},
   203  							},
   204  							&ast.LiteralNode{
   205  								Value: 1,
   206  								Typex: ast.TypeInt,
   207  								Posx:  ast.Pos{Column: 15, Line: 1},
   208  							},
   209  						},
   210  						Posx: ast.Pos{Column: 7, Line: 1},
   211  					},
   212  					&ast.LiteralNode{
   213  						Value: " baz",
   214  						Typex: ast.TypeString,
   215  						Posx:  ast.Pos{Column: 17, Line: 1},
   216  					},
   217  				},
   218  			},
   219  		},
   220  
   221  		{
   222  			"${foo()}",
   223  			false,
   224  			&ast.Concat{
   225  				Posx: ast.Pos{Column: 3, Line: 1},
   226  				Exprs: []ast.Node{
   227  					&ast.Call{
   228  						Func: "foo",
   229  						Args: nil,
   230  						Posx: ast.Pos{Column: 3, Line: 1},
   231  					},
   232  				},
   233  			},
   234  		},
   235  
   236  		{
   237  			"${foo(bar)}",
   238  			false,
   239  			&ast.Concat{
   240  				Posx: ast.Pos{Column: 3, Line: 1},
   241  				Exprs: []ast.Node{
   242  					&ast.Call{
   243  						Func: "foo",
   244  						Posx: ast.Pos{Column: 3, Line: 1},
   245  						Args: []ast.Node{
   246  							&ast.VariableAccess{
   247  								Name: "bar",
   248  								Posx: ast.Pos{Column: 7, Line: 1},
   249  							},
   250  						},
   251  					},
   252  				},
   253  			},
   254  		},
   255  
   256  		{
   257  			"${foo(bar, baz)}",
   258  			false,
   259  			&ast.Concat{
   260  				Posx: ast.Pos{Column: 3, Line: 1},
   261  				Exprs: []ast.Node{
   262  					&ast.Call{
   263  						Func: "foo",
   264  						Posx: ast.Pos{Column: 3, Line: 1},
   265  						Args: []ast.Node{
   266  							&ast.VariableAccess{
   267  								Name: "bar",
   268  								Posx: ast.Pos{Column: 7, Line: 1},
   269  							},
   270  							&ast.VariableAccess{
   271  								Name: "baz",
   272  								Posx: ast.Pos{Column: 11, Line: 1},
   273  							},
   274  						},
   275  					},
   276  				},
   277  			},
   278  		},
   279  
   280  		{
   281  			"${foo(bar(baz))}",
   282  			false,
   283  			&ast.Concat{
   284  				Posx: ast.Pos{Column: 3, Line: 1},
   285  				Exprs: []ast.Node{
   286  					&ast.Call{
   287  						Func: "foo",
   288  						Posx: ast.Pos{Column: 3, Line: 1},
   289  						Args: []ast.Node{
   290  							&ast.Call{
   291  								Func: "bar",
   292  								Posx: ast.Pos{Column: 7, Line: 1},
   293  								Args: []ast.Node{
   294  									&ast.VariableAccess{
   295  										Name: "baz",
   296  										Posx: ast.Pos{Column: 11, Line: 1},
   297  									},
   298  								},
   299  							},
   300  						},
   301  					},
   302  				},
   303  			},
   304  		},
   305  
   306  		{
   307  			`foo ${"bar ${baz}"}`,
   308  			false,
   309  			&ast.Concat{
   310  				Posx: ast.Pos{Column: 1, Line: 1},
   311  				Exprs: []ast.Node{
   312  					&ast.LiteralNode{
   313  						Value: "foo ",
   314  						Typex: ast.TypeString,
   315  						Posx:  ast.Pos{Column: 1, Line: 1},
   316  					},
   317  					&ast.Concat{
   318  						Posx: ast.Pos{Column: 7, Line: 1},
   319  						Exprs: []ast.Node{
   320  							&ast.LiteralNode{
   321  								Value: "bar ",
   322  								Typex: ast.TypeString,
   323  								Posx:  ast.Pos{Column: 7, Line: 1},
   324  							},
   325  							&ast.VariableAccess{
   326  								Name: "baz",
   327  								Posx: ast.Pos{Column: 14, Line: 1},
   328  							},
   329  						},
   330  					},
   331  				},
   332  			},
   333  		},
   334  
   335  		{
   336  			`foo ${bar ${baz}}`,
   337  			true,
   338  			nil,
   339  		},
   340  
   341  		{
   342  			`foo ${${baz}}`,
   343  			true,
   344  			nil,
   345  		},
   346  
   347  		{
   348  			"${var",
   349  			true,
   350  			nil,
   351  		},
   352  	}
   353  
   354  	for _, tc := range cases {
   355  		actual, err := Parse(tc.Input)
   356  		if err != nil != tc.Error {
   357  			t.Fatalf("Error: %s\n\nInput: %s", err, tc.Input)
   358  		}
   359  		if !reflect.DeepEqual(actual, tc.Result) {
   360  			t.Fatalf("Bad: %#v\n\nInput: %s", actual, tc.Input)
   361  		}
   362  	}
   363  }