-
Notifications
You must be signed in to change notification settings - Fork 0
/
acceptence_test.go
200 lines (169 loc) · 3.82 KB
/
acceptence_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
package conversion
import (
"fmt"
"reflect"
"testing"
"github.com/stretchr/testify/suite"
)
type Struct1 struct {
Field1 string
Field2 int
}
type Struct2 struct {
Field1 string
Field2 int
}
type Struct3 struct {
Field1 *string
Field2 *int
}
func testHelper[TSource, TDestination any](suite *AcceptanceTests, valid TSource, expected TDestination) {
var err error
var s TSource
var d TDestination
var tDestination reflect.Type = reflect.TypeOf(d)
var tSource reflect.Type = reflect.TypeOf(s)
suite.Run(
fmt.Sprintf("%v", tDestination), func() {
suite.Run(
fmt.Sprintf("%v\u2192%v", tSource, tDestination), func() {
source := valid
var destination TDestination
cfg := NewConfiguration()
mapper := cfg.BuildMapper()
err = mapper.Map(source, &destination)
suite.NoError(err)
if err == nil {
suite.Equal(expected, destination)
}
},
)
suite.Run(
fmt.Sprintf("%v\u2192*%v", tSource, tDestination), func() {
source := valid
var destination *TDestination
cfg := NewConfiguration()
mapper := cfg.BuildMapper()
err = mapper.Map(source, &destination)
suite.NoError(err)
if err == nil {
suite.Equal(expected, *destination)
}
},
)
suite.Run(
fmt.Sprintf("*%v\u2192*%v", tSource, tDestination), func() {
value := valid
source := &value
var destination *TDestination
cfg := NewConfiguration()
mapper := cfg.BuildMapper()
err = mapper.Map(source, &destination)
suite.NoError(err)
if err == nil {
suite.Equal(expected, *destination)
}
},
)
suite.Run(
fmt.Sprintf("*%v\u2192%v", tSource, tDestination), func() {
value := valid
source := &value
var destination TDestination
cfg := NewConfiguration()
mapper := cfg.BuildMapper()
err = mapper.Map(source, &destination)
suite.NoError(err)
if err == nil {
suite.Equal(expected, destination)
}
},
)
},
)
}
type AcceptanceTests struct {
suite.Suite
}
func (suite *AcceptanceTests) SetupTest() {
fmt.Printf("SetupTest\n")
}
func (suite *AcceptanceTests) TearDownTest() {
fmt.Printf("TearDownTest\n")
}
func TestAcceptanceTests(t *testing.T) {
suite.Run(t, new(AcceptanceTests))
}
func (suite *AcceptanceTests) TestMap() {
suite.Run(
"int", func() {
testHelper(suite, 123, 123)
testHelper(suite, 123, "123")
testHelper(suite, 123, 123.0)
},
)
suite.Run(
"string", func() {
testHelper(suite, "123", "123")
},
)
suite.Run(
"bool", func() {
testHelper(suite, true, true)
},
)
suite.Run(
"Struct", func() {
s := "123"
i := 123
s1 := Struct1{"123", 123}
s2 := Struct2{"123", 123}
s3 := Struct3{Field1: &s, Field2: &i}
testHelper(suite, s1, s1)
testHelper(suite, s1, s2)
testHelper(suite, s1, s3)
},
)
}
func (suite *AcceptanceTests) TestMap_1() {
var err error
source := "123"
var destination *string
cfg := NewConfiguration()
mapper := cfg.BuildMapper()
err = mapper.Map(source, &destination)
suite.Nil(err)
suite.Equal("123", *destination)
}
func (suite *AcceptanceTests) TestMap_2() {
var err error
source := "123"
var destination string
cfg := NewConfiguration()
mapper := cfg.BuildMapper()
err = mapper.Map(source, &destination)
suite.Nil(err)
suite.Equal("123", destination)
}
func (suite *AcceptanceTests) TestMap_3() {
var err error
value := "123"
source := &value
var destination *string
cfg := NewConfiguration()
mapper := cfg.BuildMapper()
err = mapper.Map(source, &destination)
suite.Nil(err)
suite.Equal("123", *destination)
}
func (suite *AcceptanceTests) TestMap_4() {
var err error
value := "123"
source := &value
var destination string
cfg := NewConfiguration()
mapper := cfg.BuildMapper()
err = mapper.Map(source, &destination)
suite.Nil(err)
suite.Equal("123", destination)
}