github.com/opentofu/opentofu@v1.7.1/internal/addrs/module_test.go (about) 1 // Copyright (c) The OpenTofu Authors 2 // SPDX-License-Identifier: MPL-2.0 3 // Copyright (c) 2023 HashiCorp, Inc. 4 // SPDX-License-Identifier: MPL-2.0 5 6 package addrs 7 8 import ( 9 "fmt" 10 "testing" 11 ) 12 13 func TestModuleEqual_true(t *testing.T) { 14 modules := []Module{ 15 RootModule, 16 {"a"}, 17 {"a", "b"}, 18 {"a", "b", "c"}, 19 } 20 for _, m := range modules { 21 t.Run(m.String(), func(t *testing.T) { 22 if !m.Equal(m) { 23 t.Fatalf("expected %#v to be equal to itself", m) 24 } 25 }) 26 } 27 } 28 29 func TestModuleEqual_false(t *testing.T) { 30 testCases := []struct { 31 left Module 32 right Module 33 }{ 34 { 35 RootModule, 36 Module{"a"}, 37 }, 38 { 39 Module{"a"}, 40 Module{"b"}, 41 }, 42 { 43 Module{"a"}, 44 Module{"a", "a"}, 45 }, 46 { 47 Module{"a", "b"}, 48 Module{"a", "B"}, 49 }, 50 } 51 for _, tc := range testCases { 52 t.Run(fmt.Sprintf("%s = %s", tc.left, tc.right), func(t *testing.T) { 53 if tc.left.Equal(tc.right) { 54 t.Fatalf("expected %#v not to be equal to %#v", tc.left, tc.right) 55 } 56 57 if tc.right.Equal(tc.left) { 58 t.Fatalf("expected %#v not to be equal to %#v", tc.right, tc.left) 59 } 60 }) 61 } 62 } 63 64 func TestModuleString(t *testing.T) { 65 testCases := map[string]Module{ 66 "": {}, 67 "module.alpha": { 68 "alpha", 69 }, 70 "module.alpha.module.beta": { 71 "alpha", 72 "beta", 73 }, 74 "module.alpha.module.beta.module.charlie": { 75 "alpha", 76 "beta", 77 "charlie", 78 }, 79 } 80 for str, module := range testCases { 81 t.Run(str, func(t *testing.T) { 82 if got, want := module.String(), str; got != want { 83 t.Errorf("wrong result: got %q, want %q", got, want) 84 } 85 }) 86 } 87 } 88 89 func BenchmarkModuleStringShort(b *testing.B) { 90 module := Module{"a", "b"} 91 for n := 0; n < b.N; n++ { 92 module.String() 93 } 94 } 95 96 func BenchmarkModuleStringLong(b *testing.B) { 97 module := Module{"southamerica-brazil-region", "user-regional-desktop", "user-name"} 98 for n := 0; n < b.N; n++ { 99 module.String() 100 } 101 }