-
Notifications
You must be signed in to change notification settings - Fork 0
/
conversion_test.go
214 lines (179 loc) · 9.63 KB
/
conversion_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
212
213
214
package core
import (
"github.com/procyon-projects/goo"
"github.com/stretchr/testify/assert"
"testing"
)
type testStruct struct {
}
func testConverter(t *testing.T, converter TypeConverter, sourceType goo.Type, targetType goo.Type, value interface{}, expected interface{}) {
assert.True(t, converter.Support(sourceType, targetType))
assert.False(t, converter.Support(goo.GetType(testStruct{}), targetType))
result, err := converter.Convert(value, sourceType, targetType)
assert.Nil(t, err)
assert.Equal(t, expected, result)
assert.Equal(t, targetType.GetGoType(), goo.GetType(result).GetGoType())
result, err = converter.Convert(value, goo.GetType(testStruct{}), targetType)
assert.NotNil(t, err)
assert.Equal(t, "unsupported type", err.Error())
assert.Nil(t, result)
}
func testDefaultTypeConverterService(t *testing.T, converter TypeConverterService, sourceType goo.Type, targetType goo.Type, value interface{}, expected interface{}) {
assert.True(t, converter.CanConvert(sourceType, targetType))
result, err := converter.Convert(value, sourceType, targetType)
assert.Nil(t, err)
assert.Equal(t, expected, result)
assert.Equal(t, targetType.GetGoType(), goo.GetType(result).GetGoType())
}
func TestStringToNumberConverter(t *testing.T) {
converter := NewStringToNumberConverter()
strValue := "41"
sourceType := goo.GetType(strValue)
// target type : int
testConverter(t, converter, sourceType, goo.GetType(0), strValue, 41)
// target type : int8
testConverter(t, converter, sourceType, goo.GetType(int8(0)), strValue, int8(41))
// target type : int16
testConverter(t, converter, sourceType, goo.GetType(int16(0)), strValue, int16(41))
// target type : int32
testConverter(t, converter, sourceType, goo.GetType(int32(0)), strValue, int32(41))
// target type : int64
testConverter(t, converter, sourceType, goo.GetType(int64(0)), strValue, int64(41))
// target type : uint
testConverter(t, converter, sourceType, goo.GetType(uint(0)), strValue, uint(41))
// target type : uint8
testConverter(t, converter, sourceType, goo.GetType(uint8(0)), strValue, uint8(41))
// target type : uint16
testConverter(t, converter, sourceType, goo.GetType(uint16(0)), strValue, uint16(41))
// target type : uint32
testConverter(t, converter, sourceType, goo.GetType(uint32(0)), strValue, uint32(41))
// target type : uint64
testConverter(t, converter, sourceType, goo.GetType(uint64(0)), strValue, uint64(41))
// target type : float32
strValue = "41.5"
testConverter(t, converter, sourceType, goo.GetType(float32(0)), strValue, float32(41.5))
// target type : float64
testConverter(t, converter, sourceType, goo.GetType(float64(0)), strValue, float64(41.5))
}
func TestNumberToStringConverter(t *testing.T) {
converter := NewNumberToStringConverter()
targetType := goo.GetType("")
expectedValue := "41"
// source type : int, target type : string
testConverter(t, converter, goo.GetType(0), targetType, 41, expectedValue)
// source type : int8, target type : string
testConverter(t, converter, goo.GetType(0), targetType, int8(41), expectedValue)
// source type : int16, target type : string
testConverter(t, converter, goo.GetType(0), targetType, int16(41), expectedValue)
// source type : int32, target type : string
testConverter(t, converter, goo.GetType(0), targetType, int32(41), expectedValue)
// source type : int64, target type : string
testConverter(t, converter, goo.GetType(0), targetType, int64(41), expectedValue)
// source type : uint, target type : string
testConverter(t, converter, goo.GetType(uint(0)), targetType, uint(41), expectedValue)
// source type : uint8, target type : string
testConverter(t, converter, goo.GetType(uint(0)), targetType, uint8(41), expectedValue)
// source type : uint16, target type : string
testConverter(t, converter, goo.GetType(uint(0)), targetType, uint16(41), expectedValue)
// source type : uint32, target type : string
testConverter(t, converter, goo.GetType(uint(0)), targetType, uint32(41), expectedValue)
// source type : uint64, target type : string
testConverter(t, converter, goo.GetType(uint(0)), targetType, uint64(41), expectedValue)
// target type : float32
testConverter(t, converter, goo.GetType(float32(0)), targetType, float32(41.5), "41.500000")
// target type : float64
testConverter(t, converter, goo.GetType(float64(0)), targetType, float64(41.5), "41.500000")
}
func TestStringToBooleanConverter(t *testing.T) {
converter := NewStringToBooleanConverter()
sourceType := goo.GetType("true")
targetType := goo.GetType(true)
testConverter(t, converter, sourceType, targetType, "true", true)
testConverter(t, converter, sourceType, targetType, "false", false)
}
func TestBooleanToStringConverter(t *testing.T) {
converter := NewBooleanToStringConverter()
sourceType := goo.GetType(true)
targetType := goo.GetType("true")
testConverter(t, converter, sourceType, targetType, true, "true")
testConverter(t, converter, sourceType, targetType, false, "false")
}
func TestDefaultTypeConverterService_RegisterConverterWithNil(t *testing.T) {
converterService := NewDefaultTypeConverterService()
assert.Panics(t, func() {
converterService.RegisterConverter(nil)
})
}
func TestDefaultTypeConverterService_StringToNumberConverter(t *testing.T) {
converterService := NewDefaultTypeConverterService()
strValue := "41"
sourceType := goo.GetType(strValue)
// target type : int
testDefaultTypeConverterService(t, converterService, sourceType, goo.GetType(0), strValue, 41)
// target type : int8
testDefaultTypeConverterService(t, converterService, sourceType, goo.GetType(int8(0)), strValue, int8(41))
// target type : int16
testDefaultTypeConverterService(t, converterService, sourceType, goo.GetType(int16(0)), strValue, int16(41))
// target type : int32
testDefaultTypeConverterService(t, converterService, sourceType, goo.GetType(int32(0)), strValue, int32(41))
// target type : int64
testDefaultTypeConverterService(t, converterService, sourceType, goo.GetType(int64(0)), strValue, int64(41))
// target type : uint
testDefaultTypeConverterService(t, converterService, sourceType, goo.GetType(uint(0)), strValue, uint(41))
// target type : uint8
testDefaultTypeConverterService(t, converterService, sourceType, goo.GetType(uint8(0)), strValue, uint8(41))
// target type : uint16
testDefaultTypeConverterService(t, converterService, sourceType, goo.GetType(uint16(0)), strValue, uint16(41))
// target type : uint32
testDefaultTypeConverterService(t, converterService, sourceType, goo.GetType(uint32(0)), strValue, uint32(41))
// target type : uint64
testDefaultTypeConverterService(t, converterService, sourceType, goo.GetType(uint64(0)), strValue, uint64(41))
// target type : float32
strValue = "41.5"
testDefaultTypeConverterService(t, converterService, sourceType, goo.GetType(float32(0)), strValue, float32(41.5))
// target type : float64
testDefaultTypeConverterService(t, converterService, sourceType, goo.GetType(float64(0)), strValue, float64(41.5))
}
func TestDefaultTypeConverterService_NumberToStringConverter(t *testing.T) {
converterService := NewDefaultTypeConverterService()
targetType := goo.GetType("")
expectedValue := "41"
// source type : int, target type : string
testDefaultTypeConverterService(t, converterService, goo.GetType(0), targetType, 41, expectedValue)
// source type : int8, target type : string
testDefaultTypeConverterService(t, converterService, goo.GetType(0), targetType, int8(41), expectedValue)
// source type : int16, target type : string
testDefaultTypeConverterService(t, converterService, goo.GetType(0), targetType, int16(41), expectedValue)
// source type : int32, target type : string
testDefaultTypeConverterService(t, converterService, goo.GetType(0), targetType, int32(41), expectedValue)
// source type : int64, target type : string
testDefaultTypeConverterService(t, converterService, goo.GetType(0), targetType, int64(41), expectedValue)
// source type : uint, target type : string
testDefaultTypeConverterService(t, converterService, goo.GetType(uint(0)), targetType, uint(41), expectedValue)
// source type : uint8, target type : string
testDefaultTypeConverterService(t, converterService, goo.GetType(uint(0)), targetType, uint8(41), expectedValue)
// source type : uint16, target type : string
testDefaultTypeConverterService(t, converterService, goo.GetType(uint(0)), targetType, uint16(41), expectedValue)
// source type : uint32, target type : string
testDefaultTypeConverterService(t, converterService, goo.GetType(uint(0)), targetType, uint32(41), expectedValue)
// source type : uint64, target type : string
testDefaultTypeConverterService(t, converterService, goo.GetType(uint(0)), targetType, uint64(41), expectedValue)
// target type : float32
testDefaultTypeConverterService(t, converterService, goo.GetType(float32(0)), targetType, float32(41.5), "41.500000")
// target type : float64
testDefaultTypeConverterService(t, converterService, goo.GetType(float64(0)), targetType, float64(41.5), "41.500000")
}
func TestDefaultTypeConverterService_StringToBooleanConverter(t *testing.T) {
converterService := NewDefaultTypeConverterService()
sourceType := goo.GetType("true")
targetType := goo.GetType(true)
testDefaultTypeConverterService(t, converterService, sourceType, targetType, "true", true)
testDefaultTypeConverterService(t, converterService, sourceType, targetType, "false", false)
}
func TestDefaultTypeConverterService_BooleanToStringConverter(t *testing.T) {
converterService := NewDefaultTypeConverterService()
sourceType := goo.GetType(true)
targetType := goo.GetType("true")
testDefaultTypeConverterService(t, converterService, sourceType, targetType, true, "true")
testDefaultTypeConverterService(t, converterService, sourceType, targetType, false, "false")
}