|
| 1 | +package ptr |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + infrastructurev1beta2 "github.com/oracle/cluster-api-provider-oci/api/v1beta2" |
| 7 | +) |
| 8 | + |
| 9 | +func TestToString(t *testing.T) { |
| 10 | + tests := []struct { |
| 11 | + name string |
| 12 | + input *string |
| 13 | + want string |
| 14 | + }{ |
| 15 | + { |
| 16 | + name: "nil pointer", |
| 17 | + input: nil, |
| 18 | + want: "", |
| 19 | + }, |
| 20 | + { |
| 21 | + name: "non-nil pointer", |
| 22 | + input: func() *string { s := "test"; return &s }(), |
| 23 | + want: "test", |
| 24 | + }, |
| 25 | + } |
| 26 | + |
| 27 | + for _, tt := range tests { |
| 28 | + t.Run(tt.name, func(t *testing.T) { |
| 29 | + if got := ToString(tt.input); got != tt.want { |
| 30 | + t.Errorf("ToString() = %v, want %v", got, tt.want) |
| 31 | + } |
| 32 | + }) |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +func TestToStringSlice(t *testing.T) { |
| 37 | + tests := []struct { |
| 38 | + name string |
| 39 | + input []*string |
| 40 | + want []string |
| 41 | + }{ |
| 42 | + { |
| 43 | + name: "nil slice", |
| 44 | + input: nil, |
| 45 | + want: []string{}, |
| 46 | + }, |
| 47 | + { |
| 48 | + name: "empty slice", |
| 49 | + input: []*string{}, |
| 50 | + want: []string{}, |
| 51 | + }, |
| 52 | + { |
| 53 | + name: "slice with nil elements", |
| 54 | + input: []*string{nil, nil}, |
| 55 | + want: []string{"", ""}, |
| 56 | + }, |
| 57 | + { |
| 58 | + name: "slice with non-nil elements", |
| 59 | + input: []*string{func() *string { s := "test1"; return &s }(), func() *string { s := "test2"; return &s }()}, |
| 60 | + want: []string{"test1", "test2"}, |
| 61 | + }, |
| 62 | + { |
| 63 | + name: "slice with mixed nil and non-nil elements", |
| 64 | + input: []*string{nil, func() *string { s := "test"; return &s }(), nil}, |
| 65 | + want: []string{"", "test", ""}, |
| 66 | + }, |
| 67 | + } |
| 68 | + |
| 69 | + for _, tt := range tests { |
| 70 | + t.Run(tt.name, func(t *testing.T) { |
| 71 | + if got := ToStringSlice(tt.input); len(got) != len(tt.want) || (len(got) > 0 && got[0] != tt.want[0]) { |
| 72 | + t.Errorf("ToStringSlice() = %v, want %v", got, tt.want) |
| 73 | + } |
| 74 | + }) |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +func TestToNSGSlice(t *testing.T) { |
| 79 | + tests := []struct { |
| 80 | + name string |
| 81 | + input []*infrastructurev1beta2.NSG |
| 82 | + want []infrastructurev1beta2.NSG |
| 83 | + }{ |
| 84 | + { |
| 85 | + name: "nil slice", |
| 86 | + input: nil, |
| 87 | + want: []infrastructurev1beta2.NSG{}, |
| 88 | + }, |
| 89 | + { |
| 90 | + name: "empty slice", |
| 91 | + input: []*infrastructurev1beta2.NSG{}, |
| 92 | + want: []infrastructurev1beta2.NSG{}, |
| 93 | + }, |
| 94 | + { |
| 95 | + name: "slice with nil elements", |
| 96 | + input: []*infrastructurev1beta2.NSG{nil, nil}, |
| 97 | + want: []infrastructurev1beta2.NSG{{}, {}}, |
| 98 | + }, |
| 99 | + { |
| 100 | + name: "slice with non-nil elements", |
| 101 | + input: []*infrastructurev1beta2.NSG{&infrastructurev1beta2.NSG{}, &infrastructurev1beta2.NSG{}}, |
| 102 | + want: []infrastructurev1beta2.NSG{{}, {}}, |
| 103 | + }, |
| 104 | + { |
| 105 | + name: "slice with mixed nil and non-nil elements", |
| 106 | + input: []*infrastructurev1beta2.NSG{nil, &infrastructurev1beta2.NSG{}, nil}, |
| 107 | + want: []infrastructurev1beta2.NSG{{}, {}, {}}, |
| 108 | + }, |
| 109 | + } |
| 110 | + |
| 111 | + for _, tt := range tests { |
| 112 | + t.Run(tt.name, func(t *testing.T) { |
| 113 | + if got := ToNSGSlice(tt.input); len(got) != len(tt.want) { |
| 114 | + t.Errorf("ToNSGSlice() length = %v, want %v", len(got), len(tt.want)) |
| 115 | + } |
| 116 | + }) |
| 117 | + } |
| 118 | +} |
0 commit comments