github.com/hashicorp/terraform-plugin-sdk@v1.17.2/internal/command/format/object_id_test.go (about)

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