github.com/terramate-io/tf@v0.0.0-20230830114523-fce866b4dfcd/legacy/helper/schema/field_reader_map_test.go (about)

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