github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/flatmap/expand_test.go (about)

     1  package flatmap
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  )
     7  
     8  func TestExpand(t *testing.T) {
     9  	cases := []struct {
    10  		Map    map[string]string
    11  		Key    string
    12  		Output interface{}
    13  	}{
    14  		{
    15  			Map: map[string]string{
    16  				"foo": "bar",
    17  				"bar": "baz",
    18  			},
    19  			Key:    "foo",
    20  			Output: "bar",
    21  		},
    22  
    23  		{
    24  			Map: map[string]string{
    25  				"foo.#": "2",
    26  				"foo.0": "one",
    27  				"foo.1": "two",
    28  			},
    29  			Key: "foo",
    30  			Output: []interface{}{
    31  				"one",
    32  				"two",
    33  			},
    34  		},
    35  
    36  		{
    37  			Map: map[string]string{
    38  				"foo.#":         "1",
    39  				"foo.0.name":    "bar",
    40  				"foo.0.port":    "3000",
    41  				"foo.0.enabled": "true",
    42  			},
    43  			Key: "foo",
    44  			Output: []interface{}{
    45  				map[string]interface{}{
    46  					"name":    "bar",
    47  					"port":    "3000",
    48  					"enabled": true,
    49  				},
    50  			},
    51  		},
    52  
    53  		{
    54  			Map: map[string]string{
    55  				"foo.#":         "1",
    56  				"foo.0.name":    "bar",
    57  				"foo.0.ports.#": "2",
    58  				"foo.0.ports.0": "1",
    59  				"foo.0.ports.1": "2",
    60  			},
    61  			Key: "foo",
    62  			Output: []interface{}{
    63  				map[string]interface{}{
    64  					"name": "bar",
    65  					"ports": []interface{}{
    66  						"1",
    67  						"2",
    68  					},
    69  				},
    70  			},
    71  		},
    72  
    73  		{
    74  			Map: map[string]string{
    75  				"list_of_map.#":   "2",
    76  				"list_of_map.0.%": "1",
    77  				"list_of_map.0.a": "1",
    78  				"list_of_map.1.%": "2",
    79  				"list_of_map.1.b": "2",
    80  				"list_of_map.1.c": "3",
    81  			},
    82  			Key: "list_of_map",
    83  			Output: []interface{}{
    84  				map[string]interface{}{
    85  					"a": "1",
    86  				},
    87  				map[string]interface{}{
    88  					"b": "2",
    89  					"c": "3",
    90  				},
    91  			},
    92  		},
    93  
    94  		{
    95  			Map: map[string]string{
    96  				"map_of_list.%":       "2",
    97  				"map_of_list.list2.#": "1",
    98  				"map_of_list.list2.0": "c",
    99  				"map_of_list.list1.#": "2",
   100  				"map_of_list.list1.0": "a",
   101  				"map_of_list.list1.1": "b",
   102  			},
   103  			Key: "map_of_list",
   104  			Output: map[string]interface{}{
   105  				"list1": []interface{}{"a", "b"},
   106  				"list2": []interface{}{"c"},
   107  			},
   108  		},
   109  
   110  		{
   111  			Map: map[string]string{
   112  				"set.#":    "3",
   113  				"set.1234": "a",
   114  				"set.1235": "b",
   115  				"set.1236": "c",
   116  			},
   117  			Key:    "set",
   118  			Output: []interface{}{"a", "b", "c"},
   119  		},
   120  	}
   121  
   122  	for _, tc := range cases {
   123  		actual := Expand(tc.Map, tc.Key)
   124  		if !reflect.DeepEqual(actual, tc.Output) {
   125  			t.Errorf(
   126  				"Key: %v\nMap:\n\n%#v\n\nOutput:\n\n%#v\n\nExpected:\n\n%#v\n",
   127  				tc.Key,
   128  				tc.Map,
   129  				actual,
   130  				tc.Output)
   131  		}
   132  	}
   133  }