github.com/ndarilek/terraform@v0.3.8-0.20150320140257-d3135c1b2bac/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  		}
    46  	})
    47  }
    48  
    49  func TestMapFieldReader_extra(t *testing.T) {
    50  	r := &MapFieldReader{
    51  		Schema: map[string]*Schema{
    52  			"mapDel": &Schema{Type: TypeMap},
    53  		},
    54  
    55  		Map: BasicMapReader(map[string]string{
    56  			"mapDel": "",
    57  		}),
    58  	}
    59  
    60  	cases := map[string]struct {
    61  		Addr        []string
    62  		Out         interface{}
    63  		OutOk       bool
    64  		OutComputed bool
    65  		OutErr      bool
    66  	}{
    67  		"mapDel": {
    68  			[]string{"mapDel"},
    69  			map[string]interface{}{},
    70  			true,
    71  			false,
    72  			false,
    73  		},
    74  	}
    75  
    76  	for name, tc := range cases {
    77  		out, err := r.ReadField(tc.Addr)
    78  		if (err != nil) != tc.OutErr {
    79  			t.Fatalf("%s: err: %s", name, err)
    80  		}
    81  		if out.Computed != tc.OutComputed {
    82  			t.Fatalf("%s: err: %#v", name, out.Computed)
    83  		}
    84  
    85  		if s, ok := out.Value.(*Set); ok {
    86  			// If it is a set, convert to a list so its more easily checked.
    87  			out.Value = s.List()
    88  		}
    89  
    90  		if !reflect.DeepEqual(out.Value, tc.Out) {
    91  			t.Fatalf("%s: out: %#v", name, out.Value)
    92  		}
    93  		if out.Exists != tc.OutOk {
    94  			t.Fatalf("%s: outOk: %#v", name, out.Exists)
    95  		}
    96  	}
    97  }