123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- package xreflect
- import (
- "reflect"
- "testing"
- "github.com/magiconair/properties/assert"
- "github.com/stretchr/testify/require"
- )
- type TestStruct struct {
- StringVal string
- IntVal int
- Int8Val int8
- Int16Val int16
- Int32Val int32
- Int64Val int64
- UintVal uint
- Uint8Val uint8
- Uint16Val uint16
- Uint32Val uint32
- Uint64Val uint64
- Float32Val float32
- Float64Val float64
- }
- type UnknownTypeStruct struct {
- UnknownType struct{}
- }
- func TestSetString(t *testing.T) {
- t.Run("set success", func(t *testing.T) {
- stringValues := []string{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11.123", "12.567"}
- v := &TestStruct{}
- refV := reflect.ValueOf(v).Elem()
- for i := 0; i < refV.NumField(); i++ {
- err := SetString(refV.Field(i), stringValues[i])
- require.NoError(t, err)
- }
- assert.Equal(t, "0", v.StringVal)
- assert.Equal(t, 1, v.IntVal)
- assert.Equal(t, int8(2), v.Int8Val)
- assert.Equal(t, int16(3), v.Int16Val)
- assert.Equal(t, int32(4), v.Int32Val)
- assert.Equal(t, int64(5), v.Int64Val)
- assert.Equal(t, uint(6), v.UintVal)
- assert.Equal(t, uint8(7), v.Uint8Val)
- assert.Equal(t, uint16(8), v.Uint16Val)
- assert.Equal(t, uint32(9), v.Uint32Val)
- assert.Equal(t, uint64(10), v.Uint64Val)
- assert.Equal(t, float32(11.123), v.Float32Val)
- assert.Equal(t, 12.567, v.Float64Val)
- })
- t.Run("format error", func(t *testing.T) {
- 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"}
- v := &TestStruct{}
- refV := reflect.ValueOf(v).Elem()
- for i := 1; i < refV.NumField(); i++ {
- err := SetString(refV.Field(i), stringValues[i])
- assert.Equal(t, true, err != nil)
- }
- })
- t.Run("unknown type", func(t *testing.T) {
- stringValues := []string{"0"}
- v := &UnknownTypeStruct{}
- refV := reflect.ValueOf(v).Elem()
- for i := 1; i < refV.NumField(); i++ {
- err := SetString(refV.Field(i), stringValues[i])
- assert.Equal(t, true, err != nil)
- }
- })
- }
- func TestToString(t *testing.T) {
- t.Run("format success", func(t *testing.T) {
- stringValues := []string{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11.123", "12.567"}
- v := &TestStruct{
- StringVal: "0",
- IntVal: 1,
- Int8Val: 2,
- Int16Val: 3,
- Int32Val: 4,
- Int64Val: 5,
- UintVal: 6,
- Uint8Val: 7,
- Uint16Val: 8,
- Uint32Val: 9,
- Uint64Val: 10,
- Float32Val: 11.123,
- Float64Val: 12.567,
- }
- refV := reflect.ValueOf(v).Elem()
- for i := 1; i < refV.NumField(); i++ {
- str, err := ToString(refV.Field(i))
- require.NoError(t, err)
- assert.Equal(t, stringValues[i], str)
- }
- })
- t.Run("unknown type", func(t *testing.T) {
- v := &UnknownTypeStruct{}
- refV := reflect.ValueOf(v).Elem()
- for i := 1; i < refV.NumField(); i++ {
- _, err := ToString(refV.Field(i))
- assert.Equal(t, ErrUnkownType, err)
- }
- })
- }
|