-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanimagi_basic_mapping_test.go
211 lines (173 loc) · 5.31 KB
/
animagi_basic_mapping_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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package animagi_test
import (
"github.com/barreeyentos/animagi"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
type myint int
type mystring string
type SimpleSingleDepth struct {
T_int int
T_int8 int8
T_int16 int16
T_int32 int32
T_myint myint
T_string string
T_mystring mystring
}
type TheSameSimpleSingleDepth struct {
T_int int
T_int8 int8
T_int16 int16
T_int32 int32
T_myint myint
T_string string
T_mystring mystring
T_extraInt int
T_extra string
}
type SimpleWithDepthOfTwo struct {
Description string
SameSingleDepth TheSameSimpleSingleDepth
}
type TheSameSimpleWithDepthOfTwo struct {
Description string
ExtraDescription string
SameSingleDepth TheSameSimpleSingleDepth
SameTypeDifferentName TheSameSimpleSingleDepth
}
var _ = Describe("AnimagiBasicMapping", func() {
Context("Single Depth Structs", func() {
It("Should Map Simple Structs", func() {
src := SimpleSingleDepth{1, 127, 32767, 512, 1024, "animagi", "animato"}
var dst TheSameSimpleSingleDepth
err := animagi.Transform(src, &dst)
Expect(err).NotTo(HaveOccurred())
Expect(dst).NotTo(BeEquivalentTo(src))
Expect(dst.T_extra).To(BeEmpty())
Expect(dst.T_extraInt).To(BeZero())
Expect(dst.T_int).To(Equal(src.T_int))
Expect(dst.T_int8).To(Equal(src.T_int8))
Expect(dst.T_int16).To(Equal(src.T_int16))
Expect(dst.T_int32).To(Equal(src.T_int32))
Expect(dst.T_string).To(Equal(src.T_string))
Expect(dst.T_mystring).To(Equal(src.T_mystring))
})
It("Should Map Structs with Fields of Same Kind", func() {
src := struct {
A int
B mystring
C string
}{420, "a string", "just another string"}
var dst struct {
A myint
B string
D uint8
}
err := animagi.Transform(src, &dst)
Expect(err).NotTo(HaveOccurred())
Expect(dst.A).To(BeNumerically("==", src.A))
Expect(dst.B).To(BeEquivalentTo(src.B))
Expect(dst.D).To(BeZero())
})
It("Should Map Structs with Pointers of same Type", func() {
src := struct {
Iptr *int
Sptr *string
}{new(int), new(string)}
*src.Iptr = 33
*src.Sptr = "point to me"
var dst struct {
Iptr *int
Sptr *string
extra int
}
err := animagi.Transform(src, &dst)
Expect(err).ToNot(HaveOccurred())
Expect(dst.Iptr).ToNot(BeIdenticalTo(src.Iptr))
Expect(*dst.Iptr).To(Equal(*src.Iptr))
Expect(dst.Sptr).ToNot(BeIdenticalTo(src.Sptr))
Expect(*dst.Sptr).To(Equal(*src.Sptr))
})
It("Should Map literals to pointer", func() {
src := struct {
I int
S string
}{32, "not pointy"}
var dst struct {
I *int
S *string
}
err := animagi.Transform(src, &dst)
Expect(err).ToNot(HaveOccurred())
Expect(*dst.I).To(BeNumerically("==", src.I))
Expect(*dst.S).To(Equal(src.S))
})
It("Should Map pointers to literals", func() {
src := struct {
I *int
S *string
}{new(int), new(string)}
*src.I = 31
*src.S = "pointy str"
var dst struct {
I int
S string
}
err := animagi.Transform(src, &dst)
Expect(err).ToNot(HaveOccurred())
Expect(dst.I).To(BeNumerically("==", *src.I))
Expect(dst.S).To(Equal(*src.S))
})
It("Should map pointer fields whose base kind are the same", func() {
src := struct {
MyI *myint
MyS *mystring
I myint
S *mystring
}{new(myint), new(mystring), 2, new(mystring)}
*src.MyI = 31
*src.MyS = "pointy str"
*src.S = "pointy redux"
var dst struct {
MyI *int
MyS *string
I *int
S string
}
err := animagi.Transform(src, &dst)
Expect(err).ToNot(HaveOccurred())
Expect(*dst.MyI).To(BeNumerically("==", *src.MyI))
Expect(*dst.I).To(BeNumerically("==", src.I))
Expect(*dst.MyS).To(BeEquivalentTo(*src.MyS))
Expect(dst.S).To(BeEquivalentTo(*src.S))
})
})
Context("Depth of Two Structs", func() {
It("Should Map Simple Structs", func() {
src := SimpleWithDepthOfTwo{"two", TheSameSimpleSingleDepth{1, 127, 32767, 512, 1024, "animagi", "animato", 42, "extra"}}
var dst TheSameSimpleWithDepthOfTwo
err := animagi.Transform(src, &dst)
Expect(err).NotTo(HaveOccurred())
Expect(dst).NotTo(BeEquivalentTo(src))
Expect(dst.Description).To(Equal(src.Description))
Expect(dst.ExtraDescription).To(BeEmpty())
Expect(dst.SameSingleDepth.T_extra).To(Equal(src.SameSingleDepth.T_extra))
Expect(dst.SameSingleDepth.T_extraInt).To(Equal(src.SameSingleDepth.T_extraInt))
Expect(dst.SameSingleDepth.T_int).To(Equal(src.SameSingleDepth.T_int))
Expect(dst.SameSingleDepth.T_int8).To(Equal(src.SameSingleDepth.T_int8))
Expect(dst.SameSingleDepth.T_int16).To(Equal(src.SameSingleDepth.T_int16))
Expect(dst.SameSingleDepth.T_int32).To(Equal(src.SameSingleDepth.T_int32))
Expect(dst.SameSingleDepth.T_string).To(Equal(src.SameSingleDepth.T_string))
Expect(dst.SameSingleDepth.T_mystring).To(Equal(src.SameSingleDepth.T_mystring))
Expect(dst.SameTypeDifferentName.T_extra).To(BeZero())
Expect(dst.SameTypeDifferentName.T_extraInt).To(BeZero())
Expect(dst.SameTypeDifferentName.T_int).To(BeZero())
Expect(dst.SameTypeDifferentName.T_int8).To(BeZero())
Expect(dst.SameTypeDifferentName.T_int16).To(BeZero())
Expect(dst.SameTypeDifferentName.T_int32).To(BeZero())
Expect(dst.SameTypeDifferentName.T_string).To(BeEmpty())
Expect(dst.SameTypeDifferentName.T_mystring).To(BeEmpty())
})
})
})