Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more unit test #24

Merged
merged 1 commit into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 137 additions & 0 deletions data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,140 @@ func ptrOf[T any](v T) *T {
var (
errTest = errors.New("err test")
)

type srcStruct2 struct {
S string
I int
U uint32
F float64
B bool

Method int
}

type dstStruct2 struct {
S string
I int
U uint32
F float64
B bool

MethodVal int `copy:"Method"`
}

func (d *dstStruct2) CopyMethod(v int) error {
d.MethodVal = v * 2
return nil
}

func (d *dstStruct2) EqualSrcStruct2(s *srcStruct2) bool {
return d.S == s.S && d.I == s.I && d.U == s.U && d.F == s.F && d.B == s.B && d.MethodVal == s.Method*2
}

type srcStruct1 struct {
S string
I int
U uint32
F float64
B bool

II []int
UU []uint
SS []string
V srcStruct2
VV []srcStruct2

M1 map[int]string
M2 *map[int8]int8
M3 map[int]int
M4 map[[3]int]*srcStruct2

NilP2V *int
P2V *int
V2P int
S2A []string

MatchedX string `copy:"Matched"`
UnmatchedX string `copy:"UnmatchedX"`
}

type dstStruct1 struct {
S StrT
I int
U uint32
F float64
B *bool

II []int
UU []uint
SS []*StrT
V dstStruct2
VV []dstStruct2

M1 map[int]string
M2 map[int8]int8
M3 *map[int]IntT
M4 map[[3]int]*dstStruct2

NilP2V int
P2V int
V2P *int
S2A [3]string

MatchedY string `copy:"Matched"`
UnmatchedY string `copy:"UnmatchedY"`
}

var (
srcStructA = srcStruct2{
S: "string",
I: 10,
U: 100,
F: 1.234,
B: true,

Method: 1234,
}

srcStructB = srcStruct2{
S: "string",
I: 10,
U: 100,
F: 1.234,
B: true,

Method: 4321,
}

p2v = 1234
//nolint
srcStruct = srcStruct1{
S: "string",
I: 10,
U: 10,
F: 1.234,
B: true,

II: []int{1, 2, 3},
UU: nil,
SS: []string{"string1", "string2", "", "string4", "string5"},
V: srcStructA,
VV: []srcStruct2{srcStructA, srcStructB, srcStructA, srcStructB, srcStructA},

M1: map[int]string{1: "11", 2: "22", 3: "33"},
M2: nil,
M3: map[int]int{7: 77, 8: 88, 9: 99},
M4: map[[3]int]*srcStruct2{
[3]int{1, 1, 1}: &srcStructA,
[3]int{2, 2, 2}: &srcStructB,
},

NilP2V: nil,
P2V: &p2v,
V2P: 123,
S2A: []string{"1", "2", "3", "4"},

MatchedX: "MatchedX",
UnmatchedX: "UnmatchedX",
}
)
228 changes: 41 additions & 187 deletions deepcopy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,202 +6,56 @@ import (
"github.com/stretchr/testify/assert"
)

type srcStruct2 struct {
S string
I int
I8 int8
I16 int16
I32 int32
I64 int64
U uint
U8 uint8
U16 uint16
U32 uint32
U64 uint64
F32 float32
F64 float64
B bool

Method int
}
type srcStruct1 struct {
S string
I int
I8 int8
I16 int16
I32 int32
I64 int64
U uint
U8 uint8
U16 uint16
U32 uint32
U64 uint64
F32 float32
F64 float64
B bool

II []int
UU []uint
SS []string
V srcStruct2
VV []srcStruct2

M1 map[int]string
M2 *map[int8]int8
M3 map[int]int
M4 map[[3]int]*srcStruct2

P2V *int
V2P int
S2A []string

MatchedX string `copy:"Matched"`
UnmatchedX string `copy:"UnmatchedX"`
}

type dstStruct2 struct {
S string
I int
I8 int8
I16 int16
I32 int32
I64 int64
U uint
U8 uint8
U16 uint16
U32 uint32
U64 uint64
F32 float32
F64 float64
B bool

MethodVal int
}
func Test_Copy(t *testing.T) {
src := &srcStruct
var dst dstStruct1
err := Copy(&dst, src)
assert.Nil(t, err)

func (d *dstStruct2) CopyMethod(v int) error {
d.MethodVal = v * 2
return nil
}
// Common fields
assert.Equal(t, StrT(src.S), dst.S)
assert.Equal(t, src.I, dst.I)
assert.Equal(t, src.U, dst.U)
assert.Equal(t, src.F, dst.F)
assert.Equal(t, src.B, *dst.B)

type dstStruct1 struct {
S StrT
I int
I8 int8
I16 int16
I32 int32
I64 int64
U uint
U8 uint8
U16 uint16
U32 uint32
U64 uint64
F32 float32
F64 float64
B *bool

II []int
UU []uint
SS []*StrT
V dstStruct2
VV []dstStruct2

M1 map[int]string
M2 map[int8]int8
M3 *map[int]IntT
M4 map[[3]int]*dstStruct2

P2V int
V2P *int
S2A [3]string

MatchedY string `copy:"Matched"`
UnmatchedY string `copy:"UnmatchedY"`
}
assert.True(t, dst.V.EqualSrcStruct2(&src.V))

var (
srcStructA = srcStruct2{
S: "string",
I: 10,
I8: -8,
I16: -16,
I32: -32,
I64: -64,
U: 10,
U8: 8,
U16: 16,
U32: 32,
U64: 64,
F32: 32.32,
F64: 64.64,
B: true,

Method: 1234,
// Slice/Array fields
assert.Equal(t, src.II, dst.II)
assert.Equal(t, src.UU, dst.UU)
for i := range dst.SS {
assert.Equal(t, StrT(src.SS[i]), *dst.SS[i])
}
for i := range dst.VV {
assert.True(t, dst.VV[i].EqualSrcStruct2(&src.VV[i]))
}

srcStructB = srcStruct2{
S: "string",
I: 10,
I8: -8,
I16: -16,
I32: -32,
I64: -64,
U: 10,
U8: 8,
U16: 16,
U32: 32,
U64: 64,
F32: 32.32,
F64: 64.64,
B: true,

Method: 4321,
assert.Equal(t, 0, dst.NilP2V)
assert.Equal(t, *src.P2V, dst.P2V)
assert.Equal(t, src.V2P, *dst.V2P)

// Map fields
assert.Equal(t, src.M1, dst.M1)
assert.True(t, src.M2 == nil && dst.M2 == nil)
assert.Equal(t, len(src.M3), len(*dst.M3))
for k, v := range *dst.M3 {
assert.Equal(t, IntT(src.M3[k]), v)
}
assert.Equal(t, len(src.M4), len(dst.M4))
for k, v := range dst.M4 {
assert.True(t, v.EqualSrcStruct2(src.M4[k]))
}

srcStruct = srcStruct1{
S: "string",
I: 10,
I8: -8,
I16: -16,
I32: -32,
I64: -64,
U: 10,
U8: 8,
U16: 16,
U32: 32,
U64: 64,
F32: 32.32,
F64: 64.64,
B: true,

II: []int{},
UU: nil,
SS: []string{"string1", "string2", "", "string4", "string5"},
V: srcStructA,
VV: []srcStruct2{srcStructA, srcStructB, srcStructA, srcStructB, srcStructA},

M1: map[int]string{1: "11", 2: "22", 3: "33"},
M2: nil,
M3: map[int]int{7: 77, 8: 88, 9: 99},
//nolint:gofmt
M4: map[[3]int]*srcStruct2{
[3]int{1, 1, 1}: &srcStructA,
[3]int{2, 2, 2}: &srcStructB,
},

P2V: nil,
V2P: 0,
S2A: nil,

MatchedX: "hahaha",
UnmatchedX: "hihihi",
// Slice to Array
assert.Equal(t, 3, len(dst.S2A))
for i := range dst.S2A {
assert.Equal(t, src.S2A[i], dst.S2A[i])
}
)

func Test_Copy(t *testing.T) {
var dst dstStruct1
err := Copy(&dst, srcStruct)
assert.Nil(t, err)
// TODO: need verification here
// Copy key matching
assert.Equal(t, src.MatchedX, dst.MatchedY)
assert.NotEqual(t, src.UnmatchedX, dst.UnmatchedY)
}

func Test_ClearCache(t *testing.T) {
Expand Down
Loading