github.com/terramate-io/tf@v0.0.0-20230830114523-fce866b4dfcd/addrs/module_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 TestModuleEqual_true(t *testing.T) { 12 modules := []Module{ 13 RootModule, 14 {"a"}, 15 {"a", "b"}, 16 {"a", "b", "c"}, 17 } 18 for _, m := range modules { 19 t.Run(m.String(), func(t *testing.T) { 20 if !m.Equal(m) { 21 t.Fatalf("expected %#v to be equal to itself", m) 22 } 23 }) 24 } 25 } 26 27 func TestModuleEqual_false(t *testing.T) { 28 testCases := []struct { 29 left Module 30 right Module 31 }{ 32 { 33 RootModule, 34 Module{"a"}, 35 }, 36 { 37 Module{"a"}, 38 Module{"b"}, 39 }, 40 { 41 Module{"a"}, 42 Module{"a", "a"}, 43 }, 44 { 45 Module{"a", "b"}, 46 Module{"a", "B"}, 47 }, 48 } 49 for _, tc := range testCases { 50 t.Run(fmt.Sprintf("%s = %s", tc.left, tc.right), func(t *testing.T) { 51 if tc.left.Equal(tc.right) { 52 t.Fatalf("expected %#v not to be equal to %#v", tc.left, tc.right) 53 } 54 55 if tc.right.Equal(tc.left) { 56 t.Fatalf("expected %#v not to be equal to %#v", tc.right, tc.left) 57 } 58 }) 59 } 60 } 61 62 func TestModuleString(t *testing.T) { 63 testCases := map[string]Module{ 64 "": {}, 65 "module.alpha": { 66 "alpha", 67 }, 68 "module.alpha.module.beta": { 69 "alpha", 70 "beta", 71 }, 72 "module.alpha.module.beta.module.charlie": { 73 "alpha", 74 "beta", 75 "charlie", 76 }, 77 } 78 for str, module := range testCases { 79 t.Run(str, func(t *testing.T) { 80 if got, want := module.String(), str; got != want { 81 t.Errorf("wrong result: got %q, want %q", got, want) 82 } 83 }) 84 } 85 } 86 87 func BenchmarkModuleStringShort(b *testing.B) { 88 module := Module{"a", "b"} 89 for n := 0; n < b.N; n++ { 90 module.String() 91 } 92 } 93 94 func BenchmarkModuleStringLong(b *testing.B) { 95 module := Module{"southamerica-brazil-region", "user-regional-desktop", "user-name"} 96 for n := 0; n < b.N; n++ { 97 module.String() 98 } 99 }