github.com/timandy/routine@v1.1.4-0.20240507073150-e4a3e1fe2ba5/api_cloneable_test.go (about) 1 package routine 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/assert" 7 ) 8 9 func TestCloneable(t *testing.T) { 10 //struct can not be cast to interface 11 var value interface{} = personCloneable{Id: 1, Name: "Hello"} 12 _, ok := value.(Cloneable) 13 assert.False(t, ok) 14 //pointer can be cast to interface 15 var pointer interface{} = &personCloneable{Id: 1, Name: "Hello"} 16 _, ok2 := pointer.(Cloneable) 17 assert.True(t, ok2) 18 } 19 20 func TestCloneable_Clone(t *testing.T) { 21 //clone struct 22 pc := &personCloneable{Id: 1, Name: "Hello"} 23 assert.NotSame(t, pc, pc.Clone()) 24 assert.Equal(t, *pc, *(pc.Clone().(*personCloneable))) 25 //copy pointer 26 pcs := make([]*personCloneable, 1) 27 pcs[0] = pc 28 pcs2 := make([]*personCloneable, 1) 29 copy(pcs2, pcs) 30 assert.Same(t, pc, pcs2[0]) 31 } 32 33 type personCloneable struct { 34 Id int 35 Name string 36 } 37 38 func (p *personCloneable) Clone() any { 39 return &personCloneable{Id: p.Id, Name: p.Name} 40 }