github.com/jxskiss/gopkg@v0.17.3/reflectx/reflect_test.go (about)

     1  package reflectx
     2  
     3  import (
     4  	"github.com/stretchr/testify/assert"
     5  	"math"
     6  	"reflect"
     7  	"testing"
     8  )
     9  
    10  func TestIsNilInterface(t *testing.T) {
    11  	testcases := []struct {
    12  		v    interface{}
    13  		want bool
    14  	}{
    15  		{nil, true},
    16  		{(map[string]int)(nil), true},
    17  		{([]string)(nil), true},
    18  		{(*int)(nil), true},
    19  		{(*simple)(nil), true},
    20  		{map[string]int{}, false},
    21  		{[]string{}, false},
    22  		{1, false},
    23  		{"abc", false},
    24  		{simple{}, false},
    25  		{&simple{}, false},
    26  	}
    27  	for i, tc := range testcases {
    28  		got := IsNilInterface(tc.v)
    29  		assert.Equalf(t, tc.want, got, "i= %v, v = %q", i, tc.v)
    30  	}
    31  }
    32  
    33  func TestCastInt(t *testing.T) {
    34  	values := []interface{}{
    35  		int8(1), int8(math.MinInt8), int8(math.MaxInt8),
    36  		int16(1), int16(math.MinInt16), int16(math.MaxInt16),
    37  		int32(1), int32(math.MinInt32), int32(math.MaxInt32),
    38  		int64(1), int64(math.MinInt64), int64(math.MaxInt64),
    39  		uint8(0), uint8(1), uint8(math.MaxUint8),
    40  		uint16(0), uint16(1), uint16(math.MaxUint16),
    41  		uint32(0), uint32(1), uint32(math.MaxUint32),
    42  		uint64(0), uint64(1), uint64(math.MaxUint64),
    43  		int(1), int(math.MinInt64), int(math.MaxInt64),
    44  		uint(0), uint(1), uint(math.MaxUint64),
    45  		uintptr(0), uintptr(1), uintptr(math.MaxUint64),
    46  	}
    47  	for _, x := range values {
    48  		want := ReflectInt(reflect.ValueOf(x))
    49  		got := CastInt(x)
    50  		assert.Equal(t, want, got)
    51  	}
    52  }