github.com/songzhibin97/go-baseutils@v0.0.2-0.20240302024150-487d8ce9c082/base/bpoint/point_test.go (about)

     1  package bpoint
     2  
     3  import (
     4  	"github.com/stretchr/testify/assert"
     5  	"reflect"
     6  	"testing"
     7  )
     8  
     9  type tInterface interface {
    10  	test()
    11  }
    12  
    13  type tStruct struct {
    14  	t any
    15  }
    16  
    17  func (t tStruct) test() {
    18  }
    19  
    20  func TestToPoint(t *testing.T) {
    21  	var v int = 1
    22  	rv := ToPoint(v)
    23  	assert.Equal(t, reflect.Ptr, reflect.ValueOf(rv).Kind())
    24  	assert.Equal(t, v, reflect.ValueOf(rv).Elem().Interface())
    25  
    26  	var v2 *int = nil
    27  	rv2 := ToPoint(v2)
    28  	assert.Equal(t, reflect.Ptr, reflect.ValueOf(rv2).Kind())
    29  	if rv2 != nil {
    30  		t.Error("rv2 should be nil")
    31  	}
    32  
    33  	var v3 *int = &v
    34  	rv3 := ToPoint(v3)
    35  	assert.Equal(t, reflect.Ptr, reflect.ValueOf(rv2).Kind())
    36  	assert.Equal(t, v3, reflect.ValueOf(rv3).Elem().Interface())
    37  
    38  	var v4 tStruct = tStruct{t: 1}
    39  	rv4 := ToPoint(v4)
    40  	assert.Equal(t, reflect.Ptr, reflect.ValueOf(rv4).Kind())
    41  	assert.Equal(t, v4, reflect.ValueOf(rv4).Elem().Interface())
    42  
    43  	var v5 *tStruct = nil
    44  	rv5 := ToPoint(v5)
    45  	assert.Equal(t, reflect.Ptr, reflect.ValueOf(rv5).Kind())
    46  	if rv5 != nil {
    47  		t.Error("rv5 should be nil")
    48  	}
    49  
    50  	var v6 *tStruct = &v4
    51  	rv6 := ToPoint(v6)
    52  	assert.Equal(t, reflect.Ptr, reflect.ValueOf(rv6).Kind())
    53  	assert.Equal(t, v6, reflect.ValueOf(rv6).Elem().Interface())
    54  
    55  	var v7 tInterface = tStruct{t: 1}
    56  	rv7 := ToPoint(v7)
    57  	assert.Equal(t, reflect.Ptr, reflect.ValueOf(rv7).Kind())
    58  	assert.Equal(t, v7, reflect.ValueOf(rv7).Elem().Interface())
    59  
    60  	var v8 *tInterface = nil
    61  	rv8 := ToPoint(v8)
    62  	assert.Equal(t, reflect.Ptr, reflect.ValueOf(rv8).Kind())
    63  	if rv8 != nil {
    64  		t.Error("rv8 should be nil")
    65  	}
    66  
    67  	var v9 *tInterface = &v7
    68  	rv9 := ToPoint(v9)
    69  	assert.Equal(t, reflect.Ptr, reflect.ValueOf(rv9).Kind())
    70  	assert.Equal(t, v9, reflect.ValueOf(rv9).Elem().Interface())
    71  }