github.com/unionj-cloud/go-doudou@v1.3.8-0.20221011095552-0088008e5b31/toolkit/sliceutils/sliceutils_test.go (about) 1 package sliceutils 2 3 import ( 4 "github.com/stretchr/testify/assert" 5 "github.com/unionj-cloud/go-doudou/toolkit/cast" 6 "reflect" 7 "testing" 8 ) 9 10 func TestContains(t *testing.T) { 11 type args struct { 12 src []interface{} 13 test interface{} 14 } 15 tests := []struct { 16 name string 17 args args 18 want bool 19 }{ 20 { 21 name: "1", 22 args: args{ 23 src: []interface{}{"a", "2", "c"}, 24 test: "2", 25 }, 26 want: true, 27 }, 28 { 29 name: "1", 30 args: args{ 31 src: []interface{}{"a", "2", "c"}, 32 test: "3", 33 }, 34 want: false, 35 }, 36 } 37 for _, tt := range tests { 38 t.Run(tt.name, func(t *testing.T) { 39 if got := Contains(tt.args.src, tt.args.test); got != tt.want { 40 t.Errorf("Contains() = %v, want %v", got, tt.want) 41 } 42 }) 43 } 44 } 45 46 func TestInterfaceSlice2StringSlice(t *testing.T) { 47 type args struct { 48 strSlice []interface{} 49 } 50 tests := []struct { 51 name string 52 args args 53 want []string 54 }{ 55 { 56 name: "2", 57 args: args{ 58 strSlice: []interface{}{"a", "n"}, 59 }, 60 want: []string{"a", "n"}, 61 }, 62 } 63 for _, tt := range tests { 64 t.Run(tt.name, func(t *testing.T) { 65 if got := InterfaceSlice2StringSlice(tt.args.strSlice); !reflect.DeepEqual(got, tt.want) { 66 t.Errorf("InterfaceSlice2StringSlice() = %v, want %v", got, tt.want) 67 } 68 }) 69 } 70 } 71 72 func TestIndexOfAny(t *testing.T) { 73 type args struct { 74 target interface{} 75 anySlice interface{} 76 } 77 tests := []struct { 78 name string 79 args args 80 want int 81 wantErr bool 82 }{ 83 { 84 name: "", 85 args: args{ 86 target: "a", 87 anySlice: []string{ 88 "b", "m", "a", "K", 89 }, 90 }, 91 want: 2, 92 wantErr: false, 93 }, 94 { 95 name: "", 96 args: args{ 97 target: "a", 98 anySlice: "abc", 99 }, 100 want: -1, 101 wantErr: true, 102 }, 103 } 104 for _, tt := range tests { 105 t.Run(tt.name, func(t *testing.T) { 106 got, err := IndexOfAny(tt.args.target, tt.args.anySlice) 107 if (err != nil) != tt.wantErr { 108 t.Errorf("IndexOfAny() error = %v, wantErr %v", err, tt.wantErr) 109 return 110 } 111 if got != tt.want { 112 t.Errorf("IndexOfAny() got = %v, want %v", got, tt.want) 113 } 114 }) 115 } 116 } 117 118 func TestIndexOfAnyInt(t *testing.T) { 119 type args struct { 120 target interface{} 121 anySlice interface{} 122 } 123 tests := []struct { 124 name string 125 args args 126 want int 127 wantErr bool 128 }{ 129 { 130 name: "4", 131 args: args{ 132 target: 3, 133 anySlice: []int{ 134 2, 5, 1, 6, 3, 8, 9, 135 }, 136 }, 137 want: 4, 138 wantErr: false, 139 }, 140 } 141 for _, tt := range tests { 142 t.Run(tt.name, func(t *testing.T) { 143 got, err := IndexOfAny(tt.args.target, tt.args.anySlice) 144 if (err != nil) != tt.wantErr { 145 t.Errorf("IndexOfAny() error = %v, wantErr %v", err, tt.wantErr) 146 return 147 } 148 if got != tt.want { 149 t.Errorf("IndexOfAny() got = %v, want %v", got, tt.want) 150 } 151 }) 152 } 153 } 154 155 func TestIndexOfAnyNotContain(t *testing.T) { 156 type args struct { 157 target interface{} 158 anySlice interface{} 159 } 160 tests := []struct { 161 name string 162 args args 163 want int 164 wantErr bool 165 }{ 166 { 167 name: "4", 168 args: args{ 169 target: 3, 170 anySlice: []int{ 171 2, 5, 1, 6, 11, 8, 9, 172 }, 173 }, 174 want: -1, 175 wantErr: false, 176 }, 177 } 178 for _, tt := range tests { 179 t.Run(tt.name, func(t *testing.T) { 180 got, err := IndexOfAny(tt.args.target, tt.args.anySlice) 181 if (err != nil) != tt.wantErr { 182 t.Errorf("IndexOfAny() error = %v, wantErr %v", err, tt.wantErr) 183 return 184 } 185 if got != tt.want { 186 t.Errorf("IndexOfAny() got = %v, want %v", got, tt.want) 187 } 188 }) 189 } 190 } 191 192 func TestConvertAny2Interface(t *testing.T) { 193 type args struct { 194 src interface{} 195 } 196 tests := []struct { 197 name string 198 args args 199 want []interface{} 200 wantErr bool 201 }{ 202 { 203 name: "", 204 args: args{ 205 src: []int{1, 2, 3}, 206 }, 207 want: []interface{}{1, 2, 3}, 208 wantErr: false, 209 }, 210 { 211 name: "", 212 args: args{ 213 src: "abc", 214 }, 215 want: nil, 216 wantErr: true, 217 }, 218 } 219 for _, tt := range tests { 220 t.Run(tt.name, func(t *testing.T) { 221 got, err := ConvertAny2Interface(tt.args.src) 222 if (err != nil) != tt.wantErr { 223 t.Errorf("ConvertAny2Interface() error = %v, wantErr %v", err, tt.wantErr) 224 return 225 } 226 if !reflect.DeepEqual(got, tt.want) { 227 t.Errorf("ConvertAny2Interface() got = %v, want %v", got, tt.want) 228 } 229 }) 230 } 231 } 232 233 func TestStringSlice2InterfaceSlice(t *testing.T) { 234 type args struct { 235 strSlice []string 236 } 237 tests := []struct { 238 name string 239 args args 240 want []interface{} 241 }{ 242 { 243 name: "", 244 args: args{ 245 strSlice: []string{"a", "b", "c"}, 246 }, 247 want: []interface{}{"a", "b", "c"}, 248 }, 249 } 250 for _, tt := range tests { 251 t.Run(tt.name, func(t *testing.T) { 252 if got := StringSlice2InterfaceSlice(tt.args.strSlice); !reflect.DeepEqual(got, tt.want) { 253 t.Errorf("StringSlice2InterfaceSlice() = %v, want %v", got, tt.want) 254 } 255 }) 256 } 257 } 258 259 type containsDeepStruct struct { 260 Name string 261 Age int 262 } 263 264 func TestContainsDeep(t *testing.T) { 265 type args struct { 266 src []interface{} 267 test interface{} 268 } 269 tests := []struct { 270 name string 271 args args 272 want bool 273 }{ 274 { 275 name: "1", 276 args: args{ 277 src: []interface{}{ 278 containsDeepStruct{ 279 Name: "Jack", 280 Age: 10, 281 }, 282 containsDeepStruct{ 283 Name: "Rose", 284 Age: 18, 285 }, 286 containsDeepStruct{ 287 Name: "David", 288 Age: 14, 289 }, 290 }, 291 test: containsDeepStruct{ 292 Name: "David", 293 Age: 14, 294 }, 295 }, 296 want: true, 297 }, 298 { 299 name: "2", 300 args: args{ 301 src: []interface{}{ 302 containsDeepStruct{ 303 Name: "Jack", 304 Age: 10, 305 }, 306 containsDeepStruct{ 307 Name: "Rose", 308 Age: 18, 309 }, 310 containsDeepStruct{ 311 Name: "David", 312 Age: 14, 313 }, 314 }, 315 test: containsDeepStruct{ 316 Name: "Lily", 317 Age: 14, 318 }, 319 }, 320 want: false, 321 }, 322 } 323 for _, tt := range tests { 324 t.Run(tt.name, func(t *testing.T) { 325 if got := ContainsDeep(tt.args.src, tt.args.test); got != tt.want { 326 t.Errorf("ContainsDeep() = %v, want %v", got, tt.want) 327 } 328 }) 329 } 330 } 331 332 func TestStringContains(t *testing.T) { 333 type args struct { 334 src []string 335 test string 336 } 337 tests := []struct { 338 name string 339 args args 340 want bool 341 }{ 342 { 343 name: "1", 344 args: args{ 345 src: []string{"a", "b", "c"}, 346 test: "b", 347 }, 348 want: true, 349 }, 350 { 351 name: "2", 352 args: args{ 353 src: []string{"a", "b", "c"}, 354 test: "d", 355 }, 356 want: false, 357 }, 358 } 359 for _, tt := range tests { 360 t.Run(tt.name, func(t *testing.T) { 361 if got := StringContains(tt.args.src, tt.args.test); got != tt.want { 362 t.Errorf("StringContains() = %v, want %v", got, tt.want) 363 } 364 }) 365 } 366 } 367 368 func TestIndexOf(t *testing.T) { 369 type args struct { 370 element string 371 data []string 372 } 373 tests := []struct { 374 name string 375 args args 376 want int 377 }{ 378 { 379 name: "", 380 args: args{ 381 element: "c", 382 data: []string{"a", "b", "c"}, 383 }, 384 want: 2, 385 }, 386 { 387 name: "", 388 args: args{ 389 element: "d", 390 data: []string{"a", "b", "c"}, 391 }, 392 want: -1, 393 }, 394 } 395 for _, tt := range tests { 396 t.Run(tt.name, func(t *testing.T) { 397 if got := IndexOf(tt.args.element, tt.args.data); got != tt.want { 398 t.Errorf("IndexOf() = %v, want %v", got, tt.want) 399 } 400 }) 401 } 402 } 403 404 func TestIsEmpty(t *testing.T) { 405 type args struct { 406 src interface{} 407 } 408 tests := []struct { 409 name string 410 args args 411 want bool 412 }{ 413 { 414 name: "", 415 args: args{ 416 src: []interface{}{}, 417 }, 418 want: true, 419 }, 420 { 421 name: "", 422 args: args{ 423 src: []interface{}{1}, 424 }, 425 want: false, 426 }, 427 } 428 for _, tt := range tests { 429 t.Run(tt.name, func(t *testing.T) { 430 if got := IsEmpty(tt.args.src); got != tt.want { 431 t.Errorf("IsEmpty() = %v, want %v", got, tt.want) 432 } 433 }) 434 } 435 } 436 437 func TestIsEmptyP(t *testing.T) { 438 assert.Panics(t, func() { 439 IsEmpty(1) 440 }) 441 } 442 443 func TestStringFilter(t *testing.T) { 444 type args struct { 445 src []string 446 fn func(item string) bool 447 } 448 tests := []struct { 449 name string 450 args args 451 want []string 452 }{ 453 { 454 name: "", 455 args: args{ 456 src: []string{"a", "2", "c"}, 457 fn: func(item string) bool { 458 _, err := cast.ToIntE(item) 459 return err == nil 460 }, 461 }, 462 want: []string{"2"}, 463 }, 464 } 465 for _, tt := range tests { 466 t.Run(tt.name, func(t *testing.T) { 467 assert.Equalf(t, tt.want, StringFilter(tt.args.src, tt.args.fn), "StringFilter(%v, %v)", tt.args.src, tt.args.fn) 468 }) 469 } 470 }