Ai
1 Star 3 Fork 0

TimAndy/routine

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
api_cloneable_test.go 1.14 KB
一键复制 编辑 原始数据 按行查看 历史
TimAndy 提交于 2025-03-12 10:33 +08:00 . Add testcases for Cloneable
package routine
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestCloneable(t *testing.T) {
//struct can not be cast to interface
var value any = personCloneable{Id: 1, Name: "Hello"}
_, ok := value.(Cloneable)
assert.False(t, ok)
//pointer can be cast to interface
var pointer any = &personCloneable{Id: 1, Name: "Hello"}
_, ok2 := pointer.(Cloneable)
assert.True(t, ok2)
//nil pointer can be cast to interface
pointer = (*personCloneable)(nil)
cloneable, ok3 := pointer.(Cloneable)
assert.True(t, ok3)
assert.True(t, cloneable != nil)
}
func TestCloneable_Clone(t *testing.T) {
//clone struct
pc := &personCloneable{Id: 1, Name: "Hello"}
assert.NotSame(t, pc, pc.Clone())
assert.Equal(t, *pc, *(pc.Clone().(*personCloneable)))
//copy pointer
pcs := make([]*personCloneable, 1)
pcs[0] = pc
pcs2 := make([]*personCloneable, 1)
copy(pcs2, pcs)
assert.Same(t, pc, pcs2[0])
//clone nil panic
assert.Panics(t, func() {
pc2 := (*personCloneable)(nil)
_ = pc2.Clone()
})
}
type personCloneable struct {
Id int
Name string
}
func (p *personCloneable) Clone() any {
return &personCloneable{Id: p.Id, Name: p.Name}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/timandy/routine.git
git@gitee.com:timandy/routine.git
timandy
routine
routine
main

搜索帮助