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