-
-
Notifications
You must be signed in to change notification settings - Fork 491
/
copier_issue170_test.go
112 lines (93 loc) · 1.84 KB
/
copier_issue170_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
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
package copier_test
import (
"github.com/jinzhu/copier"
"reflect"
"testing"
)
type A struct {
A int
}
type B struct {
A int
b int
}
var copied = B{A: 2387483274, b: 128387134}
func newOptWithConverter() copier.Option {
return copier.Option{
Converters: []copier.TypeConverter{
{
SrcType: A{},
DstType: B{},
Fn: func(from interface{}) (interface{}, error) {
return copied, nil
},
},
},
}
}
func Test_Struct_With_Converter(t *testing.T) {
aa := A{A: 11}
bb := B{A: 10, b: 100}
err := copier.CopyWithOption(&bb, &aa, newOptWithConverter())
if err != nil || !reflect.DeepEqual(copied, bb) {
t.Fatalf("Got %v, wanted %v", bb, copied)
}
}
func Test_Map_With_Converter(t *testing.T) {
aa := map[string]*A{
"a": &A{A: 10},
}
bb := map[string]*B{
"a": &B{A: 10, b: 100},
}
err := copier.CopyWithOption(&bb, &aa, newOptWithConverter())
if err != nil {
t.Fatalf("copy with converter failed: %v", err)
}
for _, v := range bb {
wanted := &copied
if !reflect.DeepEqual(v, wanted) {
t.Fatalf("Got %v, wanted %v", v, wanted)
}
}
}
func Test_Slice_With_Converter(t *testing.T) {
aa := []*A{
&A{A: 10},
}
bb := []*B{
&B{A: 10, b: 100},
}
err := copier.CopyWithOption(&bb, &aa, newOptWithConverter())
if err != nil {
t.Fatalf("copy slice error: %v", err)
}
wanted := copied
for _, v := range bb {
temp := v
if !reflect.DeepEqual(*temp, wanted) {
t.Fatalf("Got %v, wanted %v", *temp, wanted)
}
}
}
func Test_Slice_Embedded_With_Converter(t *testing.T) {
aa := struct {
A []*A
}{
A: []*A{&A{A: 10}},
}
bb := struct {
A []*B
}{
A: []*B{&B{A: 10, b: 100}},
}
err := copier.CopyWithOption(&bb, &aa, newOptWithConverter())
wanted := struct {
A []*B
}{
A: []*B{&copied},
}
if err != nil || !reflect.DeepEqual(bb, wanted) {
t.Fatalf("Got %v, wanted %v", bb, wanted)
}
}