github.com/paultyng/terraform@v0.6.11-0.20180227224804-66ff8f8bed40/flatmap/expand_test.go (about)

     1  package flatmap
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/hil"
     8  )
     9  
    10  func TestExpand(t *testing.T) {
    11  	cases := []struct {
    12  		Map    map[string]string
    13  		Key    string
    14  		Output interface{}
    15  	}{
    16  		{
    17  			Map: map[string]string{
    18  				"foo": "bar",
    19  				"bar": "baz",
    20  			},
    21  			Key:    "foo",
    22  			Output: "bar",
    23  		},
    24  
    25  		{
    26  			Map: map[string]string{
    27  				"foo.#": "2",
    28  				"foo.0": "one",
    29  				"foo.1": "two",
    30  			},
    31  			Key: "foo",
    32  			Output: []interface{}{
    33  				"one",
    34  				"two",
    35  			},
    36  		},
    37  
    38  		{
    39  			Map: map[string]string{
    40  				// # mismatches actual number of keys; actual number should
    41  				// "win" here, since the # is just a hint that this is a list.
    42  				"foo.#": "1",
    43  				"foo.0": "one",
    44  				"foo.1": "two",
    45  				"foo.2": "three",
    46  			},
    47  			Key: "foo",
    48  			Output: []interface{}{
    49  				"one",
    50  				"two",
    51  				"three",
    52  			},
    53  		},
    54  
    55  		{
    56  			Map: map[string]string{
    57  				// # mismatches actual number of keys; actual number should
    58  				// "win" here, since the # is just a hint that this is a list.
    59  				"foo.#": "5",
    60  				"foo.0": "one",
    61  				"foo.1": "two",
    62  				"foo.2": "three",
    63  			},
    64  			Key: "foo",
    65  			Output: []interface{}{
    66  				"one",
    67  				"two",
    68  				"three",
    69  			},
    70  		},
    71  
    72  		{
    73  			Map: map[string]string{
    74  				"foo.#":         "1",
    75  				"foo.0.name":    "bar",
    76  				"foo.0.port":    "3000",
    77  				"foo.0.enabled": "true",
    78  			},
    79  			Key: "foo",
    80  			Output: []interface{}{
    81  				map[string]interface{}{
    82  					"name":    "bar",
    83  					"port":    "3000",
    84  					"enabled": true,
    85  				},
    86  			},
    87  		},
    88  
    89  		{
    90  			Map: map[string]string{
    91  				"foo.#":         "1",
    92  				"foo.0.name":    "bar",
    93  				"foo.0.ports.#": "2",
    94  				"foo.0.ports.0": "1",
    95  				"foo.0.ports.1": "2",
    96  			},
    97  			Key: "foo",
    98  			Output: []interface{}{
    99  				map[string]interface{}{
   100  					"name": "bar",
   101  					"ports": []interface{}{
   102  						"1",
   103  						"2",
   104  					},
   105  				},
   106  			},
   107  		},
   108  
   109  		{
   110  			Map: map[string]string{
   111  				"list_of_map.#":   "2",
   112  				"list_of_map.0.%": "1",
   113  				"list_of_map.0.a": "1",
   114  				"list_of_map.1.%": "2",
   115  				"list_of_map.1.b": "2",
   116  				"list_of_map.1.c": "3",
   117  			},
   118  			Key: "list_of_map",
   119  			Output: []interface{}{
   120  				map[string]interface{}{
   121  					"a": "1",
   122  				},
   123  				map[string]interface{}{
   124  					"b": "2",
   125  					"c": "3",
   126  				},
   127  			},
   128  		},
   129  
   130  		{
   131  			Map: map[string]string{
   132  				"map_of_list.%":       "2",
   133  				"map_of_list.list2.#": "1",
   134  				"map_of_list.list2.0": "c",
   135  				"map_of_list.list1.#": "2",
   136  				"map_of_list.list1.0": "a",
   137  				"map_of_list.list1.1": "b",
   138  			},
   139  			Key: "map_of_list",
   140  			Output: map[string]interface{}{
   141  				"list1": []interface{}{"a", "b"},
   142  				"list2": []interface{}{"c"},
   143  			},
   144  		},
   145  
   146  		{
   147  			Map: map[string]string{
   148  				"set.#":    "3",
   149  				"set.1234": "a",
   150  				"set.1235": "b",
   151  				"set.1236": "c",
   152  			},
   153  			Key:    "set",
   154  			Output: []interface{}{"a", "b", "c"},
   155  		},
   156  
   157  		{
   158  			Map: map[string]string{
   159  				"computed_set.#":       "1",
   160  				"computed_set.~1234.a": "a",
   161  				"computed_set.~1234.b": "b",
   162  				"computed_set.~1234.c": "c",
   163  			},
   164  			Key: "computed_set",
   165  			Output: []interface{}{
   166  				map[string]interface{}{"a": "a", "b": "b", "c": "c"},
   167  			},
   168  		},
   169  
   170  		{
   171  			Map: map[string]string{
   172  				"struct.#":         "1",
   173  				"struct.0.name":    "hello",
   174  				"struct.0.rules.#": hil.UnknownValue,
   175  			},
   176  			Key: "struct",
   177  			Output: []interface{}{
   178  				map[string]interface{}{
   179  					"name":  "hello",
   180  					"rules": hil.UnknownValue,
   181  				},
   182  			},
   183  		},
   184  
   185  		{
   186  			Map: map[string]string{
   187  				"struct.#":           "1",
   188  				"struct.0.name":      "hello",
   189  				"struct.0.set.#":     "0",
   190  				"struct.0.set.0.key": "value",
   191  			},
   192  			Key: "struct",
   193  			Output: []interface{}{
   194  				map[string]interface{}{
   195  					"name": "hello",
   196  					"set":  []interface{}{},
   197  				},
   198  			},
   199  		},
   200  
   201  		{
   202  			Map: map[string]string{
   203  				"empty_map_of_sets.%":         "0",
   204  				"empty_map_of_sets.set1.#":    "0",
   205  				"empty_map_of_sets.set1.1234": "x",
   206  			},
   207  			Key:    "empty_map_of_sets",
   208  			Output: map[string]interface{}{},
   209  		},
   210  	}
   211  
   212  	for _, tc := range cases {
   213  		t.Run(tc.Key, func(t *testing.T) {
   214  			actual := Expand(tc.Map, tc.Key)
   215  			if !reflect.DeepEqual(actual, tc.Output) {
   216  				t.Errorf(
   217  					"Key: %v\nMap:\n\n%#v\n\nOutput:\n\n%#v\n\nExpected:\n\n%#v\n",
   218  					tc.Key,
   219  					tc.Map,
   220  					actual,
   221  					tc.Output)
   222  			}
   223  		})
   224  	}
   225  }