github.com/chanzuckerberg/terraform@v0.11.12-beta1/config/hcl2shim/values_test.go (about)

     1  package hcl2shim
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  	"testing"
     7  
     8  	"github.com/zclconf/go-cty/cty"
     9  )
    10  
    11  func TestConfigValueFromHCL2(t *testing.T) {
    12  	tests := []struct {
    13  		Input cty.Value
    14  		Want  interface{}
    15  	}{
    16  		{
    17  			cty.True,
    18  			true,
    19  		},
    20  		{
    21  			cty.False,
    22  			false,
    23  		},
    24  		{
    25  			cty.NumberIntVal(12),
    26  			int(12),
    27  		},
    28  		{
    29  			cty.NumberFloatVal(12.5),
    30  			float64(12.5),
    31  		},
    32  		{
    33  			cty.StringVal("hello world"),
    34  			"hello world",
    35  		},
    36  		{
    37  			cty.ObjectVal(map[string]cty.Value{
    38  				"name": cty.StringVal("Ermintrude"),
    39  				"age":  cty.NumberIntVal(19),
    40  				"address": cty.ObjectVal(map[string]cty.Value{
    41  					"street": cty.ListVal([]cty.Value{cty.StringVal("421 Shoreham Loop")}),
    42  					"city":   cty.StringVal("Fridgewater"),
    43  					"state":  cty.StringVal("MA"),
    44  					"zip":    cty.StringVal("91037"),
    45  				}),
    46  			}),
    47  			map[string]interface{}{
    48  				"name": "Ermintrude",
    49  				"age":  int(19),
    50  				"address": map[string]interface{}{
    51  					"street": []interface{}{"421 Shoreham Loop"},
    52  					"city":   "Fridgewater",
    53  					"state":  "MA",
    54  					"zip":    "91037",
    55  				},
    56  			},
    57  		},
    58  		{
    59  			cty.MapVal(map[string]cty.Value{
    60  				"foo": cty.StringVal("bar"),
    61  				"bar": cty.StringVal("baz"),
    62  			}),
    63  			map[string]interface{}{
    64  				"foo": "bar",
    65  				"bar": "baz",
    66  			},
    67  		},
    68  		{
    69  			cty.TupleVal([]cty.Value{
    70  				cty.StringVal("foo"),
    71  				cty.True,
    72  			}),
    73  			[]interface{}{
    74  				"foo",
    75  				true,
    76  			},
    77  		},
    78  		{
    79  			cty.NullVal(cty.String),
    80  			nil,
    81  		},
    82  		{
    83  			cty.UnknownVal(cty.String),
    84  			UnknownVariableValue,
    85  		},
    86  	}
    87  
    88  	for _, test := range tests {
    89  		t.Run(fmt.Sprintf("%#v", test.Input), func(t *testing.T) {
    90  			got := ConfigValueFromHCL2(test.Input)
    91  			if !reflect.DeepEqual(got, test.Want) {
    92  				t.Errorf("wrong result\ninput: %#v\ngot:   %#v\nwant:  %#v", test.Input, got, test.Want)
    93  			}
    94  		})
    95  	}
    96  }
    97  
    98  func TestHCL2ValueFromConfigValue(t *testing.T) {
    99  	tests := []struct {
   100  		Input interface{}
   101  		Want  cty.Value
   102  	}{
   103  		{
   104  			nil,
   105  			cty.NullVal(cty.DynamicPseudoType),
   106  		},
   107  		{
   108  			UnknownVariableValue,
   109  			cty.DynamicVal,
   110  		},
   111  		{
   112  			true,
   113  			cty.True,
   114  		},
   115  		{
   116  			false,
   117  			cty.False,
   118  		},
   119  		{
   120  			int(12),
   121  			cty.NumberIntVal(12),
   122  		},
   123  		{
   124  			int(0),
   125  			cty.Zero,
   126  		},
   127  		{
   128  			float64(12.5),
   129  			cty.NumberFloatVal(12.5),
   130  		},
   131  		{
   132  			"hello world",
   133  			cty.StringVal("hello world"),
   134  		},
   135  		{
   136  			"O\u0308",               // decomposed letter + diacritic
   137  			cty.StringVal("\u00D6"), // NFC-normalized on entry into cty
   138  		},
   139  		{
   140  			[]interface{}{},
   141  			cty.EmptyTupleVal,
   142  		},
   143  		{
   144  			[]interface{}(nil),
   145  			cty.EmptyTupleVal,
   146  		},
   147  		{
   148  			[]interface{}{"hello", "world"},
   149  			cty.TupleVal([]cty.Value{cty.StringVal("hello"), cty.StringVal("world")}),
   150  		},
   151  		{
   152  			map[string]interface{}{},
   153  			cty.EmptyObjectVal,
   154  		},
   155  		{
   156  			map[string]interface{}(nil),
   157  			cty.EmptyObjectVal,
   158  		},
   159  		{
   160  			map[string]interface{}{
   161  				"foo": "bar",
   162  				"bar": "baz",
   163  			},
   164  			cty.ObjectVal(map[string]cty.Value{
   165  				"foo": cty.StringVal("bar"),
   166  				"bar": cty.StringVal("baz"),
   167  			}),
   168  		},
   169  	}
   170  
   171  	for _, test := range tests {
   172  		t.Run(fmt.Sprintf("%#v", test.Input), func(t *testing.T) {
   173  			got := HCL2ValueFromConfigValue(test.Input)
   174  			if !got.RawEquals(test.Want) {
   175  				t.Errorf("wrong result\ninput: %#v\ngot:   %#v\nwant:  %#v", test.Input, got, test.Want)
   176  			}
   177  		})
   178  	}
   179  }