github.com/ewbankkit/terraform@v0.7.7/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  	for _, tc := range cases {
    75  		actual := Expand(tc.Map, tc.Key)
    76  		if !reflect.DeepEqual(actual, tc.Output) {
    77  			t.Errorf(
    78  				"Key: %v\nMap:\n\n%#v\n\nOutput:\n\n%#v\n\nExpected:\n\n%#v\n",
    79  				tc.Key,
    80  				tc.Map,
    81  				actual,
    82  				tc.Output)
    83  		}
    84  	}
    85  }