1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-16 18:52:27 +08:00

feat: add DeepClone

This commit is contained in:
dudaodong
2023-02-16 11:50:11 +08:00
parent 0bd675340f
commit 215b79140d
3 changed files with 275 additions and 0 deletions

View File

@@ -2,6 +2,7 @@ package convertor
import (
"fmt"
"reflect"
"testing"
"github.com/duke-git/lancet/internal"
@@ -213,3 +214,47 @@ func TestDecodeByte(t *testing.T) {
DecodeByte(byteData, &obj)
assert.Equal("abc", obj)
}
func TestDeepClone(t *testing.T) {
// assert := internal.NewAssert(t, "TestDeepClone")
type Struct struct {
Str string
Int int
Float float64
Bool bool
Nil interface{}
unexported string
}
cases := []interface{}{
true,
1,
0.1,
map[string]int{
"a": 1,
"b": 2,
},
&Struct{
Str: "test",
Int: 1,
Float: 0.1,
Bool: true,
Nil: nil,
// unexported: "can't be cloned",
},
}
for i, item := range cases {
cloned := DeepClone(item)
t.Log(cloned)
if &cloned == &item {
t.Fatalf("[TestDeepClone case #%d failed]: equal pointer", i)
}
if !reflect.DeepEqual(item, cloned) {
t.Fatalf("[TestDeepClone case #%d failed] unequal objects", i)
}
}
}