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

     1  package flatmap
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  )
     7  
     8  func TestFlatten(t *testing.T) {
     9  	cases := []struct {
    10  		Input  map[string]interface{}
    11  		Output map[string]string
    12  	}{
    13  		{
    14  			Input: map[string]interface{}{
    15  				"foo": "bar",
    16  				"bar": "baz",
    17  			},
    18  			Output: map[string]string{
    19  				"foo": "bar",
    20  				"bar": "baz",
    21  			},
    22  		},
    23  
    24  		{
    25  			Input: map[string]interface{}{
    26  				"foo": []string{
    27  					"one",
    28  					"two",
    29  				},
    30  			},
    31  			Output: map[string]string{
    32  				"foo.#": "2",
    33  				"foo.0": "one",
    34  				"foo.1": "two",
    35  			},
    36  		},
    37  
    38  		{
    39  			Input: map[string]interface{}{
    40  				"foo": []map[interface{}]interface{}{
    41  					map[interface{}]interface{}{
    42  						"name":    "bar",
    43  						"port":    3000,
    44  						"enabled": true,
    45  					},
    46  				},
    47  			},
    48  			Output: map[string]string{
    49  				"foo.#":         "1",
    50  				"foo.0.name":    "bar",
    51  				"foo.0.port":    "3000",
    52  				"foo.0.enabled": "true",
    53  			},
    54  		},
    55  
    56  		{
    57  			Input: map[string]interface{}{
    58  				"foo": []map[interface{}]interface{}{
    59  					map[interface{}]interface{}{
    60  						"name": "bar",
    61  						"ports": []string{
    62  							"1",
    63  							"2",
    64  						},
    65  					},
    66  				},
    67  			},
    68  			Output: map[string]string{
    69  				"foo.#":         "1",
    70  				"foo.0.name":    "bar",
    71  				"foo.0.ports.#": "2",
    72  				"foo.0.ports.0": "1",
    73  				"foo.0.ports.1": "2",
    74  			},
    75  		},
    76  	}
    77  
    78  	for _, tc := range cases {
    79  		actual := Flatten(tc.Input)
    80  		if !reflect.DeepEqual(actual, Map(tc.Output)) {
    81  			t.Fatalf(
    82  				"Input:\n\n%#v\n\nOutput:\n\n%#v\n\nExpected:\n\n%#v\n",
    83  				tc.Input,
    84  				actual,
    85  				tc.Output)
    86  		}
    87  	}
    88  }