| 1 |
package util |
| 2 |
|
| 3 |
import ( |
| 4 |
"reflect" |
| 5 |
"testing" |
| 6 |
|
| 7 |
"github.com/stretchr/testify/assert" |
| 8 |
) |
| 9 |
|
| 10 |
func TestIsUnsignedInt(t *testing.T) { |
| 11 |
t.Parallel() |
| 12 |
|
| 13 |
tests := []struct { |
| 14 |
name string |
| 15 |
kind reflect.Kind |
| 16 |
want bool |
| 17 |
}{ |
| 18 |
{ |
| 19 |
name: "returns true for Uint", |
| 20 |
kind: reflect.Uint, |
| 21 |
want: true, |
| 22 |
}, |
| 23 |
{ |
| 24 |
name: "returns true for Uint8", |
| 25 |
kind: reflect.Uint8, |
| 26 |
want: true, |
| 27 |
}, |
| 28 |
{ |
| 29 |
name: "returns true for Uint16", |
| 30 |
kind: reflect.Uint16, |
| 31 |
want: true, |
| 32 |
}, |
| 33 |
{ |
| 34 |
name: "returns true for Uint32", |
| 35 |
kind: reflect.Uint32, |
| 36 |
want: true, |
| 37 |
}, |
| 38 |
{ |
| 39 |
name: "returns true for Uint64", |
| 40 |
kind: reflect.Uint64, |
| 41 |
want: true, |
| 42 |
}, |
| 43 |
{ |
| 44 |
name: "returns false for Uintptr", |
| 45 |
kind: reflect.Uintptr, |
| 46 |
want: false, |
| 47 |
}, |
| 48 |
{ |
| 49 |
name: "returns false for Int", |
| 50 |
kind: reflect.Int, |
| 51 |
want: false, |
| 52 |
}, |
| 53 |
{ |
| 54 |
name: "returns false for Int8", |
| 55 |
kind: reflect.Int8, |
| 56 |
want: false, |
| 57 |
}, |
| 58 |
{ |
| 59 |
name: "returns false for String", |
| 60 |
kind: reflect.String, |
| 61 |
want: false, |
| 62 |
}, |
| 63 |
{ |
| 64 |
name: "returns false for Float64", |
| 65 |
kind: reflect.Float64, |
| 66 |
want: false, |
| 67 |
}, |
| 68 |
} |
| 69 |
|
| 70 |
for _, tt := range tests { |
| 71 |
t.Run(tt.name, func(t *testing.T) { |
| 72 |
t.Parallel() |
| 73 |
|
| 74 |
got := IsUnsignedInt(tt.kind) |
| 75 |
assert.Equal(t, tt.want, got, "IsUnsignedInt(%v) should return %v", tt.kind, tt.want) |
| 76 |
}) |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
func TestIsSignedInt(t *testing.T) { |
| 81 |
t.Parallel() |
| 82 |
|
| 83 |
tests := []struct { |
| 84 |
name string |
| 85 |
kind reflect.Kind |
| 86 |
want bool |
| 87 |
}{ |
| 88 |
{ |
| 89 |
name: "returns true for Int", |
| 90 |
kind: reflect.Int, |
| 91 |
want: true, |
| 92 |
}, |
| 93 |
{ |
| 94 |
name: "returns true for Int8", |
| 95 |
kind: reflect.Int8, |
| 96 |
want: true, |
| 97 |
}, |
| 98 |
{ |
| 99 |
name: "returns true for Int16", |
| 100 |
kind: reflect.Int16, |
| 101 |
want: true, |
| 102 |
}, |
| 103 |
{ |
| 104 |
name: "returns true for Int32", |
| 105 |
kind: reflect.Int32, |
| 106 |
want: true, |
| 107 |
}, |
| 108 |
{ |
| 109 |
name: "returns true for Int64", |
| 110 |
kind: reflect.Int64, |
| 111 |
want: true, |
| 112 |
}, |
| 113 |
{ |
| 114 |
name: "returns false for Uint", |
| 115 |
kind: reflect.Uint, |
| 116 |
want: false, |
| 117 |
}, |
| 118 |
{ |
| 119 |
name: "returns false for Uint8", |
| 120 |
kind: reflect.Uint8, |
| 121 |
want: false, |
| 122 |
}, |
| 123 |
{ |
| 124 |
name: "returns false for String", |
| 125 |
kind: reflect.String, |
| 126 |
want: false, |
| 127 |
}, |
| 128 |
{ |
| 129 |
name: "returns false for Float64", |
| 130 |
kind: reflect.Float64, |
| 131 |
want: false, |
| 132 |
}, |
| 133 |
} |
| 134 |
|
| 135 |
for _, tt := range tests { |
| 136 |
t.Run(tt.name, func(t *testing.T) { |
| 137 |
t.Parallel() |
| 138 |
|
| 139 |
got := IsSignedInt(tt.kind) |
| 140 |
assert.Equal(t, tt.want, got, "IsSignedInt(%v) should return %v", tt.kind, tt.want) |
| 141 |
}) |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
func TestIsCollection(t *testing.T) { |
| 146 |
t.Parallel() |
| 147 |
|
| 148 |
tests := []struct { |
| 149 |
name string |
| 150 |
kind reflect.Kind |
| 151 |
want bool |
| 152 |
}{ |
| 153 |
{ |
| 154 |
name: "returns true for Slice", |
| 155 |
kind: reflect.Slice, |
| 156 |
want: true, |
| 157 |
}, |
| 158 |
{ |
| 159 |
name: "returns true for Array", |
| 160 |
kind: reflect.Array, |
| 161 |
want: true, |
| 162 |
}, |
| 163 |
{ |
| 164 |
name: "returns false for Map", |
| 165 |
kind: reflect.Map, |
| 166 |
want: false, |
| 167 |
}, |
| 168 |
{ |
| 169 |
name: "returns false for String", |
| 170 |
kind: reflect.String, |
| 171 |
want: false, |
| 172 |
}, |
| 173 |
{ |
| 174 |
name: "returns false for Int", |
| 175 |
kind: reflect.Int, |
| 176 |
want: false, |
| 177 |
}, |
| 178 |
{ |
| 179 |
name: "returns false for Struct", |
| 180 |
kind: reflect.Struct, |
| 181 |
want: false, |
| 182 |
}, |
| 183 |
{ |
| 184 |
name: "returns false for Chan", |
| 185 |
kind: reflect.Chan, |
| 186 |
want: false, |
| 187 |
}, |
| 188 |
} |
| 189 |
|
| 190 |
for _, tt := range tests { |
| 191 |
t.Run(tt.name, func(t *testing.T) { |
| 192 |
t.Parallel() |
| 193 |
|
| 194 |
got := IsCollection(tt.kind) |
| 195 |
assert.Equal(t, tt.want, got, "IsCollection(%v) should return %v", tt.kind, tt.want) |
| 196 |
}) |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
func TestIsNumeric(t *testing.T) { |
| 201 |
t.Parallel() |
| 202 |
|
| 203 |
tests := []struct { |
| 204 |
name string |
| 205 |
kind reflect.Kind |
| 206 |
want bool |
| 207 |
}{ |
| 208 |
{ |
| 209 |
name: "returns true for Int", |
| 210 |
kind: reflect.Int, |
| 211 |
want: true, |
| 212 |
}, |
| 213 |
{ |
| 214 |
name: "returns true for Int8", |
| 215 |
kind: reflect.Int8, |
| 216 |
want: true, |
| 217 |
}, |
| 218 |
{ |
| 219 |
name: "returns true for Int16", |
| 220 |
kind: reflect.Int16, |
| 221 |
want: true, |
| 222 |
}, |
| 223 |
{ |
| 224 |
name: "returns true for Int32", |
| 225 |
kind: reflect.Int32, |
| 226 |
want: true, |
| 227 |
}, |
| 228 |
{ |
| 229 |
name: "returns true for Int64", |
| 230 |
kind: reflect.Int64, |
| 231 |
want: true, |
| 232 |
}, |
| 233 |
{ |
| 234 |
name: "returns true for Uint", |
| 235 |
kind: reflect.Uint, |
| 236 |
want: true, |
| 237 |
}, |
| 238 |
{ |
| 239 |
name: "returns true for Uint8", |
| 240 |
kind: reflect.Uint8, |
| 241 |
want: true, |
| 242 |
}, |
| 243 |
{ |
| 244 |
name: "returns true for Float32", |
| 245 |
kind: reflect.Float32, |
| 246 |
want: true, |
| 247 |
}, |
| 248 |
{ |
| 249 |
name: "returns true for Float64", |
| 250 |
kind: reflect.Float64, |
| 251 |
want: true, |
| 252 |
}, |
| 253 |
{ |
| 254 |
name: "returns true for Complex64", |
| 255 |
kind: reflect.Complex64, |
| 256 |
want: true, |
| 257 |
}, |
| 258 |
{ |
| 259 |
name: "returns true for Complex128", |
| 260 |
kind: reflect.Complex128, |
| 261 |
want: true, |
| 262 |
}, |
| 263 |
{ |
| 264 |
name: "returns false for String", |
| 265 |
kind: reflect.String, |
| 266 |
want: false, |
| 267 |
}, |
| 268 |
{ |
| 269 |
name: "returns false for Bool", |
| 270 |
kind: reflect.Bool, |
| 271 |
want: false, |
| 272 |
}, |
| 273 |
{ |
| 274 |
name: "returns false for Slice", |
| 275 |
kind: reflect.Slice, |
| 276 |
want: false, |
| 277 |
}, |
| 278 |
{ |
| 279 |
name: "returns false for Pointer", |
| 280 |
kind: reflect.Pointer, |
| 281 |
want: false, |
| 282 |
}, |
| 283 |
} |
| 284 |
|
| 285 |
for _, tt := range tests { |
| 286 |
t.Run(tt.name, func(t *testing.T) { |
| 287 |
t.Parallel() |
| 288 |
|
| 289 |
got := IsNumeric(tt.kind) |
| 290 |
assert.Equal(t, tt.want, got, "IsNumeric(%v) should return %v", tt.kind, tt.want) |
| 291 |
}) |
| 292 |
} |
| 293 |
} |