github.com/terramate-io/tf@v0.0.0-20230830114523-fce866b4dfcd/addrs/unique_key_test.go (about)

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