xreflect_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package xreflect
  2. import (
  3. "reflect"
  4. "testing"
  5. "github.com/magiconair/properties/assert"
  6. "github.com/stretchr/testify/require"
  7. )
  8. type TestStruct struct {
  9. StringVal string
  10. IntVal int
  11. Int8Val int8
  12. Int16Val int16
  13. Int32Val int32
  14. Int64Val int64
  15. UintVal uint
  16. Uint8Val uint8
  17. Uint16Val uint16
  18. Uint32Val uint32
  19. Uint64Val uint64
  20. Float32Val float32
  21. Float64Val float64
  22. }
  23. type UnknownTypeStruct struct {
  24. UnknownType struct{}
  25. }
  26. func TestSetString(t *testing.T) {
  27. t.Run("set success", func(t *testing.T) {
  28. stringValues := []string{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11.123", "12.567"}
  29. v := &TestStruct{}
  30. refV := reflect.ValueOf(v).Elem()
  31. for i := 0; i < refV.NumField(); i++ {
  32. err := SetString(refV.Field(i), stringValues[i])
  33. require.NoError(t, err)
  34. }
  35. assert.Equal(t, "0", v.StringVal)
  36. assert.Equal(t, 1, v.IntVal)
  37. assert.Equal(t, int8(2), v.Int8Val)
  38. assert.Equal(t, int16(3), v.Int16Val)
  39. assert.Equal(t, int32(4), v.Int32Val)
  40. assert.Equal(t, int64(5), v.Int64Val)
  41. assert.Equal(t, uint(6), v.UintVal)
  42. assert.Equal(t, uint8(7), v.Uint8Val)
  43. assert.Equal(t, uint16(8), v.Uint16Val)
  44. assert.Equal(t, uint32(9), v.Uint32Val)
  45. assert.Equal(t, uint64(10), v.Uint64Val)
  46. assert.Equal(t, float32(11.123), v.Float32Val)
  47. assert.Equal(t, 12.567, v.Float64Val)
  48. })
  49. t.Run("format error", func(t *testing.T) {
  50. stringValues := []string{"0", "1.5", "2.5", "3.5", "4.5", "5.5", "6.5", "7.5", "8.5", "9.5", "10.5", "xxx", "xxx"}
  51. v := &TestStruct{}
  52. refV := reflect.ValueOf(v).Elem()
  53. for i := 1; i < refV.NumField(); i++ {
  54. err := SetString(refV.Field(i), stringValues[i])
  55. assert.Equal(t, true, err != nil)
  56. }
  57. })
  58. t.Run("unknown type", func(t *testing.T) {
  59. stringValues := []string{"0"}
  60. v := &UnknownTypeStruct{}
  61. refV := reflect.ValueOf(v).Elem()
  62. for i := 1; i < refV.NumField(); i++ {
  63. err := SetString(refV.Field(i), stringValues[i])
  64. assert.Equal(t, true, err != nil)
  65. }
  66. })
  67. }
  68. func TestToString(t *testing.T) {
  69. t.Run("format success", func(t *testing.T) {
  70. stringValues := []string{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11.123", "12.567"}
  71. v := &TestStruct{
  72. StringVal: "0",
  73. IntVal: 1,
  74. Int8Val: 2,
  75. Int16Val: 3,
  76. Int32Val: 4,
  77. Int64Val: 5,
  78. UintVal: 6,
  79. Uint8Val: 7,
  80. Uint16Val: 8,
  81. Uint32Val: 9,
  82. Uint64Val: 10,
  83. Float32Val: 11.123,
  84. Float64Val: 12.567,
  85. }
  86. refV := reflect.ValueOf(v).Elem()
  87. for i := 1; i < refV.NumField(); i++ {
  88. str, err := ToString(refV.Field(i))
  89. require.NoError(t, err)
  90. assert.Equal(t, stringValues[i], str)
  91. }
  92. })
  93. t.Run("unknown type", func(t *testing.T) {
  94. v := &UnknownTypeStruct{}
  95. refV := reflect.ValueOf(v).Elem()
  96. for i := 1; i < refV.NumField(); i++ {
  97. _, err := ToString(refV.Field(i))
  98. assert.Equal(t, ErrUnkownType, err)
  99. }
  100. })
  101. }