github.com/pulumi/terraform@v1.4.0/pkg/addrs/unique_key_test.go (about)

     1  package addrs
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  )
     7  
     8  // TestUniqueKeyer aims to ensure that all of the types that have unique keys
     9  // will continue to meet the UniqueKeyer contract under future changes.
    10  //
    11  // If you add a new implementation of UniqueKey, consider adding a test case
    12  // for it here.
    13  func TestUniqueKeyer(t *testing.T) {
    14  	tests := []UniqueKeyer{
    15  		CountAttr{Name: "index"},
    16  		ForEachAttr{Name: "key"},
    17  		TerraformAttr{Name: "workspace"},
    18  		PathAttr{Name: "module"},
    19  		InputVariable{Name: "foo"},
    20  		ModuleCall{Name: "foo"},
    21  		ModuleCallInstance{
    22  			Call: ModuleCall{Name: "foo"},
    23  			Key:  StringKey("a"),
    24  		},
    25  		ModuleCallOutput{
    26  			Call: ModuleCall{Name: "foo"},
    27  			Name: "bar",
    28  		},
    29  		ModuleCallInstanceOutput{
    30  			Call: ModuleCallInstance{
    31  				Call: ModuleCall{Name: "foo"},
    32  				Key:  StringKey("a"),
    33  			},
    34  			Name: "bar",
    35  		},
    36  		Resource{
    37  			Mode: ManagedResourceMode,
    38  			Type: "foo",
    39  			Name: "bar",
    40  		},
    41  		ResourceInstance{
    42  			Resource: Resource{
    43  				Mode: ManagedResourceMode,
    44  				Type: "foo",
    45  				Name: "bar",
    46  			},
    47  			Key: IntKey(1),
    48  		},
    49  		RootModuleInstance,
    50  		RootModuleInstance.Child("foo", NoKey),
    51  		RootModuleInstance.ResourceInstance(
    52  			DataResourceMode,
    53  			"boop",
    54  			"beep",
    55  			NoKey,
    56  		),
    57  		Self,
    58  	}
    59  
    60  	for _, test := range tests {
    61  		t.Run(fmt.Sprintf("%s", test), func(t *testing.T) {
    62  			a := test.UniqueKey()
    63  			b := test.UniqueKey()
    64  
    65  			// The following comparison will panic if the unique key is not
    66  			// of a comparable type.
    67  			if a != b {
    68  				t.Fatalf("the two unique keys are not equal\na: %#v\b: %#v", a, b)
    69  			}
    70  		})
    71  	}
    72  }