github.com/pulumi/terraform@v1.4.0/pkg/command/format/object_id_test.go (about)

     1  package format
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/pulumi/terraform/pkg/lang/marks"
     8  	"github.com/zclconf/go-cty/cty"
     9  )
    10  
    11  func TestObjectValueIDOrName(t *testing.T) {
    12  	tests := []struct {
    13  		obj    cty.Value
    14  		id     [2]string
    15  		name   [2]string
    16  		either [2]string
    17  	}{
    18  		{
    19  			cty.NullVal(cty.EmptyObject),
    20  			[...]string{"", ""},
    21  			[...]string{"", ""},
    22  			[...]string{"", ""},
    23  		},
    24  		{
    25  			cty.UnknownVal(cty.EmptyObject),
    26  			[...]string{"", ""},
    27  			[...]string{"", ""},
    28  			[...]string{"", ""},
    29  		},
    30  		{
    31  			cty.EmptyObjectVal,
    32  			[...]string{"", ""},
    33  			[...]string{"", ""},
    34  			[...]string{"", ""},
    35  		},
    36  		{
    37  			cty.ObjectVal(map[string]cty.Value{
    38  				"id": cty.StringVal("foo-123"),
    39  			}),
    40  			[...]string{"id", "foo-123"},
    41  			[...]string{"", ""},
    42  			[...]string{"id", "foo-123"},
    43  		},
    44  		{
    45  			cty.ObjectVal(map[string]cty.Value{
    46  				"id":   cty.StringVal("foo-123"),
    47  				"name": cty.StringVal("awesome-foo"),
    48  			}),
    49  			[...]string{"id", "foo-123"},
    50  			[...]string{"name", "awesome-foo"},
    51  			[...]string{"id", "foo-123"},
    52  		},
    53  		{
    54  			cty.ObjectVal(map[string]cty.Value{
    55  				"name": cty.StringVal("awesome-foo"),
    56  			}),
    57  			[...]string{"name", "awesome-foo"},
    58  			[...]string{"name", "awesome-foo"},
    59  			[...]string{"name", "awesome-foo"},
    60  		},
    61  		{
    62  			cty.ObjectVal(map[string]cty.Value{
    63  				"name": cty.StringVal("awesome-foo").Mark(marks.Sensitive),
    64  			}),
    65  			[...]string{"", ""},
    66  			[...]string{"", ""},
    67  			[...]string{"", ""},
    68  		},
    69  		{
    70  			cty.ObjectVal(map[string]cty.Value{
    71  				"name": cty.StringVal("awesome-foo"),
    72  				"tags": cty.MapVal(map[string]cty.Value{
    73  					"Name": cty.StringVal("My Awesome Foo"),
    74  				}),
    75  			}),
    76  			[...]string{"name", "awesome-foo"},
    77  			[...]string{"name", "awesome-foo"},
    78  			[...]string{"name", "awesome-foo"},
    79  		},
    80  		{
    81  			cty.ObjectVal(map[string]cty.Value{
    82  				"tags": cty.MapVal(map[string]cty.Value{
    83  					"Name": cty.StringVal("My Awesome Foo"),
    84  					"name": cty.StringVal("awesome-foo"),
    85  				}),
    86  			}),
    87  			[...]string{"", ""},
    88  			[...]string{"tags.name", "awesome-foo"},
    89  			[...]string{"tags.name", "awesome-foo"},
    90  		},
    91  		{
    92  			cty.ObjectVal(map[string]cty.Value{
    93  				"tags": cty.MapVal(map[string]cty.Value{
    94  					"Name": cty.StringVal("My Awesome Foo"),
    95  				}),
    96  			}),
    97  			[...]string{"", ""},
    98  			[...]string{"tags.Name", "My Awesome Foo"},
    99  			[...]string{"tags.Name", "My Awesome Foo"},
   100  		},
   101  
   102  		// The following are degenerate cases, included to make sure we don't
   103  		// crash when we encounter them. If you're here fixing a reported panic
   104  		// in this formatter, this is the place to add a new test case.
   105  		{
   106  			cty.ObjectVal(map[string]cty.Value{
   107  				"id": cty.True,
   108  			}),
   109  			[...]string{"", ""},
   110  			[...]string{"", ""},
   111  			[...]string{"", ""},
   112  		},
   113  		{
   114  			cty.ObjectVal(map[string]cty.Value{
   115  				"id": cty.NullVal(cty.String),
   116  			}),
   117  			[...]string{"", ""},
   118  			[...]string{"", ""},
   119  			[...]string{"", ""},
   120  		},
   121  		{
   122  			cty.ObjectVal(map[string]cty.Value{
   123  				"id": cty.UnknownVal(cty.String),
   124  			}),
   125  			[...]string{"", ""},
   126  			[...]string{"", ""},
   127  			[...]string{"", ""},
   128  		},
   129  		{
   130  			cty.ObjectVal(map[string]cty.Value{
   131  				"tags": cty.StringVal("foo"),
   132  			}),
   133  			[...]string{"", ""},
   134  			[...]string{"", ""},
   135  			[...]string{"", ""},
   136  		},
   137  		{
   138  			cty.ObjectVal(map[string]cty.Value{
   139  				"tags": cty.NullVal(cty.Map(cty.String)),
   140  			}),
   141  			[...]string{"", ""},
   142  			[...]string{"", ""},
   143  			[...]string{"", ""},
   144  		},
   145  		{
   146  			cty.ObjectVal(map[string]cty.Value{
   147  				"tags": cty.UnknownVal(cty.Map(cty.String)),
   148  			}),
   149  			[...]string{"", ""},
   150  			[...]string{"", ""},
   151  			[...]string{"", ""},
   152  		},
   153  		{
   154  			cty.ObjectVal(map[string]cty.Value{
   155  				"tags": cty.MapVal(map[string]cty.Value{
   156  					"Name": cty.True,
   157  				}),
   158  			}),
   159  			[...]string{"", ""},
   160  			[...]string{"", ""},
   161  			[...]string{"", ""},
   162  		},
   163  		{
   164  			cty.ObjectVal(map[string]cty.Value{
   165  				"tags": cty.MapVal(map[string]cty.Value{
   166  					"Name": cty.UnknownVal(cty.String),
   167  				}),
   168  			}),
   169  			[...]string{"", ""},
   170  			[...]string{"", ""},
   171  			[...]string{"", ""},
   172  		},
   173  		{
   174  			cty.ObjectVal(map[string]cty.Value{
   175  				"tags": cty.MapVal(map[string]cty.Value{
   176  					"Name": cty.UnknownVal(cty.String).Mark(marks.Sensitive),
   177  				}),
   178  			}),
   179  			[...]string{"", ""},
   180  			[...]string{"", ""},
   181  			[...]string{"", ""},
   182  		},
   183  		{
   184  			cty.ObjectVal(map[string]cty.Value{
   185  				"tags": cty.MapVal(map[string]cty.Value{
   186  					"Name": cty.NullVal(cty.String),
   187  				}),
   188  			}),
   189  			[...]string{"", ""},
   190  			[...]string{"", ""},
   191  			[...]string{"", ""},
   192  		},
   193  	}
   194  
   195  	for _, test := range tests {
   196  		t.Run(fmt.Sprintf("%#v", test.obj), func(t *testing.T) {
   197  			obj := test.obj
   198  			gotIDKey, gotIDVal := ObjectValueID(obj)
   199  			gotNameKey, gotNameVal := ObjectValueName(obj)
   200  			gotEitherKey, gotEitherVal := ObjectValueIDOrName(obj)
   201  
   202  			if got, want := [...]string{gotIDKey, gotIDVal}, test.id; got != want {
   203  				t.Errorf("wrong ObjectValueID result\ngot:  %#v\nwant: %#v", got, want)
   204  			}
   205  			if got, want := [...]string{gotNameKey, gotNameVal}, test.name; got != want {
   206  				t.Errorf("wrong ObjectValueName result\ngot:  %#v\nwant: %#v", got, want)
   207  			}
   208  			if got, want := [...]string{gotEitherKey, gotEitherVal}, test.either; got != want {
   209  				t.Errorf("wrong ObjectValueIDOrName result\ngot:  %#v\nwant: %#v", got, want)
   210  			}
   211  		})
   212  	}
   213  }