|
| 1 | +package convert |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/assert" |
| 7 | +) |
| 8 | + |
| 9 | +func TestPointerToOrNil(t *testing.T) { |
| 10 | + t.Run("int with true condition", func(t *testing.T) { |
| 11 | + result := PointerToOrNil(42, true) |
| 12 | + assert.NotNil(t, result) |
| 13 | + assert.Equal(t, 42, *result) |
| 14 | + }) |
| 15 | + |
| 16 | + t.Run("int with false condition", func(t *testing.T) { |
| 17 | + result := PointerToOrNil(42, false) |
| 18 | + assert.Nil(t, result) |
| 19 | + }) |
| 20 | + |
| 21 | + t.Run("zero value with true condition", func(t *testing.T) { |
| 22 | + result := PointerToOrNil(0, true) |
| 23 | + assert.NotNil(t, result) |
| 24 | + assert.Equal(t, 0, *result) |
| 25 | + }) |
| 26 | + |
| 27 | + t.Run("string with true condition", func(t *testing.T) { |
| 28 | + result := PointerToOrNil("hello", true) |
| 29 | + assert.NotNil(t, result) |
| 30 | + assert.Equal(t, "hello", *result) |
| 31 | + }) |
| 32 | + |
| 33 | + t.Run("empty string with false condition", func(t *testing.T) { |
| 34 | + result := PointerToOrNil("", false) |
| 35 | + assert.Nil(t, result) |
| 36 | + }) |
| 37 | + |
| 38 | + t.Run("float64 with condition", func(t *testing.T) { |
| 39 | + result := PointerToOrNil(3.14, true) |
| 40 | + assert.NotNil(t, result) |
| 41 | + assert.Equal(t, 3.14, *result) |
| 42 | + |
| 43 | + resultNil := PointerToOrNil(3.14, false) |
| 44 | + assert.Nil(t, resultNil) |
| 45 | + }) |
| 46 | + |
| 47 | + t.Run("struct with true condition", func(t *testing.T) { |
| 48 | + type testStruct struct { |
| 49 | + x int |
| 50 | + y string |
| 51 | + } |
| 52 | + val := testStruct{x: 42, y: "test"} |
| 53 | + result := PointerToOrNil(val, true) |
| 54 | + assert.NotNil(t, result) |
| 55 | + assert.Equal(t, val, *result) |
| 56 | + }) |
| 57 | +} |
| 58 | + |
| 59 | +func TestPointerToIf(t *testing.T) { |
| 60 | + t.Run("predicate returns true", func(t *testing.T) { |
| 61 | + result := PointerToIf(42, func(v int) bool { return v > 0 }) |
| 62 | + assert.NotNil(t, result) |
| 63 | + assert.Equal(t, 42, *result) |
| 64 | + }) |
| 65 | + |
| 66 | + t.Run("predicate returns false", func(t *testing.T) { |
| 67 | + result := PointerToIf(-5, func(v int) bool { return v > 0 }) |
| 68 | + assert.Nil(t, result) |
| 69 | + }) |
| 70 | + |
| 71 | + t.Run("complex predicate - latitude range check valid", func(t *testing.T) { |
| 72 | + isValidLatitude := func(v float64) bool { |
| 73 | + return v >= -90 && v <= 90 |
| 74 | + } |
| 75 | + result := PointerToIf(45.5, isValidLatitude) |
| 76 | + assert.NotNil(t, result) |
| 77 | + assert.Equal(t, 45.5, *result) |
| 78 | + }) |
| 79 | + |
| 80 | + t.Run("complex predicate - latitude out of range", func(t *testing.T) { |
| 81 | + isValidLatitude := func(v float64) bool { |
| 82 | + return v >= -90 && v <= 90 |
| 83 | + } |
| 84 | + result := PointerToIf(100.0, isValidLatitude) |
| 85 | + assert.Nil(t, result) |
| 86 | + }) |
| 87 | + |
| 88 | + t.Run("string length predicate", func(t *testing.T) { |
| 89 | + isNotEmpty := func(s string) bool { return len(s) > 0 } |
| 90 | + result := PointerToIf("hello", isNotEmpty) |
| 91 | + assert.NotNil(t, result) |
| 92 | + assert.Equal(t, "hello", *result) |
| 93 | + |
| 94 | + resultEmpty := PointerToIf("", isNotEmpty) |
| 95 | + assert.Nil(t, resultEmpty) |
| 96 | + }) |
| 97 | + |
| 98 | + t.Run("zero value with predicate that accepts it", func(t *testing.T) { |
| 99 | + alwaysTrue := func(v int) bool { return true } |
| 100 | + result := PointerToIf(0, alwaysTrue) |
| 101 | + assert.NotNil(t, result) |
| 102 | + assert.Equal(t, 0, *result) |
| 103 | + }) |
| 104 | +} |
| 105 | + |
| 106 | +func TestPointerToIfNonZero(t *testing.T) { |
| 107 | + t.Run("non-zero int", func(t *testing.T) { |
| 108 | + result := PointerToIfNonZero(42) |
| 109 | + assert.NotNil(t, result) |
| 110 | + assert.Equal(t, 42, *result) |
| 111 | + }) |
| 112 | + |
| 113 | + t.Run("zero int", func(t *testing.T) { |
| 114 | + result := PointerToIfNonZero(0) |
| 115 | + assert.Nil(t, result) |
| 116 | + }) |
| 117 | + |
| 118 | + t.Run("non-empty string", func(t *testing.T) { |
| 119 | + result := PointerToIfNonZero("hello") |
| 120 | + assert.NotNil(t, result) |
| 121 | + assert.Equal(t, "hello", *result) |
| 122 | + }) |
| 123 | + |
| 124 | + t.Run("empty string", func(t *testing.T) { |
| 125 | + result := PointerToIfNonZero("") |
| 126 | + assert.Nil(t, result) |
| 127 | + }) |
| 128 | + |
| 129 | + t.Run("non-zero float64", func(t *testing.T) { |
| 130 | + result := PointerToIfNonZero(3.14) |
| 131 | + assert.NotNil(t, result) |
| 132 | + assert.Equal(t, 3.14, *result) |
| 133 | + }) |
| 134 | + |
| 135 | + t.Run("zero float64", func(t *testing.T) { |
| 136 | + result := PointerToIfNonZero(0.0) |
| 137 | + assert.Nil(t, result) |
| 138 | + }) |
| 139 | + |
| 140 | + t.Run("negative int is non-zero", func(t *testing.T) { |
| 141 | + result := PointerToIfNonZero(-5) |
| 142 | + assert.NotNil(t, result) |
| 143 | + assert.Equal(t, -5, *result) |
| 144 | + }) |
| 145 | + |
| 146 | + t.Run("negative float is non-zero", func(t *testing.T) { |
| 147 | + result := PointerToIfNonZero(-3.14) |
| 148 | + assert.NotNil(t, result) |
| 149 | + assert.Equal(t, -3.14, *result) |
| 150 | + }) |
| 151 | +} |
| 152 | + |
| 153 | +// Example tests for documentation |
| 154 | + |
| 155 | +func ExamplePointerToOrNil() { |
| 156 | + type GeoLocation struct { |
| 157 | + Latitude float64 |
| 158 | + Longitude float64 |
| 159 | + } |
| 160 | + |
| 161 | + geo := &GeoLocation{Latitude: 48.8566, Longitude: 2.3522} |
| 162 | + |
| 163 | + // Using PointerToOrNil for conditional pointer creation |
| 164 | + latPtr := PointerToOrNil(geo.Latitude, geo != nil && geo.Latitude != 0) |
| 165 | + |
| 166 | + if latPtr != nil { |
| 167 | + println(*latPtr) // 48.8566 |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +func ExamplePointerToIf() { |
| 172 | + // Validate latitude is in valid range |
| 173 | + isValidLatitude := func(lat float64) bool { |
| 174 | + return lat >= -90 && lat <= 90 |
| 175 | + } |
| 176 | + |
| 177 | + validLat := PointerToIf(48.8566, isValidLatitude) // returns pointer |
| 178 | + invalidLat := PointerToIf(200.0, isValidLatitude) // returns nil |
| 179 | + |
| 180 | + println(validLat != nil) // true |
| 181 | + println(invalidLat != nil) // false |
| 182 | +} |
| 183 | + |
| 184 | +func ExamplePointerToIfNonZero() { |
| 185 | + // Simple non-zero check |
| 186 | + name := PointerToIfNonZero("John") // returns pointer to "John" |
| 187 | + empty := PointerToIfNonZero("") // returns nil |
| 188 | + count := PointerToIfNonZero(5) // returns pointer to 5 |
| 189 | + zero := PointerToIfNonZero(0) // returns nil |
| 190 | + |
| 191 | + println(name != nil) // true |
| 192 | + println(empty != nil) // false |
| 193 | + println(count != nil) // true |
| 194 | + println(zero != nil) // false |
| 195 | +} |
0 commit comments