github.com/dougneal/terraform@v0.6.15-0.20170330092735-b6a3840768a4/helper/schema/field_reader_map_test.go (about)

     1  package schema
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  )
     7  
     8  func TestMapFieldReader_impl(t *testing.T) {
     9  	var _ FieldReader = new(MapFieldReader)
    10  }
    11  
    12  func TestMapFieldReader(t *testing.T) {
    13  	testFieldReader(t, func(s map[string]*Schema) FieldReader {
    14  		return &MapFieldReader{
    15  			Schema: s,
    16  
    17  			Map: BasicMapReader(map[string]string{
    18  				"bool":   "true",
    19  				"int":    "42",
    20  				"float":  "3.1415",
    21  				"string": "string",
    22  
    23  				"list.#": "2",
    24  				"list.0": "foo",
    25  				"list.1": "bar",
    26  
    27  				"listInt.#": "2",
    28  				"listInt.0": "21",
    29  				"listInt.1": "42",
    30  
    31  				"map.%":   "2",
    32  				"map.foo": "bar",
    33  				"map.bar": "baz",
    34  
    35  				"set.#":  "2",
    36  				"set.10": "10",
    37  				"set.50": "50",
    38  
    39  				"setDeep.#":        "2",
    40  				"setDeep.10.index": "10",
    41  				"setDeep.10.value": "foo",
    42  				"setDeep.50.index": "50",
    43  				"setDeep.50.value": "bar",
    44  
    45  				"mapInt.%":   "2",
    46  				"mapInt.one": "1",
    47  				"mapInt.two": "2",
    48  
    49  				"mapFloat.%":         "1",
    50  				"mapFloat.oneDotTwo": "1.2",
    51  
    52  				"mapBool.%":     "2",
    53  				"mapBool.True":  "true",
    54  				"mapBool.False": "false",
    55  			}),
    56  		}
    57  	})
    58  }
    59  
    60  func TestMapFieldReader_extra(t *testing.T) {
    61  	r := &MapFieldReader{
    62  		Schema: map[string]*Schema{
    63  			"mapDel":   &Schema{Type: TypeMap},
    64  			"mapEmpty": &Schema{Type: TypeMap},
    65  		},
    66  
    67  		Map: BasicMapReader(map[string]string{
    68  			"mapDel": "",
    69  
    70  			"mapEmpty.%": "0",
    71  		}),
    72  	}
    73  
    74  	cases := map[string]struct {
    75  		Addr        []string
    76  		Out         interface{}
    77  		OutOk       bool
    78  		OutComputed bool
    79  		OutErr      bool
    80  	}{
    81  		"mapDel": {
    82  			[]string{"mapDel"},
    83  			map[string]interface{}{},
    84  			true,
    85  			false,
    86  			false,
    87  		},
    88  
    89  		"mapEmpty": {
    90  			[]string{"mapEmpty"},
    91  			map[string]interface{}{},
    92  			true,
    93  			false,
    94  			false,
    95  		},
    96  	}
    97  
    98  	for name, tc := range cases {
    99  		out, err := r.ReadField(tc.Addr)
   100  		if err != nil != tc.OutErr {
   101  			t.Fatalf("%s: err: %s", name, err)
   102  		}
   103  		if out.Computed != tc.OutComputed {
   104  			t.Fatalf("%s: err: %#v", name, out.Computed)
   105  		}
   106  
   107  		if s, ok := out.Value.(*Set); ok {
   108  			// If it is a set, convert to a list so its more easily checked.
   109  			out.Value = s.List()
   110  		}
   111  
   112  		if !reflect.DeepEqual(out.Value, tc.Out) {
   113  			t.Fatalf("%s: out: %#v", name, out.Value)
   114  		}
   115  		if out.Exists != tc.OutOk {
   116  			t.Fatalf("%s: outOk: %#v", name, out.Exists)
   117  		}
   118  	}
   119  }