-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcopit_tag_test.go
More file actions
122 lines (105 loc) · 3.48 KB
/
copit_tag_test.go
File metadata and controls
122 lines (105 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package copit_test
import (
"reflect"
"testing"
"time"
"github.com/pkgng/copit"
)
type Human struct {
Name string
Birthday *time.Time
Nickname string
Role string
Age int32
FakeAge *int32
Notes []string
flags []byte
}
func (man Human) DoubleAge() int32 {
return 2 * man.Age
}
type Farmer struct {
Name string `copit:"Name"`
Birthday *time.Time
Nickname *string `copit:"Nickname"`
Age int64
FakeAge int
EmployeID int64
DoubleAge int32
SuperRule string
Notes []string
flags []byte
}
func (farmer *Farmer) Role(role string) {
farmer.SuperRule = "Super " + role
}
func checkFarmer(farmer Farmer, man Human, t *testing.T, testCase string) {
if farmer.Name != man.Name {
t.Errorf("%v: Name haven't been copied correctly.", testCase)
}
if farmer.Nickname == nil || *farmer.Nickname != man.Nickname {
t.Errorf("%v: NickName haven't been copied correctly.", testCase)
}
if farmer.Birthday == nil && man.Birthday != nil {
t.Errorf("%v: Birthday haven't been copied correctly.", testCase)
}
if farmer.Birthday != nil && man.Birthday == nil {
t.Errorf("%v: Birthday haven't been copied correctly.", testCase)
}
if farmer.Birthday != nil && man.Birthday != nil &&
!farmer.Birthday.Equal(*(man.Birthday)) {
t.Errorf("%v: Birthday haven't been copied correctly.", testCase)
}
if farmer.Age != int64(man.Age) {
t.Errorf("%v: Age haven't been copied correctly.", testCase)
}
if man.FakeAge != nil && farmer.FakeAge != int(*man.FakeAge) {
t.Errorf("%v: FakeAge haven't been copied correctly.", testCase)
}
if !reflect.DeepEqual(farmer.Notes, man.Notes) {
t.Errorf("%v: Copy from slice doen't work", testCase)
}
}
func TestCopySameStructWithPointerField2(t *testing.T) {
var fakeAge int32 = 12
var currentTime time.Time = time.Now()
man := &Human{Birthday: ¤tTime, Name: "Zhangsan", Nickname: "zhangsan", Age: 18, FakeAge: &fakeAge, Role: "Admin", Notes: []string{"hello world", "welcome"}, flags: []byte{'x'}}
newHuman := &Human{}
copit.Copy(newHuman, man)
if man.Birthday == newHuman.Birthday {
t.Errorf("TestCopySameStructWithPointerField: copy Birthday failed since they need to have different address")
}
if man.FakeAge == newHuman.FakeAge {
t.Errorf("TestCopySameStructWithPointerField: copy FakeAge failed since they need to have different address")
}
}
func checkFarmer2(farmer Farmer, man *Human, t *testing.T, testCase string) {
if man == nil {
if farmer.Name != "" || farmer.Nickname != nil || farmer.Birthday != nil || farmer.Age != 0 ||
farmer.DoubleAge != 0 || farmer.FakeAge != 0 || farmer.SuperRule != "" || farmer.Notes != nil {
t.Errorf("%v : farmer should be empty", testCase)
}
return
}
checkFarmer(farmer, *man, t, testCase)
}
func TestCopyStruct2(t *testing.T) {
var fakeAge int32 = 12
man := Human{Name: "Zhangsan", Nickname: "zhangsan", Age: 18, FakeAge: &fakeAge, Role: "Admin", Notes: []string{"hello world", "welcome"}, flags: []byte{'x'}}
farmer := Farmer{}
if err := copit.Copy(farmer, &man); err == nil {
t.Errorf("Copy to unaddressable value should get error")
}
copit.Copy(&farmer, &man)
checkFarmer(farmer, man, t, "Copy From Ptr To Ptr")
farmer2 := Farmer{}
copit.Copy(&farmer2, man)
checkFarmer(farmer2, man, t, "Copy From Struct To Ptr")
farmer3 := Farmer{}
ptrToHuman := &man
copit.Copy(&farmer3, &ptrToHuman)
checkFarmer(farmer3, man, t, "Copy From Double Ptr To Ptr")
farmer4 := &Farmer{}
copit.Copy(&farmer4, man)
checkFarmer(*farmer4, man, t, "Copy From Ptr To Double Ptr")
}