github.com/bingoohuang/gg@v0.0.0-20240325092523-45da7dee9335/pkg/goo/goo_test.go (about) 1 package goo_test 2 3 import ( 4 "errors" 5 "fmt" 6 "log" 7 "math" 8 "math/bits" 9 "reflect" 10 "testing" 11 12 "github.com/bingoohuang/gg/pkg/goo" 13 "github.com/stretchr/testify/assert" 14 ) 15 16 func TestBooleanType_NewInstance(t *testing.T) { 17 typ := goo.TypeOf(true) 18 assert.True(t, typ.IsBool()) 19 20 booleanType := typ.ToBoolType() 21 instance := booleanType.New() 22 assert.NotNil(t, instance) 23 24 boolPtr := instance.(*bool) 25 assert.False(t, *boolPtr) 26 } 27 28 func TestBooleanType_ToString(t *testing.T) { 29 typ := goo.TypeOf(true) 30 assert.True(t, typ.IsBool()) 31 booleanType := typ.ToBoolType() 32 33 assert.Equal(t, "true", booleanType.ToStr(true)) 34 assert.Equal(t, "false", booleanType.ToStr(false)) 35 } 36 37 func TestBooleanType_ToBoolean(t *testing.T) { 38 typ := goo.TypeOf(true) 39 assert.True(t, typ.IsBool()) 40 booleanType := typ.ToBoolType() 41 42 assert.True(t, booleanType.ToBool("true")) 43 assert.False(t, booleanType.ToBool("false")) 44 assert.Panics(t, func() { 45 booleanType.ToBool("test") 46 }) 47 } 48 49 func TestArrayType_GetElementType(t *testing.T) { 50 arr := [5]string{} 51 typ := goo.TypeOf(arr) 52 assert.True(t, typ.IsArray()) 53 54 arrayType := typ.ToArrayType() 55 assert.Equal(t, "string", arrayType.ElemType().NameFull()) 56 } 57 58 func TestArrayType_GetLength(t *testing.T) { 59 arr := [5]string{} 60 typ := goo.TypeOf(arr) 61 assert.True(t, typ.IsArray()) 62 63 arrayType := typ.ToArrayType() 64 assert.Equal(t, 5, arrayType.Len()) 65 } 66 67 func TestArrayType_NewInstance(t *testing.T) { 68 arr := [5]string{} 69 typ := goo.TypeOf(arr) 70 assert.True(t, typ.IsArray()) 71 72 arrayType := typ.ToArrayType() 73 instance := arrayType.New().(*[5]string) 74 assert.NotNil(t, instance) 75 assert.Len(t, *instance, 5) 76 } 77 78 type Stock struct { 79 quantity uint32 `json:"quantity" yaml:"quantity"` 80 } 81 82 type Product struct { 83 Name string `json:"name" yaml:"name" customTag:"customTagValue"` 84 price float64 `json:"price" yaml:"price"` 85 Stock `json:"stock" yaml:"stock" customTag:"customTagValue"` 86 supplier Supplier `json:invalid` 87 } 88 89 type Supplier struct { 90 Name string `json:"name" yaml:"name" ` 91 } 92 93 func TestMemberField_GetName(t *testing.T) { 94 typ := goo.TypeOf(&Product{}) 95 assert.True(t, typ.IsStruct()) 96 97 structType := typ.ToStructType() 98 assert.Equal(t, 4, structType.FieldNum()) 99 100 assert.Equal(t, "Name", structType.Fields()[0].Name()) 101 assert.Equal(t, "price", structType.Fields()[1].Name()) 102 assert.Equal(t, "Stock", structType.Fields()[2].Name()) 103 assert.Equal(t, "supplier", structType.Fields()[3].Name()) 104 } 105 106 func TestMemberField_String(t *testing.T) { 107 typ := goo.TypeOf(&Product{}) 108 assert.True(t, typ.IsStruct()) 109 110 structType := typ.ToStructType() 111 assert.Equal(t, 4, structType.FieldNum()) 112 113 assert.Equal(t, "Name", structType.Fields()[0].String()) 114 assert.Equal(t, "price", structType.Fields()[1].String()) 115 assert.Equal(t, "Stock", structType.Fields()[2].String()) 116 assert.Equal(t, "supplier", structType.Fields()[3].String()) 117 } 118 119 func TestMemberField_IsAnonymous(t *testing.T) { 120 typ := goo.TypeOf(&Product{}) 121 assert.True(t, typ.IsStruct()) 122 123 structType := typ.ToStructType() 124 assert.Equal(t, 4, structType.FieldNum()) 125 assert.False(t, structType.Fields()[0].IsAnonymous()) 126 assert.False(t, structType.Fields()[1].IsAnonymous()) 127 assert.True(t, structType.Fields()[2].IsAnonymous()) 128 assert.False(t, structType.Fields()[3].IsAnonymous()) 129 } 130 131 func TestMemberField_IsExported(t *testing.T) { 132 typ := goo.TypeOf(&Product{}) 133 assert.True(t, typ.IsStruct()) 134 135 structType := typ.ToStructType() 136 assert.Equal(t, 4, structType.FieldNum()) 137 assert.True(t, structType.Fields()[0].IsExported()) 138 assert.False(t, structType.Fields()[1].IsExported()) 139 assert.True(t, structType.Fields()[2].IsExported()) 140 assert.False(t, structType.Fields()[3].IsExported()) 141 } 142 143 func TestMemberField_CanSet(t *testing.T) { 144 typ := goo.TypeOf(&Product{}) 145 assert.True(t, typ.IsStruct()) 146 147 structType := typ.ToStructType() 148 assert.Equal(t, 4, structType.FieldNum()) 149 assert.True(t, structType.Fields()[0].CanSet()) 150 assert.False(t, structType.Fields()[1].CanSet()) 151 assert.True(t, structType.Fields()[2].CanSet()) 152 assert.False(t, structType.Fields()[3].CanSet()) 153 } 154 155 func TestMemberField_SetValue(t *testing.T) { 156 product := &Product{ 157 Name: "test-product", 158 price: 39.90, 159 Stock: Stock{ 160 quantity: 20, 161 }, 162 supplier: Supplier{ 163 Name: "test-supplier", 164 }, 165 } 166 typ := goo.TypeOf(product) 167 assert.True(t, typ.IsStruct()) 168 169 structType := typ.ToStructType() 170 assert.Equal(t, 4, structType.FieldNum()) 171 172 name := structType.Fields()[0].Get(product).(string) 173 assert.Equal(t, "test-product", name) 174 175 assert.Panics(t, func() { structType.Fields()[1].Get(product) }) 176 177 stock := structType.Fields()[2].Get(product).(Stock) 178 assert.Equal(t, uint32(20), stock.quantity) 179 180 assert.Panics(t, func() { structType.Fields()[3].Get(product) }) 181 assert.Panics(t, func() { structType.Fields()[0].Set(23, nil) }) 182 assert.Panics(t, func() { structType.Fields()[0].Set(*product, nil) }) 183 } 184 185 func TestMemberField_GetValue(t *testing.T) { 186 product := &Product{ 187 Name: "test-product", 188 price: 39.90, 189 Stock: Stock{ 190 quantity: 20, 191 }, 192 supplier: Supplier{ 193 Name: "test-supplier", 194 }, 195 } 196 typ := goo.TypeOf(product) 197 assert.True(t, typ.IsStruct()) 198 199 structType := typ.ToStructType() 200 assert.Equal(t, 4, structType.FieldNum()) 201 202 structType.Fields()[0].Set(product, "test-product-2") 203 assert.Equal(t, "test-product-2", product.Name) 204 205 assert.Panics(t, func() { structType.Fields()[1].Set(product, 23.20) }) 206 207 structType.Fields()[2].Set(product, Stock{quantity: 30}) 208 assert.Equal(t, uint32(30), product.quantity) 209 210 assert.Panics(t, func() { structType.Fields()[3].Set(product, Supplier{Name: "test-supplier-2"}) }) 211 assert.Panics(t, func() { structType.Fields()[0].Get(23) }) 212 assert.Panics(t, func() { structType.Fields()[1].Get(Stock{}) }) 213 } 214 215 func TestMemberField_GetTags(t *testing.T) { 216 product := &Product{ 217 Name: "test-product", 218 price: 39.90, 219 Stock: Stock{ 220 quantity: 20, 221 }, 222 supplier: Supplier{ 223 Name: "test-supplier", 224 }, 225 } 226 typ := goo.TypeOf(product) 227 assert.True(t, typ.IsStruct()) 228 229 structType := typ.ToStructType() 230 assert.Equal(t, 4, structType.FieldNum()) 231 232 assert.Equal(t, 3, len(structType.Fields()[0].Tags())) 233 assert.Equal(t, 2, len(structType.Fields()[1].Tags())) 234 assert.Equal(t, 3, len(structType.Fields()[2].Tags())) 235 assert.Equal(t, 0, len(structType.Fields()[3].Tags())) 236 237 // name 238 tag, err := structType.Fields()[0].TagByName("json") 239 assert.Nil(t, err) 240 assert.Equal(t, "json", tag.Name) 241 assert.Equal(t, "name", tag.Value) 242 243 tag, err = structType.Fields()[0].TagByName("yaml") 244 assert.Nil(t, err) 245 assert.Equal(t, "yaml", tag.Name) 246 assert.Equal(t, "name", tag.Value) 247 248 tag, err = structType.Fields()[0].TagByName("customTag") 249 assert.Nil(t, err) 250 assert.Equal(t, "customTag", tag.Name) 251 assert.Equal(t, "customTagValue", tag.Value) 252 253 tag, err = structType.Fields()[0].TagByName("nonExistTag") 254 assert.NotNil(t, err) 255 256 // price 257 tag, err = structType.Fields()[1].TagByName("json") 258 assert.Nil(t, err) 259 assert.Equal(t, "json", tag.Name) 260 assert.Equal(t, "price", tag.Value) 261 262 tag, err = structType.Fields()[1].TagByName("yaml") 263 assert.Nil(t, err) 264 assert.Equal(t, "yaml", tag.Name) 265 assert.Equal(t, "price", tag.Value) 266 267 // stock 268 tag, err = structType.Fields()[2].TagByName("json") 269 assert.Nil(t, err) 270 assert.Equal(t, "json", tag.Name) 271 assert.Equal(t, "stock", tag.Value) 272 273 tag, err = structType.Fields()[2].TagByName("yaml") 274 assert.Nil(t, err) 275 assert.Equal(t, "yaml", tag.Name) 276 assert.Equal(t, "stock", tag.Value) 277 278 tag, err = structType.Fields()[2].TagByName("customTag") 279 assert.Nil(t, err) 280 assert.Equal(t, "customTag", tag.Name) 281 assert.Equal(t, "customTagValue", tag.Value) 282 } 283 284 func testFunction(name string, i int, val bool, test *string) (string, error) { 285 return "test", errors.New("test error") 286 } 287 288 func TestFunctionType_GetFunctionParameterCount(t *testing.T) { 289 typ := goo.TypeOf(testFunction) 290 assert.True(t, typ.IsFunc()) 291 292 functionType := typ.ToFuncType() 293 assert.Equal(t, 4, functionType.InNum()) 294 } 295 296 func TestFunctionType_GetFunctionParameterTypes(t *testing.T) { 297 typ := goo.TypeOf(testFunction) 298 assert.True(t, typ.IsFunc()) 299 300 functionType := typ.ToFuncType() 301 parameterTypes := functionType.InTypes() 302 assert.Equal(t, goo.TypeOf("").GoType(), parameterTypes[0].GoType()) 303 assert.Equal(t, goo.TypeOf(0).GoType(), parameterTypes[1].GoType()) 304 assert.Equal(t, goo.TypeOf(true).GoType(), parameterTypes[2].GoType()) 305 } 306 307 func TestFunctionType_GetFunctionReturnTypeCount(t *testing.T) { 308 typ := goo.TypeOf(testFunction) 309 assert.True(t, typ.IsFunc()) 310 311 functionType := typ.ToFuncType() 312 assert.Equal(t, 2, functionType.OutNum()) 313 } 314 315 func TestFunctionType_GetFunctionReturnTypes(t *testing.T) { 316 typ := goo.TypeOf(testFunction) 317 assert.True(t, typ.IsFunc()) 318 319 functionType := typ.ToFuncType() 320 parameterTypes := functionType.OutTypes() 321 assert.Equal(t, goo.TypeOf("").GoType(), parameterTypes[0].GoType()) 322 assert.Equal(t, "error", parameterTypes[1].NameFull()) 323 } 324 325 func TestFunctionType_Call(t *testing.T) { 326 typ := goo.TypeOf(testFunction) 327 assert.True(t, typ.IsFunc()) 328 functionType := typ.ToFuncType() 329 330 args := make([]interface{}, 0) 331 args = append(args, "test") 332 args = append(args, 1) 333 args = append(args, nil) 334 args = append(args, nil) 335 336 outputs := functionType.Call(args) 337 assert.Len(t, outputs, 2) 338 339 assert.Equal(t, "test", outputs[0]) 340 assert.Equal(t, "test error", outputs[1].(error).Error()) 341 } 342 343 func TestFunctionType_Call_WithMissingParameters(t *testing.T) { 344 typ := goo.TypeOf(testFunction) 345 assert.True(t, typ.IsFunc()) 346 functionType := typ.ToFuncType() 347 348 args := make([]interface{}, 0) 349 args = append(args, "test") 350 args = append(args, 1) 351 352 assert.Panics(t, func() { 353 functionType.Call(args) 354 }) 355 } 356 357 type testInterface interface { 358 testMethod(name string, i int, val bool) (string, error) 359 testMethod2() 360 TestMethod3() 361 } 362 363 func TestInterfaceType_GetMethodCount(t *testing.T) { 364 typ := goo.TypeOf((*testInterface)(nil)) 365 assert.True(t, typ.IsInterface()) 366 367 interfaceType := typ.ToInterfaceType() 368 assert.Equal(t, 3, interfaceType.MethodNum()) 369 } 370 371 func TestInterfaceType_GetMethods(t *testing.T) { 372 typ := goo.TypeOf((*testInterface)(nil)) 373 assert.True(t, typ.IsInterface()) 374 375 interfaceType := typ.ToInterfaceType() 376 assert.Len(t, interfaceType.Methods(), 3) 377 } 378 379 func TestMapType_NewInstance(t *testing.T) { 380 m := make(map[string]bool) 381 typ := goo.TypeOf(m) 382 assert.True(t, typ.IsMap()) 383 384 mapType := typ.ToMapType() 385 newMapInstance := mapType.New().(map[string]bool) 386 assert.NotNil(t, newMapInstance) 387 } 388 389 func TestMapType_GetKeyType(t *testing.T) { 390 m := make(map[string]bool) 391 typ := goo.TypeOf(m) 392 assert.True(t, typ.IsMap()) 393 394 mapType := typ.ToMapType() 395 assert.Equal(t, "string", mapType.KeyType().NameFull()) 396 } 397 398 func TestMapType_GetValueType(t *testing.T) { 399 m := make(map[string]bool) 400 typ := goo.TypeOf(m) 401 assert.True(t, typ.IsMap()) 402 403 mapType := typ.ToMapType() 404 assert.Equal(t, "bool", mapType.ValueType().NameFull()) 405 } 406 407 func TestMemberMethod_GetName(t *testing.T) { 408 typ := goo.TypeOf((*testInterface)(nil)) 409 assert.True(t, typ.IsInterface()) 410 411 interfaceType := typ.ToInterfaceType() 412 assert.Len(t, interfaceType.Methods(), 3) 413 414 assert.Equal(t, "TestMethod3", interfaceType.Methods()[0].Name()) 415 assert.Equal(t, "testMethod", interfaceType.Methods()[1].Name()) 416 assert.Equal(t, "testMethod2", interfaceType.Methods()[2].Name()) 417 } 418 419 func TestMemberMethod_GetMethodParameterCount(t *testing.T) { 420 typ := goo.TypeOf((*testInterface)(nil)) 421 assert.True(t, typ.IsInterface()) 422 423 interfaceType := typ.ToInterfaceType() 424 assert.Len(t, interfaceType.Methods(), 3) 425 426 assert.Equal(t, 0, interfaceType.Methods()[0].InNum()) 427 assert.Equal(t, 3, interfaceType.Methods()[1].InNum()) 428 assert.Equal(t, 0, interfaceType.Methods()[2].InNum()) 429 } 430 431 func TestMemberMethod_GetMethodParameterTypes(t *testing.T) { 432 typ := goo.TypeOf((*testInterface)(nil)) 433 assert.True(t, typ.IsInterface()) 434 435 interfaceType := typ.ToInterfaceType() 436 assert.Len(t, interfaceType.Methods(), 3) 437 438 assert.Equal(t, 0, len(interfaceType.Methods()[0].InTypes())) 439 assert.Equal(t, 3, len(interfaceType.Methods()[1].InTypes())) 440 assert.Equal(t, 0, len(interfaceType.Methods()[2].InTypes())) 441 442 types := interfaceType.Methods()[1].InTypes() 443 assert.Equal(t, "string", types[0].NameFull()) 444 assert.Equal(t, "int", types[1].NameFull()) 445 assert.Equal(t, "bool", types[2].NameFull()) 446 } 447 448 func TestMemberMethod_GetMethodReturnTypeCount(t *testing.T) { 449 typ := goo.TypeOf((*testInterface)(nil)) 450 assert.True(t, typ.IsInterface()) 451 452 interfaceType := typ.ToInterfaceType() 453 assert.Len(t, interfaceType.Methods(), 3) 454 455 assert.Equal(t, 0, interfaceType.Methods()[0].OutNum()) 456 assert.Equal(t, 2, interfaceType.Methods()[1].OutNum()) 457 assert.Equal(t, 0, interfaceType.Methods()[2].OutNum()) 458 } 459 460 func TestMemberMethod_GetMethodReturnTypes(t *testing.T) { 461 typ := goo.TypeOf((*testInterface)(nil)) 462 assert.True(t, typ.IsInterface()) 463 464 interfaceType := typ.ToInterfaceType() 465 assert.Len(t, interfaceType.Methods(), 3) 466 467 assert.Equal(t, 0, len(interfaceType.Methods()[0].OutTypes())) 468 assert.Equal(t, 2, len(interfaceType.Methods()[1].OutTypes())) 469 assert.Equal(t, 0, len(interfaceType.Methods()[2].OutTypes())) 470 471 types := interfaceType.Methods()[1].OutTypes() 472 assert.Equal(t, "string", types[0].NameFull()) 473 assert.Equal(t, "error", types[1].NameFull()) 474 } 475 476 func TestMemberMethod_Invoke(t *testing.T) { 477 typ := goo.TypeOf(Dog{}) 478 structType := typ.ToStructType() 479 methods := structType.Methods() 480 481 assert.NotPanics(t, func() { 482 methods[0].Invoke(Dog{}) 483 methods[2].Invoke(Dog{}, nil, nil) 484 methods[2].Invoke(Dog{}, "test", nil) 485 outputs := methods[3].Invoke(Dog{}) 486 assert.Len(t, outputs, 1) 487 }) 488 489 assert.Panics(t, func() { 490 methods[0].Invoke(2) 491 }) 492 493 assert.Panics(t, func() { 494 methods[0].Invoke(Dog{}, "arg1", "arg2") 495 }) 496 497 assert.Panics(t, func() { 498 methods[0].Invoke(Product{}) 499 }) 500 } 501 502 func TestMemberMethod_IsExported(t *testing.T) { 503 typ := goo.TypeOf((*testInterface)(nil)) 504 assert.True(t, typ.IsInterface()) 505 506 interfaceType := typ.ToInterfaceType() 507 assert.Len(t, interfaceType.Methods(), 3) 508 509 assert.True(t, interfaceType.Methods()[0].IsExported()) 510 assert.False(t, interfaceType.Methods()[1].IsExported()) 511 assert.False(t, interfaceType.Methods()[2].IsExported()) 512 } 513 514 func TestMemberMethod_String(t *testing.T) { 515 typ := goo.TypeOf((*testInterface)(nil)) 516 assert.True(t, typ.IsInterface()) 517 518 interfaceType := typ.ToInterfaceType() 519 assert.Len(t, interfaceType.Methods(), 3) 520 521 assert.Equal(t, "TestMethod3", interfaceType.Methods()[0].String()) 522 assert.Equal(t, "testMethod", interfaceType.Methods()[1].String()) 523 assert.Equal(t, "testMethod2", interfaceType.Methods()[2].String()) 524 } 525 526 func TestSignedIntegerType(t *testing.T) { 527 intType := goo.TypeOf(8) 528 assert.True(t, intType.IsNumber()) 529 530 intNumberType := intType.ToNumberType() 531 assert.Equal(t, goo.IntType, intNumberType.Type()) 532 if bits.UintSize == goo.Bit32 { 533 assert.Equal(t, goo.Bit32, intNumberType.BitSize()) 534 } else { 535 assert.Equal(t, goo.Bit64, intNumberType.BitSize()) 536 } 537 538 assert.Panics(t, func() { 539 intNumberType.Overflow(&Animal{}) 540 }) 541 542 assert.Panics(t, func() { 543 intNumberType.ToString("test") 544 }) 545 546 int8Type := goo.TypeOf(int8(8)) 547 assert.True(t, int8Type.IsNumber()) 548 549 int8NumberType := int8Type.ToNumberType() 550 assert.Equal(t, goo.IntType, int8NumberType.Type()) 551 assert.Equal(t, goo.Bit8, int8NumberType.BitSize()) 552 553 assert.True(t, int8NumberType.Overflow(129)) 554 assert.True(t, int8NumberType.Overflow(-150)) 555 assert.Equal(t, "120", int8NumberType.ToString(120)) 556 557 assert.Panics(t, func() { 558 int8NumberType.ToString("test") 559 }) 560 561 int16Type := goo.TypeOf(int16(25)) 562 assert.True(t, int16Type.IsNumber()) 563 564 int16NumberType := int16Type.ToNumberType() 565 assert.Equal(t, goo.IntType, int16NumberType.Type()) 566 assert.Equal(t, goo.Bit16, int16NumberType.BitSize()) 567 568 assert.True(t, int16NumberType.Overflow(35974)) 569 assert.True(t, int16NumberType.Overflow(-39755)) 570 assert.Equal(t, "1575", int16NumberType.ToString(1575)) 571 572 assert.Panics(t, func() { 573 int16NumberType.ToString("test") 574 }) 575 576 int32Type := goo.TypeOf(int32(25)) 577 assert.True(t, int32Type.IsNumber()) 578 579 int32NumberType := int32Type.ToNumberType() 580 assert.Equal(t, goo.IntType, int32NumberType.Type()) 581 assert.Equal(t, goo.Bit32, int32NumberType.BitSize()) 582 583 assert.True(t, int32NumberType.Overflow(2443252523)) 584 assert.True(t, int32NumberType.Overflow(-2443252523)) 585 assert.Equal(t, "244325", int32NumberType.ToString(244325)) 586 587 assert.Panics(t, func() { 588 int32NumberType.ToString("test") 589 }) 590 591 int64Type := goo.TypeOf(int64(25)) 592 assert.True(t, int32Type.IsNumber()) 593 594 int64NumberType := int64Type.ToNumberType() 595 assert.Equal(t, goo.IntType, int64NumberType.Type()) 596 assert.Equal(t, goo.Bit64, int64NumberType.BitSize()) 597 assert.Equal(t, "244325", int64NumberType.ToString(244325)) 598 599 assert.Panics(t, func() { 600 int64NumberType.ToString("test") 601 }) 602 } 603 604 func TestSignedIntegerType_NewInstance(t *testing.T) { 605 intType := goo.TypeOf(8) 606 intNumberType := intType.ToNumberType() 607 val := intNumberType.New() 608 assert.NotNil(t, val.(*int)) 609 610 int8Type := goo.TypeOf(int8(8)) 611 int8NumberType := int8Type.ToNumberType() 612 val = int8NumberType.New() 613 assert.NotNil(t, val.(*int8)) 614 615 int16Type := goo.TypeOf(int16(25)) 616 int16NumberType := int16Type.ToNumberType() 617 val = int16NumberType.New() 618 assert.NotNil(t, val.(*int16)) 619 620 int32Type := goo.TypeOf(int32(25)) 621 int32NumberType := int32Type.ToNumberType() 622 val = int32NumberType.New() 623 assert.NotNil(t, val.(*int32)) 624 625 int64Type := goo.TypeOf(int64(25)) 626 int64NumberType := int64Type.ToNumberType() 627 val = int64NumberType.New() 628 assert.NotNil(t, val.(*int64)) 629 } 630 631 func TestUnSignedIntegerType(t *testing.T) { 632 intType := goo.TypeOf(uint(8)) 633 assert.True(t, intType.IsNumber()) 634 635 intNumberType := intType.ToNumberType() 636 assert.Equal(t, goo.IntType, intNumberType.Type()) 637 if bits.UintSize == goo.Bit32 { 638 assert.Equal(t, goo.Bit32, intNumberType.BitSize()) 639 } else { 640 assert.Equal(t, goo.Bit64, intNumberType.BitSize()) 641 } 642 643 assert.Panics(t, func() { 644 intNumberType.Overflow(&Animal{}) 645 }) 646 647 assert.Panics(t, func() { 648 intNumberType.ToString("test") 649 }) 650 651 int8Type := goo.TypeOf(uint8(8)) 652 assert.True(t, int8Type.IsNumber()) 653 654 int8NumberType := int8Type.ToNumberType() 655 assert.Equal(t, goo.IntType, int8NumberType.Type()) 656 assert.Equal(t, goo.Bit8, int8NumberType.BitSize()) 657 658 assert.True(t, int8NumberType.Overflow(uint(280))) 659 assert.Equal(t, "120", int8NumberType.ToString(uint(120))) 660 661 assert.Panics(t, func() { 662 int8NumberType.ToString("test") 663 }) 664 665 int16Type := goo.TypeOf(uint16(25)) 666 assert.True(t, int16Type.IsNumber()) 667 668 int16NumberType := int16Type.ToNumberType() 669 assert.Equal(t, goo.IntType, int16NumberType.Type()) 670 assert.Equal(t, goo.Bit16, int16NumberType.BitSize()) 671 672 assert.True(t, int16NumberType.Overflow(uint(68954))) 673 assert.Equal(t, "1575", int16NumberType.ToString(uint(1575))) 674 675 assert.Panics(t, func() { 676 int16NumberType.ToString("test") 677 }) 678 679 int32Type := goo.TypeOf(uint32(25)) 680 assert.True(t, int32Type.IsNumber()) 681 682 int32NumberType := int32Type.ToNumberType() 683 assert.Equal(t, goo.IntType, int32NumberType.Type()) 684 assert.Equal(t, goo.Bit32, int32NumberType.BitSize()) 685 686 assert.True(t, int32NumberType.Overflow(uint(2443252687523))) 687 assert.Equal(t, "244325", int32NumberType.ToString(uint(244325))) 688 689 assert.Panics(t, func() { 690 int32NumberType.ToString("test") 691 }) 692 693 int64Type := goo.TypeOf(uint64(25)) 694 assert.True(t, int32Type.IsNumber()) 695 696 int64NumberType := int64Type.ToNumberType() 697 assert.Equal(t, goo.IntType, int64NumberType.Type()) 698 assert.Equal(t, goo.Bit64, int64NumberType.BitSize()) 699 assert.Equal(t, "244325", int64NumberType.ToString(uint(244325))) 700 701 assert.Panics(t, func() { 702 int64NumberType.ToString("test") 703 }) 704 } 705 706 func TestUnSignedIntegerType_NewInstance(t *testing.T) { 707 intType := goo.TypeOf(uint(8)) 708 intNumberType := intType.ToNumberType() 709 val := intNumberType.New() 710 assert.NotNil(t, val.(*uint)) 711 712 int8Type := goo.TypeOf(uint8(8)) 713 int8NumberType := int8Type.ToNumberType() 714 val = int8NumberType.New() 715 assert.NotNil(t, val.(*uint8)) 716 717 int16Type := goo.TypeOf(uint16(25)) 718 int16NumberType := int16Type.ToNumberType() 719 val = int16NumberType.New() 720 assert.NotNil(t, val.(*uint16)) 721 722 int32Type := goo.TypeOf(uint32(25)) 723 int32NumberType := int32Type.ToNumberType() 724 val = int32NumberType.New() 725 assert.NotNil(t, val.(*uint32)) 726 727 int64Type := goo.TypeOf(uint64(25)) 728 int64NumberType := int64Type.ToNumberType() 729 val = int64NumberType.New() 730 assert.NotNil(t, val.(*uint64)) 731 } 732 733 func TestComplexType_ToString(t *testing.T) { 734 complexNumber := complex(14.3, 22.5) 735 typ := goo.TypeOf(complexNumber) 736 assert.True(t, typ.IsNumber()) 737 738 numberType := typ.ToNumberType() 739 assert.Equal(t, goo.ComplexType, numberType.Type()) 740 assert.Equal(t, "(14.300000+22.500000i)", numberType.ToString(complexNumber)) 741 742 assert.Panics(t, func() { 743 numberType.ToString(23) 744 }) 745 } 746 747 func TestComplexType_GetBitSize(t *testing.T) { 748 complexNumber := complex(14.3, 22.5) 749 typ := goo.TypeOf(complexNumber) 750 assert.True(t, typ.IsNumber()) 751 752 numberType := typ.ToNumberType() 753 assert.Equal(t, goo.ComplexType, numberType.Type()) 754 assert.Equal(t, goo.Bit128, numberType.BitSize()) 755 } 756 757 func TestComplexType_Overflow(t *testing.T) { 758 complexNumber := complex(14.3, 22.5) 759 typ := goo.TypeOf(complexNumber) 760 assert.True(t, typ.IsNumber()) 761 762 numberType := typ.ToNumberType() 763 assert.Equal(t, goo.ComplexType, numberType.Type()) 764 assert.Panics(t, func() { 765 numberType.Overflow(nil) 766 }) 767 } 768 769 func TestComplexType_NewInstance(t *testing.T) { 770 complexNumber := complex(14.3, 22.5) 771 typ := goo.TypeOf(complexNumber) 772 assert.True(t, typ.IsNumber()) 773 774 numberType := typ.ToNumberType() 775 assert.Equal(t, goo.ComplexType, numberType.Type()) 776 777 instance := numberType.New() 778 assert.NotNil(t, instance) 779 } 780 781 func TestComplexType_GetRealData(t *testing.T) { 782 complexNumber := complex(14.3, 22.5) 783 typ := goo.TypeOf(complexNumber) 784 assert.True(t, typ.IsNumber()) 785 786 numberType := typ.ToNumberType() 787 assert.Equal(t, goo.ComplexType, numberType.Type()) 788 789 complexType := numberType.(goo.Complex) 790 assert.Equal(t, 14.3, complexType.RealData(complexNumber)) 791 792 assert.Panics(t, func() { 793 complexType.RealData(23) 794 }) 795 } 796 797 func TestComplexType_GetImaginaryData(t *testing.T) { 798 complexNumber := complex(14.3, 22.5) 799 typ := goo.TypeOf(complexNumber) 800 assert.True(t, typ.IsNumber()) 801 802 numberType := typ.ToNumberType() 803 assert.Equal(t, goo.ComplexType, numberType.Type()) 804 805 complexType := numberType.(goo.Complex) 806 assert.Equal(t, 22.5, complexType.ImaginaryData(complexNumber)) 807 808 assert.Panics(t, func() { 809 complexType.ImaginaryData(23) 810 }) 811 } 812 813 func TestFloatType_GetType(t *testing.T) { 814 float32Number := float32(23.2) 815 typ := goo.TypeOf(float32Number) 816 assert.True(t, typ.IsNumber()) 817 818 numberType := typ.ToNumberType() 819 assert.Equal(t, goo.FloatType, numberType.Type()) 820 821 float64Number := 23.2 822 typ = goo.TypeOf(float64Number) 823 assert.True(t, typ.IsNumber()) 824 825 numberType = typ.ToNumberType() 826 assert.Equal(t, goo.FloatType, numberType.Type()) 827 } 828 829 func TestFloatType_GetBitSize(t *testing.T) { 830 float32Number := float32(23.2) 831 typ := goo.TypeOf(float32Number) 832 assert.True(t, typ.IsNumber()) 833 834 numberType := typ.ToNumberType() 835 assert.Equal(t, goo.Bit32, numberType.BitSize()) 836 837 float64Number := 23.2 838 typ = goo.TypeOf(float64Number) 839 assert.True(t, typ.IsNumber()) 840 841 numberType = typ.ToNumberType() 842 assert.Equal(t, goo.Bit64, numberType.BitSize()) 843 } 844 845 func TestFloatType_NewInstance(t *testing.T) { 846 float32Number := float32(23.2) 847 typ := goo.TypeOf(float32Number) 848 assert.True(t, typ.IsNumber()) 849 850 numberType := typ.ToNumberType() 851 val := numberType.New() 852 assert.NotNil(t, val) 853 854 float64Number := 23.2 855 typ = goo.TypeOf(float64Number) 856 assert.True(t, typ.IsNumber()) 857 858 numberType = typ.ToNumberType() 859 val = numberType.New() 860 assert.NotNil(t, val) 861 } 862 863 func TestFloatType_Overflow(t *testing.T) { 864 float32Number := float32(23.2) 865 typ := goo.TypeOf(float32Number) 866 assert.True(t, typ.IsNumber()) 867 868 numberType := typ.ToNumberType() 869 assert.Panics(t, func() { numberType.Overflow(&Animal{}) }) 870 } 871 872 func TestFloatType_ToString(t *testing.T) { 873 floatNumber := 23.2 874 typ := goo.TypeOf(floatNumber) 875 assert.True(t, typ.IsNumber()) 876 877 numberType := typ.ToNumberType() 878 879 assert.Panics(t, func() { numberType.ToString(&Animal{}) }) 880 assert.Equal(t, "23.200000", numberType.ToString(floatNumber)) 881 } 882 883 func TestSliceType_GetElementType(t *testing.T) { 884 var slice []string 885 typ := goo.TypeOf(slice) 886 assert.True(t, typ.IsSlice()) 887 888 sliceType := typ.ToSliceType() 889 assert.Equal(t, "string", sliceType.GetElementType().NameFull()) 890 } 891 892 func TestSliceType_NewInstance(t *testing.T) { 893 arr := [5]string{} 894 typ := goo.TypeOf(arr[2:3]) 895 assert.True(t, typ.IsSlice()) 896 897 sliceType := typ.ToSliceType() 898 instance := sliceType.New().([]string) 899 assert.NotNil(t, instance) 900 } 901 902 func TestStringType_NewInstance(t *testing.T) { 903 typ := goo.TypeOf("") 904 assert.True(t, typ.IsString()) 905 906 stringType := typ.ToStringType() 907 stringVal := stringType.New() 908 assert.NotNil(t, stringVal) 909 } 910 911 func TestStringType_ToFloat32(t *testing.T) { 912 typ := goo.TypeOf("") 913 assert.True(t, typ.IsString()) 914 915 stringType := typ.ToStringType() 916 floatVal := stringType.ToFloat32("23.22") 917 assert.Equal(t, float32(23.22), floatVal) 918 919 assert.Panics(t, func() { stringType.ToFloat32("") }) 920 } 921 922 func TestStringType_ToFloat64(t *testing.T) { 923 typ := goo.TypeOf("") 924 assert.True(t, typ.IsString()) 925 926 stringType := typ.ToStringType() 927 floatVal := stringType.ToFloat64("23.22") 928 assert.Equal(t, 23.22, floatVal) 929 930 assert.Panics(t, func() { stringType.ToFloat64("") }) 931 } 932 933 func TestStringType_ToNumber(t *testing.T) { 934 typ := goo.TypeOf("") 935 assert.True(t, typ.IsString()) 936 937 stringType := typ.ToStringType() 938 939 // float32 940 val, err := stringType.ToNumber("23.75", goo.TypeOf(float32(0)).ToNumberType()) 941 assert.Nil(t, err) 942 assert.Equal(t, float32(23.75), val) 943 944 val, err = stringType.ToNumber(fmt.Sprintf("%f", math.MaxFloat64-1.0), goo.TypeOf(float32(0)).ToNumberType()) 945 assert.NotNil(t, err) 946 947 val, err = stringType.ToNumber("", goo.TypeOf(float32(0)).ToNumberType()) 948 assert.NotNil(t, err) 949 950 // float64 951 val, err = stringType.ToNumber("23.75", goo.TypeOf(float64(0)).ToNumberType()) 952 assert.Nil(t, err) 953 assert.Equal(t, 23.75, val) 954 955 val, err = stringType.ToNumber("", goo.TypeOf(float64(0)).ToNumberType()) 956 assert.NotNil(t, err) 957 958 // int 959 val, err = stringType.ToNumber("23", goo.TypeOf(0).ToNumberType()) 960 assert.Nil(t, err) 961 assert.Equal(t, 23, val) 962 963 val, err = stringType.ToNumber("", goo.TypeOf(0).ToNumberType()) 964 assert.NotNil(t, err) 965 966 assert.Panics(t, func() { 967 stringType.ToNumber("", nil) 968 }) 969 970 // int8 971 val, err = stringType.ToNumber("23", goo.TypeOf(int8(0)).ToNumberType()) 972 assert.Nil(t, err) 973 assert.Equal(t, int8(23), val) 974 975 val, err = stringType.ToNumber("-128", goo.TypeOf(int8(0)).ToNumberType()) 976 assert.Nil(t, err) 977 assert.Equal(t, int8(-128), val) 978 979 val, err = stringType.ToNumber("-150", goo.TypeOf(int8(0)).ToNumberType()) 980 assert.NotNil(t, err) 981 982 val, err = stringType.ToNumber("", goo.TypeOf(int8(0)).ToNumberType()) 983 assert.NotNil(t, err) 984 985 // int16 986 val, err = stringType.ToNumber("19421", goo.TypeOf(int16(0)).ToNumberType()) 987 assert.Nil(t, err) 988 assert.Equal(t, int16(19421), val) 989 990 val, err = stringType.ToNumber("-15040", goo.TypeOf(int16(0)).ToNumberType()) 991 assert.Nil(t, err) 992 assert.Equal(t, int16(-15040), val) 993 994 val, err = stringType.ToNumber("32980", goo.TypeOf(int16(0)).ToNumberType()) 995 assert.NotNil(t, err) 996 997 val, err = stringType.ToNumber("", goo.TypeOf(int16(0)).ToNumberType()) 998 assert.NotNil(t, err) 999 1000 // int32 1001 val, err = stringType.ToNumber("243293245", goo.TypeOf(int32(0)).ToNumberType()) 1002 assert.Nil(t, err) 1003 assert.Equal(t, int32(243293245), val) 1004 1005 val, err = stringType.ToNumber("-243293245", goo.TypeOf(int32(0)).ToNumberType()) 1006 assert.Nil(t, err) 1007 assert.Equal(t, int32(-243293245), val) 1008 1009 val, err = stringType.ToNumber("23243293245", goo.TypeOf(int32(0)).ToNumberType()) 1010 assert.NotNil(t, err) 1011 1012 val, err = stringType.ToNumber("", goo.TypeOf(int64(0)).ToNumberType()) 1013 assert.NotNil(t, err) 1014 1015 // int64 1016 val, err = stringType.ToNumber("23243293245", goo.TypeOf(int64(0)).ToNumberType()) 1017 assert.Nil(t, err) 1018 assert.Equal(t, int64(23243293245), val) 1019 1020 val, err = stringType.ToNumber("-23243293245", goo.TypeOf(int64(0)).ToNumberType()) 1021 assert.Nil(t, err) 1022 assert.Equal(t, int64(-23243293245), val) 1023 1024 val, err = stringType.ToNumber("23545243293245741354", goo.TypeOf(int64(0)).ToNumberType()) 1025 assert.NotNil(t, err) 1026 1027 val, err = stringType.ToNumber("", goo.TypeOf(int64(0)).ToNumberType()) 1028 assert.NotNil(t, err) 1029 1030 // unit8 1031 val, err = stringType.ToNumber("23", goo.TypeOf(uint8(0)).ToNumberType()) 1032 assert.Nil(t, err) 1033 assert.Equal(t, uint8(23), val) 1034 1035 val, err = stringType.ToNumber("-150", goo.TypeOf(uint8(0)).ToNumberType()) 1036 assert.NotNil(t, err) 1037 1038 val, err = stringType.ToNumber("258", goo.TypeOf(uint8(0)).ToNumberType()) 1039 assert.NotNil(t, err) 1040 1041 val, err = stringType.ToNumber("", goo.TypeOf(uint8(0)).ToNumberType()) 1042 assert.NotNil(t, err) 1043 1044 // uint16 1045 val, err = stringType.ToNumber("19874", goo.TypeOf(uint16(0)).ToNumberType()) 1046 assert.Nil(t, err) 1047 assert.Equal(t, uint16(19874), val) 1048 1049 val, err = stringType.ToNumber("-150", goo.TypeOf(uint16(0)).ToNumberType()) 1050 assert.NotNil(t, err) 1051 1052 val, err = stringType.ToNumber("68419", goo.TypeOf(uint16(0)).ToNumberType()) 1053 assert.NotNil(t, err) 1054 1055 val, err = stringType.ToNumber("", goo.TypeOf(uint16(0)).ToNumberType()) 1056 assert.NotNil(t, err) 1057 1058 // uint32 1059 val, err = stringType.ToNumber("68941", goo.TypeOf(uint32(0)).ToNumberType()) 1060 assert.Nil(t, err) 1061 assert.Equal(t, uint32(68941), val) 1062 1063 val, err = stringType.ToNumber("-150", goo.TypeOf(uint32(0)).ToNumberType()) 1064 assert.NotNil(t, err) 1065 1066 val, err = stringType.ToNumber("254684571411", goo.TypeOf(uint32(0)).ToNumberType()) 1067 assert.NotNil(t, err) 1068 1069 val, err = stringType.ToNumber("", goo.TypeOf(uint32(0)).ToNumberType()) 1070 assert.NotNil(t, err) 1071 1072 // uint64 1073 val, err = stringType.ToNumber("254684571411", goo.TypeOf(uint64(0)).ToNumberType()) 1074 assert.Nil(t, err) 1075 assert.Equal(t, uint64(254684571411), val) 1076 1077 val, err = stringType.ToNumber("-150", goo.TypeOf(uint64(0)).ToNumberType()) 1078 assert.NotNil(t, err) 1079 1080 val, err = stringType.ToNumber("254684571411656202321", goo.TypeOf(uint64(0)).ToNumberType()) 1081 assert.NotNil(t, err) 1082 1083 val, err = stringType.ToNumber("", goo.TypeOf(uint64(0)).ToNumberType()) 1084 assert.NotNil(t, err) 1085 1086 val, err = stringType.ToNumber("", goo.TypeOf(complex(1, 2)).ToNumberType()) 1087 assert.NotNil(t, err) 1088 } 1089 1090 func TestStringType_ToInt(t *testing.T) { 1091 typ := goo.TypeOf("") 1092 assert.True(t, typ.IsString()) 1093 1094 stringType := typ.ToStringType() 1095 result := stringType.ToInt("23") 1096 1097 assert.Equal(t, 23, result) 1098 1099 assert.Panics(t, func() { 1100 stringType.ToInt("") 1101 }) 1102 } 1103 1104 func TestStringType_ToInt8(t *testing.T) { 1105 typ := goo.TypeOf("") 1106 assert.True(t, typ.IsString()) 1107 1108 stringType := typ.ToStringType() 1109 1110 result := stringType.ToInt8("23") 1111 assert.Equal(t, int8(23), result) 1112 1113 result = stringType.ToInt8("-128") 1114 assert.Equal(t, int8(-128), result) 1115 1116 assert.Panics(t, func() { 1117 result = stringType.ToInt8("150") 1118 }) 1119 1120 assert.Panics(t, func() { 1121 result = stringType.ToInt8("-130") 1122 }) 1123 1124 assert.Panics(t, func() { 1125 stringType.ToInt8("") 1126 }) 1127 } 1128 1129 func TestStringType_ToInt16(t *testing.T) { 1130 typ := goo.TypeOf("") 1131 assert.True(t, typ.IsString()) 1132 1133 stringType := typ.ToStringType() 1134 1135 result := stringType.ToInt16("19421") 1136 assert.Equal(t, int16(19421), result) 1137 1138 result = stringType.ToInt16("-15040") 1139 assert.Equal(t, int16(-15040), result) 1140 1141 assert.Panics(t, func() { result = stringType.ToInt16("32980") }) 1142 assert.Panics(t, func() { result = stringType.ToInt16("-35874") }) 1143 1144 assert.Panics(t, func() { stringType.ToInt16("") }) 1145 } 1146 1147 func TestStringType_ToInt32(t *testing.T) { 1148 typ := goo.TypeOf("") 1149 assert.True(t, typ.IsString()) 1150 1151 stringType := typ.ToStringType() 1152 1153 result := stringType.ToInt32("243293245") 1154 assert.Equal(t, int32(243293245), result) 1155 1156 result = stringType.ToInt32("-243293245") 1157 assert.Equal(t, int32(-243293245), result) 1158 1159 assert.Panics(t, func() { 1160 result = stringType.ToInt32("23243293245") 1161 }) 1162 1163 assert.Panics(t, func() { 1164 result = stringType.ToInt32("-23243293245") 1165 }) 1166 1167 assert.Panics(t, func() { 1168 stringType.ToInt32("") 1169 }) 1170 } 1171 1172 func TestStringType_ToInt64(t *testing.T) { 1173 typ := goo.TypeOf("") 1174 assert.True(t, typ.IsString()) 1175 1176 stringType := typ.ToStringType() 1177 1178 result := stringType.ToInt64("23243293245") 1179 assert.Equal(t, int64(23243293245), result) 1180 1181 result = stringType.ToInt64("-23243293245") 1182 assert.Equal(t, int64(-23243293245), result) 1183 1184 assert.Panics(t, func() { 1185 result = stringType.ToInt64("23545243293245741354") 1186 }) 1187 1188 assert.Panics(t, func() { 1189 result = stringType.ToInt64("-23545243293245741354") 1190 }) 1191 1192 assert.Panics(t, func() { 1193 stringType.ToInt64("") 1194 }) 1195 } 1196 1197 func TestStringType_ToUint(t *testing.T) { 1198 typ := goo.TypeOf("") 1199 assert.True(t, typ.IsString()) 1200 1201 stringType := typ.ToStringType() 1202 1203 result := stringType.ToUint("68941") 1204 assert.Equal(t, uint(68941), result) 1205 1206 assert.Panics(t, func() { 1207 result = stringType.ToUint("-150") 1208 }) 1209 } 1210 1211 func TestStringType_ToUint8(t *testing.T) { 1212 typ := goo.TypeOf("") 1213 assert.True(t, typ.IsString()) 1214 1215 stringType := typ.ToStringType() 1216 1217 result := stringType.ToUint8("23") 1218 assert.Equal(t, uint8(23), result) 1219 1220 assert.Panics(t, func() { 1221 result = stringType.ToUint8("-150") 1222 }) 1223 1224 assert.Panics(t, func() { 1225 result = stringType.ToUint8("258") 1226 }) 1227 1228 assert.Panics(t, func() { 1229 stringType.ToUint16("") 1230 }) 1231 } 1232 1233 func TestStringType_ToUint16(t *testing.T) { 1234 typ := goo.TypeOf("") 1235 assert.True(t, typ.IsString()) 1236 1237 stringType := typ.ToStringType() 1238 1239 result := stringType.ToUint16("19874") 1240 assert.Equal(t, uint16(19874), result) 1241 1242 assert.Panics(t, func() { 1243 result = stringType.ToUint16("-150") 1244 }) 1245 1246 assert.Panics(t, func() { 1247 result = stringType.ToUint16("68419") 1248 }) 1249 1250 assert.Panics(t, func() { 1251 stringType.ToUint16("") 1252 }) 1253 } 1254 1255 func TestStringType_ToUint32(t *testing.T) { 1256 typ := goo.TypeOf("") 1257 assert.True(t, typ.IsString()) 1258 1259 stringType := typ.ToStringType() 1260 1261 result := stringType.ToUint32("68941") 1262 assert.Equal(t, uint32(68941), result) 1263 1264 assert.Panics(t, func() { 1265 result = stringType.ToUint32("254684571411") 1266 }) 1267 1268 assert.Panics(t, func() { 1269 result = stringType.ToUint32("-150") 1270 }) 1271 1272 assert.Panics(t, func() { 1273 stringType.ToUint32("") 1274 }) 1275 } 1276 1277 func TestStringType_ToUint64(t *testing.T) { 1278 typ := goo.TypeOf("") 1279 assert.True(t, typ.IsString()) 1280 1281 stringType := typ.ToStringType() 1282 1283 result := stringType.ToUint64("254684571411") 1284 assert.Equal(t, uint64(254684571411), result) 1285 1286 assert.Panics(t, func() { 1287 result = stringType.ToUint64("254684571411656202321") 1288 }) 1289 1290 assert.Panics(t, func() { 1291 result = stringType.ToUint64("-150") 1292 }) 1293 1294 assert.Panics(t, func() { 1295 stringType.ToUint64("") 1296 }) 1297 } 1298 1299 func TestStructType_GetNames(t *testing.T) { 1300 testGetNamesForStruct(t, goo.TypeOf(Animal{})) 1301 testGetNamesForStruct(t, goo.TypeOf(&Animal{})) 1302 } 1303 1304 func testGetNamesForStruct(t *testing.T, typ goo.Type) { 1305 assert.Equal(t, "Animal", typ.Name()) 1306 assert.Equal(t, "github.com.bingoohuang.gg.pkg.goo.test.Animal", typ.NameFull()) 1307 assert.Equal(t, "goo_test", typ.PkgName()) 1308 assert.Equal(t, "github.com.bingoohuang.gg.pkg.goo.test", typ.PkgNameFull()) 1309 assert.Equal(t, typ.(goo.Struct), typ.ToStructType()) 1310 } 1311 1312 func TestStructType_GetFields(t *testing.T) { 1313 testGetFieldsForStruct(t, goo.TypeOf(Person{})) 1314 testGetFieldsForStruct(t, goo.TypeOf(&Person{})) 1315 } 1316 1317 func testGetFieldsForStruct(t *testing.T, typ goo.Type) { 1318 structType := typ.(goo.Struct) 1319 // all fields 1320 fieldCount := structType.FieldNum() 1321 assert.Equal(t, 5, fieldCount) 1322 fields := structType.Fields() 1323 assert.Equal(t, 5, len(fields)) 1324 1325 // exported fields 1326 fieldCount = structType.FieldExportedNum() 1327 assert.Equal(t, 3, fieldCount) 1328 fields = structType.FieldsExported() 1329 assert.Equal(t, 3, len(fields)) 1330 1331 // unexported fields 1332 fieldCount = structType.FieldUnexportedNum() 1333 assert.Equal(t, 2, fieldCount) 1334 fields = structType.FieldsUnexported() 1335 assert.Equal(t, 2, len(fields)) 1336 1337 // anonymous fields 1338 fieldCount = structType.FieldAnonymousNum() 1339 assert.Equal(t, 1, fieldCount) 1340 fields = structType.FieldsAnonymous() 1341 assert.Equal(t, 1, len(fields)) 1342 } 1343 1344 func TestStructType_GetStructMethods(t *testing.T) { 1345 typ := goo.TypeOf(Person{}) 1346 structType := typ.(goo.Struct) 1347 methodsCount := structType.MethodNum() 1348 assert.Equal(t, 2, methodsCount) 1349 methods := structType.Methods() 1350 assert.Equal(t, 2, len(methods)) 1351 1352 typ = goo.TypeOf(&Person{}) 1353 structType = typ.(goo.Struct) 1354 methodsCount = structType.MethodNum() 1355 assert.Equal(t, 3, methodsCount) 1356 methods = structType.Methods() 1357 assert.Equal(t, 3, len(methods)) 1358 } 1359 1360 func TestStructType_Implements(t *testing.T) { 1361 x := &Dog{} 1362 x.Run() 1363 typ := goo.TypeOf(Dog{}) 1364 structType := typ.(goo.Struct) 1365 assert.Equal(t, false, structType.Implements(goo.TypeOf((*Bark)(nil)).(goo.Interface))) 1366 assert.Equal(t, true, structType.Implements(goo.TypeOf((*Run)(nil)).(goo.Interface))) 1367 1368 typ = goo.TypeOf(&Dog{}) 1369 structType = typ.(goo.Struct) 1370 assert.Equal(t, true, structType.Implements(goo.TypeOf((*Bark)(nil)).(goo.Interface))) 1371 assert.Equal(t, true, structType.Implements(goo.TypeOf((*Run)(nil)).(goo.Interface))) 1372 } 1373 1374 func TestStructType_EmbeddedStruct(t *testing.T) { 1375 typ := goo.TypeOf(Dog{}) 1376 assert.True(t, typ.ToStructType().IsEmbedded(goo.TypeOf(Animal{}).ToStructType())) 1377 assert.True(t, typ.ToStructType().IsEmbedded(goo.TypeOf(Cell{}).ToStructType())) 1378 assert.False(t, typ.ToStructType().IsEmbedded(goo.TypeOf(Dog{}).ToStructType())) 1379 assert.Panics(t, func() { 1380 typ.ToStructType().IsEmbedded(nil) 1381 }) 1382 } 1383 1384 func TestStructType_NewInstance(t *testing.T) { 1385 typ := goo.TypeOf(Dog{}) 1386 instance := typ.ToStructType().New() 1387 assert.NotNil(t, instance) 1388 } 1389 1390 func TestTag_String(t *testing.T) { 1391 tag := &goo.Tag{ 1392 Name: "test-tag", 1393 Value: "test-value", 1394 } 1395 assert.Equal(t, tag.Name+"->"+tag.Value, tag.String()) 1396 } 1397 1398 type Run interface { 1399 Run() 1400 } 1401 1402 type Bark interface { 1403 Bark() 1404 } 1405 1406 type Cell struct{} 1407 1408 type Animal struct { 1409 Name string 1410 Cell 1411 } 1412 1413 func (animal Animal) SayHi() string { 1414 return "Hi, I'm " + animal.Name 1415 } 1416 1417 type Dog struct { 1418 Animal 1419 } 1420 1421 func (dog *Dog) Bark() { 1422 log.Print("Bark") 1423 } 1424 1425 func (dog Dog) Run() { 1426 log.Print("Run") 1427 } 1428 1429 func (dog Dog) Test(arg string, i *int) { 1430 } 1431 1432 func (dog Dog) TestOutputParam() string { 1433 return "TestOutputParam" 1434 } 1435 1436 type Person struct { 1437 name string 1438 Surname string 1439 age int 1440 Address Address 1441 Cell 1442 } 1443 1444 func (person Person) GetName() string { 1445 return person.name 1446 } 1447 1448 func (person Person) GetSurname() string { 1449 return person.Surname 1450 } 1451 1452 func (person Person) getAge() int { 1453 return person.age 1454 } 1455 1456 func (person *Person) GetAddress() Address { 1457 return person.Address 1458 } 1459 1460 type Address struct { 1461 city string 1462 country string 1463 } 1464 1465 func (address Address) GetCity() string { 1466 return address.city 1467 } 1468 1469 func (address Address) GetCountry() string { 1470 return address.country 1471 } 1472 1473 func TestType_IsMethods(t *testing.T) { 1474 typ := goo.TypeOf(Animal{}) 1475 assert.Equal(t, true, typ.IsStruct()) 1476 assert.Equal(t, false, typ.IsInterface()) 1477 assert.Equal(t, false, typ.IsFunc()) 1478 assert.Equal(t, false, typ.IsNumber()) 1479 assert.Equal(t, true, typ.IsInstantiable()) 1480 assert.Equal(t, false, typ.IsMap()) 1481 assert.Equal(t, false, typ.IsPtr()) 1482 assert.Equal(t, false, typ.IsArray()) 1483 assert.Equal(t, false, typ.IsString()) 1484 assert.Equal(t, false, typ.IsBool()) 1485 assert.Equal(t, false, typ.IsSlice()) 1486 1487 typ = goo.TypeOf(&Animal{}) 1488 assert.Equal(t, true, typ.IsStruct()) 1489 assert.Equal(t, false, typ.IsInterface()) 1490 assert.Equal(t, false, typ.IsFunc()) 1491 assert.Equal(t, false, typ.IsNumber()) 1492 assert.Equal(t, true, typ.IsInstantiable()) 1493 assert.Equal(t, false, typ.IsMap()) 1494 assert.Equal(t, true, typ.IsPtr()) 1495 assert.Equal(t, false, typ.IsArray()) 1496 assert.Equal(t, false, typ.IsString()) 1497 assert.Equal(t, false, typ.IsBool()) 1498 assert.Equal(t, false, typ.IsSlice()) 1499 } 1500 1501 func TestType_Instantiable(t *testing.T) { 1502 typ := goo.TypeOf(Animal{}) 1503 assert.True(t, typ.IsInstantiable()) 1504 1505 typ = goo.TypeOf((*testInterface)(nil)) 1506 assert.False(t, typ.IsInstantiable()) 1507 1508 typ = goo.TypeOf(testFunction) 1509 assert.False(t, typ.IsInstantiable()) 1510 } 1511 1512 func TestBaseType_Equals(t *testing.T) { 1513 typ := goo.TypeOf(Animal{}) 1514 assert.True(t, typ.Equals(goo.TypeOf(Animal{}))) 1515 assert.False(t, typ.Equals(goo.TypeOf(Dog{}))) 1516 assert.False(t, typ.Equals(nil)) 1517 } 1518 1519 func TestType_GetTypeFromGoTypeWithNil(t *testing.T) { 1520 assert.Panics(t, func() { goo.FromGoType(nil) }) 1521 } 1522 1523 func Test_isComplex(t *testing.T) { 1524 assert.True(t, goo.IsComplex(reflect.TypeOf(complex(14.3, 22.5)))) 1525 assert.False(t, goo.IsComplex(reflect.TypeOf(23))) 1526 } 1527 1528 func Test_getGoPointerTypeAndValueWithNil(t *testing.T) { 1529 assert.Panics(t, func() { goo.GoPtrTypeAndValue(nil) }) 1530 } 1531 1532 func Test_getGoTypeAndValueWithNil(t *testing.T) { 1533 assert.Panics(t, func() { goo.GoTypeAndValue(nil) }) 1534 }