github.com/profects/terraform@v0.9.0-beta1.0.20170227135739-92d4809db30d/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  				"foo.#":         "1",
    41  				"foo.0.name":    "bar",
    42  				"foo.0.port":    "3000",
    43  				"foo.0.enabled": "true",
    44  			},
    45  			Key: "foo",
    46  			Output: []interface{}{
    47  				map[string]interface{}{
    48  					"name":    "bar",
    49  					"port":    "3000",
    50  					"enabled": true,
    51  				},
    52  			},
    53  		},
    54  
    55  		{
    56  			Map: map[string]string{
    57  				"foo.#":         "1",
    58  				"foo.0.name":    "bar",
    59  				"foo.0.ports.#": "2",
    60  				"foo.0.ports.0": "1",
    61  				"foo.0.ports.1": "2",
    62  			},
    63  			Key: "foo",
    64  			Output: []interface{}{
    65  				map[string]interface{}{
    66  					"name": "bar",
    67  					"ports": []interface{}{
    68  						"1",
    69  						"2",
    70  					},
    71  				},
    72  			},
    73  		},
    74  
    75  		{
    76  			Map: map[string]string{
    77  				"list_of_map.#":   "2",
    78  				"list_of_map.0.%": "1",
    79  				"list_of_map.0.a": "1",
    80  				"list_of_map.1.%": "2",
    81  				"list_of_map.1.b": "2",
    82  				"list_of_map.1.c": "3",
    83  			},
    84  			Key: "list_of_map",
    85  			Output: []interface{}{
    86  				map[string]interface{}{
    87  					"a": "1",
    88  				},
    89  				map[string]interface{}{
    90  					"b": "2",
    91  					"c": "3",
    92  				},
    93  			},
    94  		},
    95  
    96  		{
    97  			Map: map[string]string{
    98  				"map_of_list.%":       "2",
    99  				"map_of_list.list2.#": "1",
   100  				"map_of_list.list2.0": "c",
   101  				"map_of_list.list1.#": "2",
   102  				"map_of_list.list1.0": "a",
   103  				"map_of_list.list1.1": "b",
   104  			},
   105  			Key: "map_of_list",
   106  			Output: map[string]interface{}{
   107  				"list1": []interface{}{"a", "b"},
   108  				"list2": []interface{}{"c"},
   109  			},
   110  		},
   111  
   112  		{
   113  			Map: map[string]string{
   114  				"set.#":    "3",
   115  				"set.1234": "a",
   116  				"set.1235": "b",
   117  				"set.1236": "c",
   118  			},
   119  			Key:    "set",
   120  			Output: []interface{}{"a", "b", "c"},
   121  		},
   122  
   123  		{
   124  			Map: map[string]string{
   125  				"struct.#":         "1",
   126  				"struct.0.name":    "hello",
   127  				"struct.0.rules.#": hil.UnknownValue,
   128  			},
   129  			Key: "struct",
   130  			Output: []interface{}{
   131  				map[string]interface{}{
   132  					"name":  "hello",
   133  					"rules": hil.UnknownValue,
   134  				},
   135  			},
   136  		},
   137  	}
   138  
   139  	for _, tc := range cases {
   140  		actual := Expand(tc.Map, tc.Key)
   141  		if !reflect.DeepEqual(actual, tc.Output) {
   142  			t.Errorf(
   143  				"Key: %v\nMap:\n\n%#v\n\nOutput:\n\n%#v\n\nExpected:\n\n%#v\n",
   144  				tc.Key,
   145  				tc.Map,
   146  				actual,
   147  				tc.Output)
   148  		}
   149  	}
   150  }