-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvertibility_test.go
284 lines (263 loc) · 8.11 KB
/
convertibility_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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package gospec
import (
"errors"
"fmt"
"testing"
)
// func (s *Spec) Conversion(v, t string) bool
func TestConversion01(t *testing.T) {
type Info struct {
x string
T string
couldConvert bool
}
newSpec := func(code string) (s *Spec, e error) {
defer func() {
if r := recover(); r != nil {
e = errors.New(fmt.Sprintf("%s", r))
}
}()
s = NewSpec(code)
return
}
/* Converting a constant yields a typed constant as result.
uint(iota) // iota value of type uint
float32(2.718281828) // 2.718281828 of type float32
complex128(1) // 1.0 + 0.0i of type complex128
float32(0.49999999) // 0.5 of type float32
float64(-1e-1000) // 0.0 of type float64
string('x') // "x" of type string
string(0x266c) // "♬" of type string
MyString("foo" + "bar") // "foobar" of type MyString
string([]byte{'a'}) // not a constant: []byte{'a'} is not a constant
(*int)(nil) // not a constant: nil is not a constant, *int is not a boolean, numeric, or string type
int(1.2) // illegal: 1.2 cannot be represented as an int
string(65.0) // illegal: 65.0 is not an integer constant
*/
infos := []Info{
{`iota`, `uint`, true},
{`2.718281828`, `float32`, true},
{`1`, `complex128`, true},
{`0.49999999`, `float32`, true},
{`-1e-1000`, `float64`, true},
{`'x'`, `string`, true},
{`0x266c`, `string`, true},
{`"foo" + "bar"`, `MyString; type MyString string`, true},
{`[]byte{'a'}`, `string`, false},
{`nil`, `(*int)`, false},
{`1.2`, `int`, false},
{`65.0`, `string`, false},
}
for i, v := range infos {
code := fmt.Sprintf("type T %s; const x = %s", v.T, v.x)
s, err := newSpec(code)
if i >= 0 && i <= 7 {
if err == nil && s.Conversion("x", "T") && v.couldConvert {
} else {
t.Errorf("test failed")
}
} else if i >= 8 && i <= 9 {
if err == nil {
t.Errorf("test failed")
}
} else {
if err == nil && !s.Conversion("x", "T") && !v.couldConvert {
} else {
t.Errorf("test failed")
}
}
}
}
func TestConversion02(t *testing.T) {
// A non-constant value x can be converted to type T in any of these cases:
//fmt.Println("A non-constant value x can be converted to type T in any of these cases:")
var s *Spec
// 1. x is assignable to T.
s = NewSpec(`var x = func(){}; type T func()`)
if !IsConstObject(s.MustGetValidTypeObject("x")) {
if s.Assignment("x", "T") &&
s.Conversion("x", "T") {
//fmt.Println("1. x is assignable to T.")
} else {
t.Errorf("test failed")
}
} else {
t.Errorf("test failed")
}
// 2. ignoring struct tags (see below), x's type and T have identical underlying types.
s = NewSpec(`
type T struct {
Name string
Address *struct {
Street string
City string
}
}
var x struct {
Name string ` + "`" + `json:"name"` + "`" + `
Address *struct {
Street string ` + "`" + `json:"street"` + "`" + `
City string ` + "`" + `json:"city"` + "`" + `
} ` + "`" + `json:"address"` + "`" + `
}
`)
if !IsConstObject(s.MustGetValidTypeObject("x")) {
if IdenticalIgnoreTags(s.MustGetValidType("x").Underlying(), s.MustGetValidType("T").Underlying()) &&
s.Conversion("x", "T") {
//fmt.Println("2. ignoring struct tags (see below), x's type and T have identical underlying types.")
} else {
t.Errorf("test failed")
}
} else {
t.Errorf("test failed")
}
// 3. ignoring struct tags (see below), x's type and T are pointer types that are not defined types,
// and their pointer base types have identical underlying types.
s = NewSpec(`
type Person struct {
Name string
Address *struct {
Street string
City string
}
}
var data *struct {
Name string ` + "`" + `json:"name"` + "`" + `
Address *struct {
Street string ` + "`" + `json:"street"` + "`" + `
City string ` + "`" + `json:"city"` + "`" + `
} ` + "`" + `json:"address"` + "`" + `
}
type T = *Person
var person = (*Person)(data)
`)
xt := s.MustGetValidType("data")
tt := s.MustGetValidType("T")
xp, xIsPointer := ToPointer(xt)
tp, tIsPointer := ToPointer(tt)
if !IsConstObject(s.MustGetValidTypeObject("data")) {
if xIsPointer && tIsPointer &&
!IsDefinedType(xt) && !IsDefinedType(tt) &&
IdenticalIgnoreTags(xp.Elem().Underlying(), tp.Elem().Underlying()) &&
s.Conversion("data", "T") {
//fmt.Println("3. ignoring struct tags (see below), x's type and T are pointer types that are not defined types, and their pointer base types have identical underlying types.")
} else {
t.Errorf("test failed")
}
} else {
t.Errorf("test failed")
}
// 4. x's type and T are both integer or floating point types.
s = NewSpec(`
type T uint
var x = 1
type U float32
var y = 1.0
`)
var bothIntegerOK, bothFloatOK bool
if !IsConstObject(s.MustGetValidTypeObject("x")) {
if IsInteger(s.MustGetValidType("x")) && IsInteger(s.MustGetValidType("T")) &&
s.Conversion("x", "T") {
bothIntegerOK = true
}
}
if !IsConstObject(s.MustGetValidTypeObject("y")) {
if IsFloat(s.MustGetValidType("y")) && IsFloat(s.MustGetValidType("U")) &&
s.Conversion("y", "U") {
bothFloatOK = true
}
}
if bothIntegerOK && bothFloatOK {
//fmt.Println("4. x's type and T are both integer or floating point types.")
} else {
t.Errorf("test failed")
}
// 5. x's type and T are both complex types.
s = NewSpec(`
type T complex64
var x = 1+2i
`)
if !IsConstObject(s.MustGetValidTypeObject("x")) {
if IsComplex(s.MustGetValidType("x")) && IsComplex(s.MustGetValidType("T")) &&
s.Conversion("x", "T") {
//fmt.Println("5. x's type and T are both complex types.")
} else {
t.Errorf("test failed")
}
} else {
t.Errorf("test failed")
}
// 6. x is an integer or a slice of bytes or runes and T is a string type.
s = NewSpec(`
type T string
var x = 1
var y = []byte{}
var z = []rune{}
`)
var fromIntegerOK, fromByteSliceOK, fromRuneSliceOK bool
if !IsConstObject(s.MustGetValidTypeObject("x")) {
if IsInteger(s.MustGetValidType("x")) && IsString(s.MustGetValidType("T")) &&
s.Conversion("x", "T") {
fromIntegerOK = true
}
}
if !IsConstObject(s.MustGetValidTypeObject("y")) {
if ys, yIsSlice := ToSlice(s.MustGetValidType("y")); yIsSlice && IsByte(ys.Elem()) && IsString(s.MustGetValidType("T")) &&
s.Conversion("y", "T") {
fromByteSliceOK = true
}
}
if !IsConstObject(s.MustGetValidTypeObject("z")) {
if zs, zIsSlice := ToSlice(s.MustGetValidType("z")); zIsSlice && IsRune(zs.Elem()) && IsString(s.MustGetValidType("T")) &&
s.Conversion("z", "T") {
fromRuneSliceOK = true
}
}
if fromIntegerOK && fromByteSliceOK && fromRuneSliceOK {
//fmt.Println("6. x is an integer or a slice of bytes or runes and T is a string type.")
} else {
t.Errorf("test failed")
}
// 7. x is a string and T is a slice of bytes or runes.
s = NewSpec(`
var x string = "lala"
type T []byte
type U []rune
`)
var toByteSliceOK, toRuneSliceOK bool
if !IsConstObject(s.MustGetValidTypeObject("x")) {
if ts, tIsSlice := ToSlice(s.MustGetValidType("T")); tIsSlice && IsByte(ts.Elem()) &&
s.Conversion("x", "T") {
toByteSliceOK = true
}
}
if !IsConstObject(s.MustGetValidTypeObject("x")) {
if ts, tIsSlice := ToSlice(s.MustGetValidType("U")); tIsSlice && IsRune(ts.Elem()) &&
s.Conversion("x", "U") {
toRuneSliceOK = true
}
}
if toByteSliceOK && toRuneSliceOK {
//fmt.Println("7. x is a string and T is a slice of bytes or runes.")
} else {
t.Errorf("test failed")
}
/* the output is:
A non-constant value x can be converted to type T in any of these cases:
1. x is assignable to T.
2. ignoring struct tags (see below), x's type and T have identical underlying types.
3. ignoring struct tags (see below), x's type and T are pointer types that are not defined types, and their pointer base types have identical underlying types.
4. x's type and T are both integer or floating point types.
5. x's type and T are both complex types.
6. x is an integer or a slice of bytes or runes and T is a string type.
7. x is a string and T is a slice of bytes or runes.
*/
}
// func Conversion(code, v, t string) bool
func TestConversion03(t *testing.T) {
code := "type T uint; const x = iota"
if Conversion(code, "x", "T") {
} else {
t.Errorf("test failed")
}
}