github.com/avicd/go-utilx@v0.1.0/refx/assert_test.go (about)

     1  package refx
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"github.com/stretchr/testify/assert"
     7  	"reflect"
     8  	"testing"
     9  )
    10  
    11  type testInterface interface {
    12  	TestIs()
    13  }
    14  
    15  type testAssert struct {
    16  }
    17  
    18  func (ta *testAssert) TestIs() {
    19  
    20  }
    21  
    22  func TestIs(t *testing.T) {
    23  	p0 := &testAssert{}
    24  	mp := map[string]any{}
    25  	var p1 any = p0
    26  
    27  	// IsAnyOf
    28  	assert.Equal(t, false, IsAnyOf(p0, reflect.Interface))
    29  	assert.Equal(t, true, IsAnyOf(p0, reflect.Interface, reflect.Pointer))
    30  	assert.Equal(t, true, IsAnyOf(p0, reflect.Map, reflect.Struct))
    31  
    32  	// IsInterface
    33  	assert.Equal(t, true, IsInterface(reflect.TypeOf(mp).Elem()))
    34  
    35  	// IsPointer
    36  	assert.Equal(t, true, IsPointer(p0))
    37  	assert.Equal(t, true, IsPointer(p1))
    38  
    39  	// IsBasic
    40  	assert.Equal(t, false, IsBasic(p1))
    41  	assert.Equal(t, true, IsBasic(0))
    42  	assert.Equal(t, true, IsBasic("0"))
    43  
    44  	var p11 *testOp
    45  	// IsNil
    46  	assert.Equal(t, true, IsNil(nil))
    47  	assert.Equal(t, true, IsNil(p11))
    48  	assert.Equal(t, false, IsNil(p0))
    49  
    50  	// IsList
    51  	list := make([]int, 10)
    52  	assert.Equal(t, true, IsList([]string{}))
    53  	assert.Equal(t, false, IsList(false))
    54  	assert.Equal(t, false, IsList(102))
    55  	assert.Equal(t, true, IsList(list))
    56  
    57  	//Integer
    58  	assert.Equal(t, true, IsInteger(10))
    59  	assert.Equal(t, true, IsUInteger(uint(1)))
    60  	assert.Equal(t, false, IsUInteger(uintptr(1)))
    61  	assert.Equal(t, false, IsGeneralInt(uintptr(1)))
    62  
    63  	// Float
    64  	assert.Equal(t, false, IsFloat(uintptr(1)))
    65  	assert.Equal(t, false, IsFloat(int64(1)))
    66  	assert.Equal(t, false, IsFloat(uint64(1)))
    67  	assert.Equal(t, true, IsFloat(10.2))
    68  
    69  	// IsNumber
    70  	assert.Equal(t, true, IsNumber(10.2))
    71  	assert.Equal(t, true, IsNumber(uint64(1)))
    72  	assert.Equal(t, true, IsNumber(200))
    73  
    74  	// reflect Kind
    75  	assert.Equal(t, true, IsInvalid(nil))
    76  	assert.Equal(t, true, IsInvalid(TypeOf(nil)))
    77  	assert.Equal(t, false, IsInvalid(0))
    78  
    79  	assert.Equal(t, true, IsBool(true))
    80  	assert.Equal(t, true, IsBool(false))
    81  	assert.Equal(t, false, IsBool(nil))
    82  	assert.Equal(t, false, IsBool(0))
    83  
    84  	assert.Equal(t, true, IsInt(200))
    85  	assert.Equal(t, false, IsInt("2"))
    86  
    87  	assert.Equal(t, true, IsInt8(int8(10)))
    88  	assert.Equal(t, true, IsInt8(int8(10)))
    89  
    90  	assert.Equal(t, true, IsInt16(int16(10)))
    91  	assert.Equal(t, true, IsInt16(int16(10)))
    92  
    93  	assert.Equal(t, false, IsInt32(int16(10)))
    94  	assert.Equal(t, true, IsInt32(int32(10)))
    95  
    96  	assert.Equal(t, false, IsInt64(int16(10)))
    97  	assert.Equal(t, true, IsInt64(int64(21)))
    98  
    99  	assert.Equal(t, false, IsUint(uintptr(10)))
   100  	assert.Equal(t, true, IsUint(uint(21)))
   101  
   102  	assert.Equal(t, false, IsUint8(uint32(10)))
   103  	assert.Equal(t, true, IsUint8(uint8(21)))
   104  
   105  	assert.Equal(t, false, IsUint16(uint32(10)))
   106  	assert.Equal(t, true, IsUint16(uint16(21)))
   107  
   108  	assert.Equal(t, false, IsUint32(uint64(10)))
   109  	assert.Equal(t, true, IsUint32(uint32(21)))
   110  
   111  	assert.Equal(t, false, IsUint64(uint(10)))
   112  	assert.Equal(t, true, IsUint64(uint64(21)))
   113  
   114  	assert.Equal(t, false, IsUintptr(uint(10)))
   115  	assert.Equal(t, true, IsUintptr(uintptr(21)))
   116  
   117  	assert.Equal(t, false, IsUintptr(uint(10)))
   118  	assert.Equal(t, true, IsUintptr(uintptr(21)))
   119  
   120  	assert.Equal(t, false, IsFloat32(2.54))
   121  	assert.Equal(t, true, IsFloat32(float32(21)))
   122  
   123  	assert.Equal(t, true, IsFloat64(2.54))
   124  	assert.Equal(t, false, IsFloat64(float32(2.231)))
   125  
   126  	assert.Equal(t, false, IsComplex64(2.54))
   127  	assert.Equal(t, true, IsComplex64(complex64(1+1i)))
   128  
   129  	assert.Equal(t, false, IsComplex128(2.54))
   130  	assert.Equal(t, true, IsComplex128(2.231+2i))
   131  
   132  	var buf [10]string
   133  	assert.Equal(t, true, IsArray(buf))
   134  	assert.Equal(t, false, IsArray(2.231+2i))
   135  
   136  	channel := make(chan int, 1)
   137  	assert.Equal(t, true, IsChan(channel))
   138  	assert.Equal(t, false, IsChan(2.231+2i))
   139  
   140  	var fn func()
   141  	assert.Equal(t, true, IsFunc(fn))
   142  	assert.Equal(t, false, IsFunc(2.231))
   143  
   144  	assert.Equal(t, true, IsMap(map[string]any{}))
   145  	assert.Equal(t, false, IsMap(2.231))
   146  
   147  	assert.Equal(t, true, IsSlice(make([]int, 10)))
   148  	assert.Equal(t, false, IsSlice(2.231))
   149  
   150  	assert.Equal(t, true, IsString(""))
   151  	assert.Equal(t, true, IsString("9999"))
   152  	assert.Equal(t, false, IsString(nil))
   153  	assert.Equal(t, false, IsString(2.231))
   154  
   155  	assert.Equal(t, true, IsStruct(&testAssert{}))
   156  	assert.Equal(t, true, IsStruct(testAssert{}))
   157  	assert.Equal(t, false, IsStruct(map[int]any{}))
   158  	assert.Equal(t, false, IsStruct(nil))
   159  	assert.Equal(t, false, IsStruct(2.231))
   160  
   161  	assert.Equal(t, true, IsUnsafePointer(reflect.ValueOf(&testAssert{}).UnsafePointer()))
   162  	assert.Equal(t, false, IsUnsafePointer(2.231))
   163  
   164  }
   165  
   166  type testErr struct {
   167  }
   168  
   169  func (terr testErr) Error() string {
   170  	return ""
   171  }
   172  
   173  func TestIsError(t *testing.T) {
   174  	var p func() (int, error)
   175  	tp := TypeOf(p)
   176  	println(IsInterface(tp.Out(1)))
   177  	fmt.Printf("%v", NewOf(tp.Out(1)).Interface())
   178  	assert.Equal(t, true, IsError(tp.Out(1)))
   179  	assert.Equal(t, true, IsError(testErr{}))
   180  	assert.Equal(t, true, IsError(errors.New("")))
   181  }