github.com/jxskiss/gopkg/v2@v2.14.9-0.20240514120614-899f3e7952b4/unsafe/reflectx/equality_test.go (about)

     1  package reflectx
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  )
     8  
     9  type testtype1 struct {
    10  	A string
    11  	B int64
    12  }
    13  
    14  type testtype2 struct {
    15  	A string
    16  	B int64
    17  }
    18  
    19  type testtype3 struct {
    20  	A string
    21  	B int8
    22  }
    23  
    24  type recurtype1 struct {
    25  	A    string
    26  	b    int32
    27  	self *recurtype1
    28  }
    29  
    30  type recurtype2 struct {
    31  	A    string
    32  	b    int32
    33  	self *recurtype2
    34  }
    35  
    36  type recurtype3 struct {
    37  	A *testtype1
    38  	B *recurtype3
    39  }
    40  
    41  type recurtype4 struct {
    42  	A *testtype2
    43  	B *recurtype3
    44  }
    45  
    46  func TestIsIdenticalType(t *testing.T) {
    47  	equal, msg := IsIdenticalType(&testtype1{}, &testtype2{})
    48  	t.Log(msg)
    49  	assert.True(t, equal)
    50  
    51  	equal, msg = IsIdenticalType(&testtype1{}, &testtype3{})
    52  	t.Log(msg)
    53  	assert.False(t, equal)
    54  
    55  	equal, msg = IsIdenticalType(&testtype2{}, &testtype3{})
    56  	t.Log(msg)
    57  	assert.False(t, equal)
    58  }
    59  
    60  func TestIsIdenticalType_Recursive(t *testing.T) {
    61  	equal, msg := IsIdenticalType(&recurtype1{}, &recurtype2{})
    62  	t.Log(msg)
    63  	assert.True(t, equal)
    64  
    65  	equal, msg = IsIdenticalType(&recurtype3{}, &recurtype4{})
    66  	t.Log(msg)
    67  	assert.True(t, equal)
    68  }
    69  
    70  type ThriftType1 struct {
    71  	EntityID *int64  `thrift:"Entity,1"`
    72  	HtMLLink *string `thrift:"HtmlLink,2"`
    73  }
    74  
    75  type ThriftType2 struct {
    76  	EntityId *int64  `thrift:"Entity,1"`
    77  	HtmlLink *string `thrift:"HtmlLink,2"`
    78  }
    79  
    80  type ThriftType3 struct {
    81  	String_         string                  `frugal:"1,default,string" json:"String"`
    82  	ListSimple      []*ThriftType1          `frugal:"2,default,list<Simple>" json:"ListSimple"`
    83  	Double          float64                 `frugal:"3,default,double" json:"Double"`
    84  	Byte            int8                    `frugal:"14,default,byte" json:"Byte"`
    85  	MapStringSimple map[string]*ThriftType1 `frugal:"15,default,map<string:Simple>" json:"MapStringSimple"`
    86  }
    87  
    88  type ThriftType4 struct {
    89  	String4          string                  `frugal:"1,default,string" json:"String"`
    90  	ListSimple4      []*ThriftType2          `frugal:"2,default,list<Simple>" json:"ListSimple"`
    91  	Double4          float64                 `frugal:"3,default,double" json:"Double"`
    92  	Byte4            int8                    `frugal:"14,default,byte" json:"Byte"`
    93  	MapStringSimple4 map[string]*ThriftType2 `frugal:"15,default,map<string:Simple>" json:"MapStringSimple"`
    94  }
    95  
    96  func TestIsIdenticalThriftType(t *testing.T) {
    97  	equal, msg := IsIdenticalThriftType(&ThriftType1{}, &ThriftType2{})
    98  	t.Log(msg)
    99  	assert.True(t, equal)
   100  
   101  	equal, msg = IsIdenticalThriftType(&ThriftType3{}, &ThriftType4{})
   102  	t.Log(msg)
   103  	assert.True(t, equal)
   104  }