forked from jinzhu/copier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtype_converter_test.go
77 lines (66 loc) · 1.57 KB
/
type_converter_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
package copier_test
import (
"github.com/jinzhu/copier"
"testing"
"time"
)
func TestConvertersTime(t *testing.T) {
type SrcStruct struct {
Field1 time.Time
Field2 *time.Time
Field3 time.Time
Field4 *time.Time
}
type DestStruct struct {
Field1 int64
Field2 int64
Field3 *int64
Field4 *int64
}
t1 := time.Now()
t2 := t1.Add(time.Minute)
t3 := t2.Add(time.Minute)
t4 := t3.Add(time.Minute)
src := SrcStruct{
Field1: t1,
Field2: &t2,
Field3: t3,
Field4: &t4,
}
var dst DestStruct
err := copier.Copy(&dst, &src)
if err != nil {
t.Error("copyError", err)
return
}
if v := src.Field1.UnixMilli(); v != dst.Field1 {
t.Fatalf("got %q, wanted %q", v, dst.Field1)
}
if v := src.Field2.UnixMilli(); v != dst.Field2 {
t.Fatalf("got %q, wanted %q", v, dst.Field2)
}
if v := src.Field3.UnixMilli(); v != *dst.Field3 {
t.Fatalf("got %q, wanted %q", v, *dst.Field3)
}
if v := src.Field4.UnixMilli(); v != *dst.Field4 {
t.Fatalf("got %q, wanted %q", v, *dst.Field4)
}
newSrc := SrcStruct{}
err = copier.Copy(&newSrc, &dst)
if err != nil {
t.Error("copyError", err)
return
}
if v := dst.Field1; v != newSrc.Field1.UnixMilli() {
t.Fatalf("got %q, wanted %q", v, newSrc.Field1.UnixMilli())
}
if v := dst.Field2; v != newSrc.Field2.UnixMilli() {
t.Fatalf("got %q, wanted %q", v, newSrc.Field2)
}
if v := dst.Field3; *v != newSrc.Field3.UnixMilli() {
t.Fatalf("got %q, wanted %q", *v, newSrc.Field3.UnixMilli())
}
if v := dst.Field4; *v != newSrc.Field4.UnixMilli() {
t.Fatalf("got %q, wanted %q", *v, newSrc.Field4.UnixMilli())
}
}