File tree Expand file tree Collapse file tree 2 files changed +60
-1
lines changed Expand file tree Collapse file tree 2 files changed +60
-1
lines changed Original file line number Diff line number Diff line change @@ -251,7 +251,8 @@ func ConvertToBase64PatchedServers(servers []iaas.Server) []Base64PatchedServer
251251 return result
252252}
253253
254- // GetSliceFromPointer returns the value of a pointer to a slice of type T. If the pointer is nil, it returns an empty slice.
254+ // GetSliceFromPointer returns the value of a pointer to a slice of type T.
255+ // If the pointer is nil, it returns an empty slice.
255256func GetSliceFromPointer [T any ](s * []T ) []T {
256257 if s == nil {
257258 return []T {}
Original file line number Diff line number Diff line change @@ -544,3 +544,61 @@ func TestBase64Bytes_MarshalYAML(t *testing.T) {
544544 })
545545 }
546546}
547+ func TestGetSliceFromPointer (t * testing.T ) {
548+ tests := []struct {
549+ name string
550+ input * []string
551+ expected []string
552+ expectNil bool
553+ }{
554+ {
555+ name : "nil pointer" ,
556+ input : nil ,
557+ expected : []string {},
558+ expectNil : false ,
559+ },
560+ {
561+ name : "pointer to nil slice" ,
562+ input : func () * []string {
563+ var s []string
564+ return & s
565+ }(),
566+ expected : nil ,
567+ expectNil : true ,
568+ },
569+ {
570+ name : "empty slice" ,
571+ input : & []string {},
572+ expected : []string {},
573+ expectNil : false ,
574+ },
575+ {
576+ name : "populated slice" ,
577+ input : & []string {"item1" , "item2" },
578+ expected : []string {"item1" , "item2" },
579+ expectNil : false ,
580+ },
581+ }
582+
583+ for _ , tt := range tests {
584+ t .Run (tt .name , func (t * testing.T ) {
585+ result := GetSliceFromPointer (tt .input )
586+
587+ if tt .expectNil {
588+ if result != nil {
589+ t .Errorf ("GetSliceFromPointer() = %v, want nil" , result )
590+ }
591+ return
592+ }
593+
594+ if result == nil {
595+ t .Errorf ("GetSliceFromPointer() = nil, want %v" , tt .expected )
596+ return
597+ }
598+
599+ if ! reflect .DeepEqual (result , tt .expected ) {
600+ t .Errorf ("GetSliceFromPointer() = %v, want %v" , result , tt .expected )
601+ }
602+ })
603+ }
604+ }
You can’t perform that action at this time.
0 commit comments