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