-
Notifications
You must be signed in to change notification settings - Fork 0
/
copy_test.go
80 lines (70 loc) · 1.51 KB
/
copy_test.go
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
package gocopy
import (
"testing"
)
var _src = Config{
ID: 1,
Hosts: []string{"1,2,3"},
AccessKey: "key1",
SecretKey: "key2",
Extra: map[string]*Dog{"1": {Name: "dog1"}, "2": {Name: "dog2"}},
Pets: []*Dog{{Name: "dog3"}, {Name: "dog4"}},
}
type Config struct {
ID int
Hosts []string
AccessKey string
SecretKey string
Extra map[string]*Dog
Pets []*Dog
}
type Dog struct {
Name string
}
func (c Config) Equal(v Config) bool {
if c.ID != v.ID || c.AccessKey != v.AccessKey || c.SecretKey != v.SecretKey ||
len(c.Hosts) != len(v.Hosts) || len(c.Extra) != len(v.Extra) {
return false
}
for i := range c.Hosts {
if c.Hosts[i] != v.Hosts[i] {
return false
}
}
for key, value := range c.Extra {
if v.Extra[key] != value {
return false
}
}
return true
}
func TestNewFrom(t *testing.T) {
value := NewFrom(_src)
dst, ok := value.(Config)
if !ok {
t.Error("NewFrom copy err")
}
if _src.Equal(dst) {
t.Error("NewFrom copy err")
}
_src.Hosts = append(_src.Hosts, "", "", "", "", "")
if len(_src.Hosts) == len(dst.Hosts) || _src.Pets[0] == dst.Pets[0] || _src.Extra["1"] == dst.Extra["1"] {
t.Error("NewFrom copy err")
}
}
func TestUpdate(t *testing.T) {
dst := &Config{}
if err := Update(_src, dst); err != nil {
t.Errorf("Update err:%+v", err)
}
if !_src.Equal(*dst) {
t.Error("Update err")
}
dst = &Config{}
if err := Update(&_src, dst); err != nil {
t.Errorf("Update err:%+v", err)
}
if !_src.Equal(*dst) {
t.Error("Update err")
}
}