github.com/hs0210/hashicorp-terraform@v0.11.12-beta1/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  				"mapIntNestedSchema.%":   "2",
    50  				"mapIntNestedSchema.one": "1",
    51  				"mapIntNestedSchema.two": "2",
    52  
    53  				"mapFloat.%":         "1",
    54  				"mapFloat.oneDotTwo": "1.2",
    55  
    56  				"mapBool.%":     "2",
    57  				"mapBool.True":  "true",
    58  				"mapBool.False": "false",
    59  			}),
    60  		}
    61  	})
    62  }
    63  
    64  func TestMapFieldReader_extra(t *testing.T) {
    65  	r := &MapFieldReader{
    66  		Schema: map[string]*Schema{
    67  			"mapDel":   &Schema{Type: TypeMap},
    68  			"mapEmpty": &Schema{Type: TypeMap},
    69  		},
    70  
    71  		Map: BasicMapReader(map[string]string{
    72  			"mapDel": "",
    73  
    74  			"mapEmpty.%": "0",
    75  		}),
    76  	}
    77  
    78  	cases := map[string]struct {
    79  		Addr        []string
    80  		Out         interface{}
    81  		OutOk       bool
    82  		OutComputed bool
    83  		OutErr      bool
    84  	}{
    85  		"mapDel": {
    86  			[]string{"mapDel"},
    87  			map[string]interface{}{},
    88  			true,
    89  			false,
    90  			false,
    91  		},
    92  
    93  		"mapEmpty": {
    94  			[]string{"mapEmpty"},
    95  			map[string]interface{}{},
    96  			true,
    97  			false,
    98  			false,
    99  		},
   100  	}
   101  
   102  	for name, tc := range cases {
   103  		out, err := r.ReadField(tc.Addr)
   104  		if err != nil != tc.OutErr {
   105  			t.Fatalf("%s: err: %s", name, err)
   106  		}
   107  		if out.Computed != tc.OutComputed {
   108  			t.Fatalf("%s: err: %#v", name, out.Computed)
   109  		}
   110  
   111  		if s, ok := out.Value.(*Set); ok {
   112  			// If it is a set, convert to a list so its more easily checked.
   113  			out.Value = s.List()
   114  		}
   115  
   116  		if !reflect.DeepEqual(out.Value, tc.Out) {
   117  			t.Fatalf("%s: out: %#v", name, out.Value)
   118  		}
   119  		if out.Exists != tc.OutOk {
   120  			t.Fatalf("%s: outOk: %#v", name, out.Exists)
   121  		}
   122  	}
   123  }