|
| 1 | +package pointer |
| 2 | + |
| 3 | +import ( |
| 4 | + "reflect" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | +) |
| 10 | + |
| 11 | +func TestOf(t *testing.T) { |
| 12 | + tests := []struct { |
| 13 | + name string |
| 14 | + target any |
| 15 | + }{ |
| 16 | + { |
| 17 | + name: "string type", |
| 18 | + target: "test", |
| 19 | + }, |
| 20 | + { |
| 21 | + name: "int type", |
| 22 | + target: 2, |
| 23 | + }, |
| 24 | + { |
| 25 | + name: "time type", |
| 26 | + target: func() time.Time { |
| 27 | + date, err := time.Parse(time.RFC3339, "2022-01-01T12:30:00Z") |
| 28 | + require.NoError(t, err) |
| 29 | + return date |
| 30 | + }(), |
| 31 | + }, |
| 32 | + { |
| 33 | + name: "uint32 type", |
| 34 | + target: uint32(4), |
| 35 | + }, |
| 36 | + } |
| 37 | + |
| 38 | + for _, tt := range tests { |
| 39 | + t.Run(tt.name, func(t *testing.T) { |
| 40 | + result := Of(tt.target) |
| 41 | + require.True(t, reflect.ValueOf(result).Kind() == reflect.Ptr) |
| 42 | + }) |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +func TestValue(t *testing.T) { |
| 47 | + var v = 12.5 |
| 48 | + if got := Value(Of(v), 0); got != v { |
| 49 | + t.Errorf("Value() = %v, want %v", got, &v) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +func TestValueWithDefault(t *testing.T) { |
| 54 | + var v = 12.5 |
| 55 | + if got := Value(nil, v); got != v { |
| 56 | + t.Errorf("Value() = %v, want %v", got, &v) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +func TestEqual(t *testing.T) { |
| 61 | + testCases := []struct { |
| 62 | + name string |
| 63 | + a *int |
| 64 | + b *int |
| 65 | + want bool |
| 66 | + }{ |
| 67 | + {name: "a, b nil", want: true}, |
| 68 | + {name: "a nill, b not", b: Of(1), want: false}, |
| 69 | + {name: "a not, b nil", a: Of(1), want: false}, |
| 70 | + {name: "a, b not equal", a: Of(1), b: Of(2), want: false}, |
| 71 | + {name: "a, b equal", a: Of(1), b: Of(1), want: true}, |
| 72 | + } |
| 73 | + for _, tt := range testCases { |
| 74 | + t.Run(tt.name, func(t *testing.T) { |
| 75 | + if got := Equal(tt.a, tt.b); got != tt.want { |
| 76 | + t.Errorf("Equal() = %v, want %v", got, tt.want) |
| 77 | + } |
| 78 | + }) |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +func TestEqualValue(t *testing.T) { |
| 83 | + testCases := []struct { |
| 84 | + name string |
| 85 | + p *int |
| 86 | + v int |
| 87 | + want bool |
| 88 | + }{ |
| 89 | + {name: "p nil", v: 123, want: false}, |
| 90 | + {name: "p not nil, not eq", p: Of(999), v: 123, want: false}, |
| 91 | + {name: "p not nil, eq", p: Of(123), v: 123, want: true}, |
| 92 | + } |
| 93 | + |
| 94 | + for _, tt := range testCases { |
| 95 | + t.Run(tt.name, func(t *testing.T) { |
| 96 | + if got := EqualValue(tt.p, tt.v); got != tt.want { |
| 97 | + t.Errorf("Equal() = %v, want %v", got, tt.want) |
| 98 | + } |
| 99 | + }) |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +func TestInt8(t *testing.T) { |
| 104 | + var v = int8(1) |
| 105 | + if got := Int8(v); !reflect.DeepEqual(got, &v) { |
| 106 | + t.Errorf("Int8() = %v, want %v", got, &v) |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +func TestInt16(t *testing.T) { |
| 111 | + var v = int16(1) |
| 112 | + if got := Int16(v); !reflect.DeepEqual(got, &v) { |
| 113 | + t.Errorf("Int16() = %v, want %v", got, &v) |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +func TestInt32(t *testing.T) { |
| 118 | + var v = int32(1) |
| 119 | + if got := Int32(v); !reflect.DeepEqual(got, &v) { |
| 120 | + t.Errorf("Int32() = %v, want %v", got, &v) |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +func TestInt64(t *testing.T) { |
| 125 | + var v = int64(1) |
| 126 | + if got := Int64(v); !reflect.DeepEqual(got, &v) { |
| 127 | + t.Errorf("Int64() = %v, want %v", got, &v) |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +func TestUInt8(t *testing.T) { |
| 132 | + var v = uint8(1) |
| 133 | + if got := UInt8(v); !reflect.DeepEqual(got, &v) { |
| 134 | + t.Errorf("UInt8() = %v, want %v", got, &v) |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +func TestUInt16(t *testing.T) { |
| 139 | + var v = uint16(1) |
| 140 | + if got := UInt16(v); !reflect.DeepEqual(got, &v) { |
| 141 | + t.Errorf("UInt16() = %v, want %v", got, &v) |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +func TestUInt32(t *testing.T) { |
| 146 | + var v = uint32(1) |
| 147 | + if got := UInt32(v); !reflect.DeepEqual(got, &v) { |
| 148 | + t.Errorf("UInt32() = %v, want %v", got, &v) |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +func TestUInt64(t *testing.T) { |
| 153 | + var v = uint64(1) |
| 154 | + if got := UInt64(v); !reflect.DeepEqual(got, &v) { |
| 155 | + t.Errorf("UInt64() = %v, want %v", got, &v) |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +func TestFloat32(t *testing.T) { |
| 160 | + var v = float32(1) |
| 161 | + if got := Float32(v); !reflect.DeepEqual(got, &v) { |
| 162 | + t.Errorf("Float32() = %v, want %v", got, &v) |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +func TestFloat64(t *testing.T) { |
| 167 | + var v = float64(1) |
| 168 | + if got := Float64(v); !reflect.DeepEqual(got, &v) { |
| 169 | + t.Errorf("Float64() = %v, want %v", got, &v) |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | +func TestComplex64(t *testing.T) { |
| 174 | + var v = complex64(1) |
| 175 | + if got := Complex64(v); !reflect.DeepEqual(got, &v) { |
| 176 | + t.Errorf("Complex64() = %v, want %v", got, &v) |
| 177 | + } |
| 178 | +} |
| 179 | + |
| 180 | +func TestComplex128(t *testing.T) { |
| 181 | + var v = complex128(1) |
| 182 | + if got := Complex128(v); !reflect.DeepEqual(got, &v) { |
| 183 | + t.Errorf("Complex128() = %v, want %v", got, &v) |
| 184 | + } |
| 185 | +} |
0 commit comments