github.com/franklinhu/terraform@v0.6.9-0.20151202232446-81f7fb1e6f9e/config/lang/lex_test.go (about)

     1  package lang
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  )
     7  
     8  func TestLex(t *testing.T) {
     9  	cases := []struct {
    10  		Input  string
    11  		Output []int
    12  	}{
    13  		{
    14  			"foo",
    15  			[]int{STRING, lexEOF},
    16  		},
    17  
    18  		{
    19  			"foo$bar",
    20  			[]int{STRING, lexEOF},
    21  		},
    22  
    23  		{
    24  			"foo ${bar}",
    25  			[]int{STRING, PROGRAM_BRACKET_LEFT, IDENTIFIER, PROGRAM_BRACKET_RIGHT, lexEOF},
    26  		},
    27  
    28  		{
    29  			"foo $${bar}",
    30  			[]int{STRING, lexEOF},
    31  		},
    32  
    33  		{
    34  			"foo $$$${bar}",
    35  			[]int{STRING, lexEOF},
    36  		},
    37  
    38  		{
    39  			"foo ${\"bar\"}",
    40  			[]int{STRING, PROGRAM_BRACKET_LEFT, STRING, PROGRAM_BRACKET_RIGHT, lexEOF},
    41  		},
    42  
    43  		{
    44  			"${bar(baz)}",
    45  			[]int{PROGRAM_BRACKET_LEFT,
    46  				IDENTIFIER, PAREN_LEFT, IDENTIFIER, PAREN_RIGHT,
    47  				PROGRAM_BRACKET_RIGHT, lexEOF},
    48  		},
    49  
    50  		{
    51  			"${bar(baz, foo)}",
    52  			[]int{PROGRAM_BRACKET_LEFT,
    53  				IDENTIFIER, PAREN_LEFT,
    54  				IDENTIFIER, COMMA, IDENTIFIER,
    55  				PAREN_RIGHT,
    56  				PROGRAM_BRACKET_RIGHT, lexEOF},
    57  		},
    58  
    59  		{
    60  			"${bar(42)}",
    61  			[]int{PROGRAM_BRACKET_LEFT,
    62  				IDENTIFIER, PAREN_LEFT, INTEGER, PAREN_RIGHT,
    63  				PROGRAM_BRACKET_RIGHT, lexEOF},
    64  		},
    65  
    66  		{
    67  			"${bar(42+1)}",
    68  			[]int{PROGRAM_BRACKET_LEFT,
    69  				IDENTIFIER, PAREN_LEFT,
    70  				INTEGER, ARITH_OP, INTEGER,
    71  				PAREN_RIGHT,
    72  				PROGRAM_BRACKET_RIGHT, lexEOF},
    73  		},
    74  
    75  		{
    76  			"${bar(3.14159)}",
    77  			[]int{PROGRAM_BRACKET_LEFT,
    78  				IDENTIFIER, PAREN_LEFT, FLOAT, PAREN_RIGHT,
    79  				PROGRAM_BRACKET_RIGHT, lexEOF},
    80  		},
    81  
    82  		{
    83  			"${bar(inner(baz))}",
    84  			[]int{PROGRAM_BRACKET_LEFT,
    85  				IDENTIFIER, PAREN_LEFT,
    86  				IDENTIFIER, PAREN_LEFT,
    87  				IDENTIFIER,
    88  				PAREN_RIGHT, PAREN_RIGHT,
    89  				PROGRAM_BRACKET_RIGHT, lexEOF},
    90  		},
    91  
    92  		{
    93  			"foo ${foo.bar.baz}",
    94  			[]int{STRING, PROGRAM_BRACKET_LEFT, IDENTIFIER, PROGRAM_BRACKET_RIGHT, lexEOF},
    95  		},
    96  
    97  		{
    98  			"foo ${foo.bar.*.baz}",
    99  			[]int{STRING, PROGRAM_BRACKET_LEFT, IDENTIFIER, PROGRAM_BRACKET_RIGHT, lexEOF},
   100  		},
   101  
   102  		{
   103  			"foo ${foo(\"baz\")}",
   104  			[]int{STRING, PROGRAM_BRACKET_LEFT,
   105  				IDENTIFIER, PAREN_LEFT, STRING, PAREN_RIGHT,
   106  				PROGRAM_BRACKET_RIGHT, lexEOF},
   107  		},
   108  
   109  		{
   110  			`foo ${"${var.foo}"}`,
   111  			[]int{STRING, PROGRAM_BRACKET_LEFT,
   112  				PROGRAM_BRACKET_LEFT, IDENTIFIER, PROGRAM_BRACKET_RIGHT,
   113  				PROGRAM_BRACKET_RIGHT, lexEOF},
   114  		},
   115  	}
   116  
   117  	for _, tc := range cases {
   118  		l := &parserLex{Input: tc.Input}
   119  		var actual []int
   120  		for {
   121  			token := l.Lex(new(parserSymType))
   122  			actual = append(actual, token)
   123  
   124  			if token == lexEOF {
   125  				break
   126  			}
   127  
   128  			// Be careful against what are probably infinite loops
   129  			if len(actual) > 100 {
   130  				t.Fatalf("Input:%s\n\nExausted.", tc.Input)
   131  			}
   132  		}
   133  
   134  		if !reflect.DeepEqual(actual, tc.Output) {
   135  			t.Fatalf(
   136  				"Input: %s\n\nBad: %#v\n\nExpected: %#v",
   137  				tc.Input, actual, tc.Output)
   138  		}
   139  	}
   140  }