opentofu/internal/addrs/unique_key_test.go
Martin Atkins cd06572c4f addrs: ModuleInstance and AbsResourceInstance are UniqueKeyers
Since these address types are not directly comparable themselves, we use
an unexported named type around the string representation, whereby the
special type can avoid any ambiguity between string representations of
different types and thus each type only needs to worry about possible
ambiguity of its _own_ string representation.
2021-07-14 17:37:48 -07:00

73 lines
1.5 KiB
Go

package addrs
import (
"fmt"
"testing"
)
// TestUniqueKeyer aims to ensure that all of the types that have unique keys
// will continue to meet the UniqueKeyer contract under future changes.
//
// If you add a new implementation of UniqueKey, consider adding a test case
// for it here.
func TestUniqueKeyer(t *testing.T) {
tests := []UniqueKeyer{
CountAttr{Name: "index"},
ForEachAttr{Name: "key"},
TerraformAttr{Name: "workspace"},
PathAttr{Name: "module"},
InputVariable{Name: "foo"},
ModuleCall{Name: "foo"},
ModuleCallInstance{
Call: ModuleCall{Name: "foo"},
Key: StringKey("a"),
},
ModuleCallOutput{
Call: ModuleCall{Name: "foo"},
Name: "bar",
},
ModuleCallInstanceOutput{
Call: ModuleCallInstance{
Call: ModuleCall{Name: "foo"},
Key: StringKey("a"),
},
Name: "bar",
},
Resource{
Mode: ManagedResourceMode,
Type: "foo",
Name: "bar",
},
ResourceInstance{
Resource: Resource{
Mode: ManagedResourceMode,
Type: "foo",
Name: "bar",
},
Key: IntKey(1),
},
RootModuleInstance,
RootModuleInstance.Child("foo", NoKey),
RootModuleInstance.ResourceInstance(
DataResourceMode,
"boop",
"beep",
NoKey,
),
Self,
}
for _, test := range tests {
t.Run(fmt.Sprintf("%s", test), func(t *testing.T) {
a := test.UniqueKey()
b := test.UniqueKey()
// The following comparison will panic if the unique key is not
// of a comparable type.
if a != b {
t.Fatalf("the two unique keys are not equal\na: %#v\b: %#v", a, b)
}
})
}
}