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