sigs.k8s.io/cluster-api-provider-azure@v1.14.3/azure/pointers_test.go (about) 1 /* 2 Copyright 2023 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package azure 18 19 import ( 20 "testing" 21 22 "github.com/google/go-cmp/cmp" 23 . "github.com/onsi/gomega" 24 "k8s.io/utils/ptr" 25 ) 26 27 func TestStringSlice(t *testing.T) { 28 cases := []struct { 29 Name string 30 Arg *[]string 31 Expected []string 32 }{ 33 { 34 Name: "Should return nil if the pointer is nil", 35 Arg: nil, 36 Expected: nil, 37 }, 38 { 39 Name: "Should return string slice value for the passed string slice pointer", 40 Arg: &[]string{"foo", "bar"}, 41 Expected: []string{"foo", "bar"}, 42 }, 43 } 44 for _, tc := range cases { 45 t.Run(tc.Name, func(t *testing.T) { 46 g := NewWithT(t) 47 actual := StringSlice(tc.Arg) 48 g.Expect(tc.Expected).To(Equal(actual)) 49 }) 50 } 51 } 52 53 func TestStringMapPtr(t *testing.T) { 54 cases := []struct { 55 Name string 56 Arg map[string]string 57 Expected map[string]*string 58 }{ 59 { 60 Name: "Should return nil if the map is nil", 61 Arg: nil, 62 Expected: nil, 63 }, 64 { 65 Name: "Should convert to a map[string]*string", 66 Arg: map[string]string{"foo": "baz", "bar": "qux"}, 67 Expected: map[string]*string{"foo": ptr.To("baz"), "bar": ptr.To("qux")}, 68 }, 69 } 70 for _, tc := range cases { 71 t.Run(tc.Name, func(t *testing.T) { 72 actual := StringMapPtr(tc.Arg) 73 if !cmp.Equal(tc.Expected, actual) { 74 t.Errorf("Got difference between expected result and result %v", cmp.Diff(tc.Expected, actual)) 75 } 76 }) 77 } 78 } 79 80 func TestPtrSlice(t *testing.T) { 81 cases := []struct { 82 Name string 83 Arg *[]string 84 Expected []*string 85 }{ 86 { 87 Name: "Should return nil if the pointer is nil", 88 Arg: nil, 89 Expected: nil, 90 }, 91 { 92 Name: "Should return nil if the slice pointed to is empty", 93 Arg: &[]string{}, 94 Expected: nil, 95 }, 96 { 97 Name: "Should return slice of pointers from a pointer to a slice", 98 Arg: &[]string{"foo", "bar"}, 99 Expected: []*string{ptr.To("foo"), ptr.To("bar")}, 100 }, 101 } 102 for _, tc := range cases { 103 t.Run(tc.Name, func(t *testing.T) { 104 g := NewWithT(t) 105 actual := PtrSlice(tc.Arg) 106 g.Expect(tc.Expected).To(Equal(actual)) 107 }) 108 } 109 } 110 111 func TestAliasOrNil(t *testing.T) { 112 type TestAlias string 113 cases := []struct { 114 Name string 115 Arg *string 116 Expected *string 117 }{ 118 { 119 Name: "Should return nil if the pointer is nil", 120 }, 121 { 122 Name: "Should return nil for an empty string pointer", 123 Arg: ptr.To(""), 124 }, 125 { 126 Name: "Should return a string pointer", 127 Arg: ptr.To("foo"), 128 Expected: ptr.To("foo"), 129 }, 130 } 131 for _, tc := range cases { 132 t.Run(tc.Name, func(t *testing.T) { 133 g := NewWithT(t) 134 135 // Test with string 136 actual := AliasOrNil[string](tc.Arg) 137 g.Expect(tc.Expected).To(Equal(actual)) 138 139 // Test with type alias 140 aliasActual := AliasOrNil[TestAlias](tc.Arg) 141 if tc.Expected == nil { 142 g.Expect(aliasActual).To(BeNil()) 143 } else { 144 g.Expect(aliasActual).To(Equal(ptr.To(TestAlias(*tc.Expected)))) 145 } 146 }) 147 } 148 }