github.com/terramate-io/tf@v0.0.0-20230830114523-fce866b4dfcd/addrs/module_instance_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  func TestModuleInstanceEqual_true(t *testing.T) {
    12  	addrs := []string{
    13  		"module.foo",
    14  		"module.foo.module.bar",
    15  		"module.foo[1].module.bar",
    16  		`module.foo["a"].module.bar["b"]`,
    17  		`module.foo["a"].module.bar.module.baz[3]`,
    18  	}
    19  	for _, m := range addrs {
    20  		t.Run(m, func(t *testing.T) {
    21  			addr, diags := ParseModuleInstanceStr(m)
    22  			if len(diags) > 0 {
    23  				t.Fatalf("unexpected diags: %s", diags.Err())
    24  			}
    25  			if !addr.Equal(addr) {
    26  				t.Fatalf("expected %#v to be equal to itself", addr)
    27  			}
    28  		})
    29  	}
    30  }
    31  
    32  func TestModuleInstanceEqual_false(t *testing.T) {
    33  	testCases := []struct {
    34  		left  string
    35  		right string
    36  	}{
    37  		{
    38  			"module.foo",
    39  			"module.bar",
    40  		},
    41  		{
    42  			"module.foo",
    43  			"module.foo.module.bar",
    44  		},
    45  		{
    46  			"module.foo[1]",
    47  			"module.bar[1]",
    48  		},
    49  		{
    50  			`module.foo[1]`,
    51  			`module.foo["1"]`,
    52  		},
    53  		{
    54  			"module.foo.module.bar",
    55  			"module.foo[1].module.bar",
    56  		},
    57  		{
    58  			`module.foo.module.bar`,
    59  			`module.foo["a"].module.bar`,
    60  		},
    61  	}
    62  	for _, tc := range testCases {
    63  		t.Run(fmt.Sprintf("%s = %s", tc.left, tc.right), func(t *testing.T) {
    64  			left, diags := ParseModuleInstanceStr(tc.left)
    65  			if len(diags) > 0 {
    66  				t.Fatalf("unexpected diags parsing %s: %s", tc.left, diags.Err())
    67  			}
    68  			right, diags := ParseModuleInstanceStr(tc.right)
    69  			if len(diags) > 0 {
    70  				t.Fatalf("unexpected diags parsing %s: %s", tc.right, diags.Err())
    71  			}
    72  
    73  			if left.Equal(right) {
    74  				t.Fatalf("expected %#v not to be equal to %#v", left, right)
    75  			}
    76  
    77  			if right.Equal(left) {
    78  				t.Fatalf("expected %#v not to be equal to %#v", right, left)
    79  			}
    80  		})
    81  	}
    82  }
    83  
    84  func BenchmarkStringShort(b *testing.B) {
    85  	addr, _ := ParseModuleInstanceStr(`module.foo`)
    86  	for n := 0; n < b.N; n++ {
    87  		addr.String()
    88  	}
    89  }
    90  
    91  func BenchmarkStringLong(b *testing.B) {
    92  	addr, _ := ParseModuleInstanceStr(`module.southamerica-brazil-region.module.user-regional-desktops.module.user-name`)
    93  	for n := 0; n < b.N; n++ {
    94  		addr.String()
    95  	}
    96  }
    97  
    98  func TestModuleInstance_IsDeclaredByCall(t *testing.T) {
    99  	tests := []struct {
   100  		instance ModuleInstance
   101  		call     AbsModuleCall
   102  		want     bool
   103  	}{
   104  		{
   105  			ModuleInstance{},
   106  			AbsModuleCall{},
   107  			false,
   108  		},
   109  		{
   110  			mustParseModuleInstanceStr("module.child"),
   111  			AbsModuleCall{},
   112  			false,
   113  		},
   114  		{
   115  			ModuleInstance{},
   116  			AbsModuleCall{
   117  				RootModuleInstance,
   118  				ModuleCall{Name: "child"},
   119  			},
   120  			false,
   121  		},
   122  		{
   123  			mustParseModuleInstanceStr("module.child"),
   124  			AbsModuleCall{ // module.child
   125  				RootModuleInstance,
   126  				ModuleCall{Name: "child"},
   127  			},
   128  			true,
   129  		},
   130  		{
   131  			mustParseModuleInstanceStr(`module.child`),
   132  			AbsModuleCall{ // module.kinder.module.child
   133  				mustParseModuleInstanceStr("module.kinder"),
   134  				ModuleCall{Name: "child"},
   135  			},
   136  			false,
   137  		},
   138  		{
   139  			mustParseModuleInstanceStr("module.kinder"),
   140  			// module.kinder.module.child contains module.kinder, but is not itself an instance of module.kinder
   141  			AbsModuleCall{
   142  				mustParseModuleInstanceStr("module.kinder"),
   143  				ModuleCall{Name: "child"},
   144  			},
   145  			false,
   146  		},
   147  		{
   148  			mustParseModuleInstanceStr("module.child"),
   149  			AbsModuleCall{
   150  				mustParseModuleInstanceStr(`module.kinder["a"]`),
   151  				ModuleCall{Name: "kinder"},
   152  			},
   153  			false,
   154  		},
   155  	}
   156  
   157  	for _, test := range tests {
   158  		t.Run(fmt.Sprintf("%q.IsCallInstance(%q)", test.instance, test.call.String()), func(t *testing.T) {
   159  			got := test.instance.IsDeclaredByCall(test.call)
   160  			if got != test.want {
   161  				t.Fatal("wrong result")
   162  			}
   163  		})
   164  	}
   165  }
   166  
   167  func mustParseModuleInstanceStr(str string) ModuleInstance {
   168  	mi, diags := ParseModuleInstanceStr(str)
   169  	if diags.HasErrors() {
   170  		panic(diags.ErrWithWarnings())
   171  	}
   172  	return mi
   173  }