-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathutils_test.go
More file actions
272 lines (227 loc) · 8.26 KB
/
utils_test.go
File metadata and controls
272 lines (227 loc) · 8.26 KB
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
package qjs_test
import (
"reflect"
"testing"
"unsafe"
"github.com/fastschema/qjs"
"github.com/stretchr/testify/assert"
)
func TestIsConvertibleToJs(t *testing.T) {
t.Run("BasicTypes", func(t *testing.T) {
basicTypes := []struct {
name string
value any
expected bool
}{
{"int", 42, true},
{"string", "hello", true},
{"bool", true, true},
{"float64", 3.14, true},
{"[]int", []int{1, 2, 3}, true},
{"[]unsafe.Pointer", []unsafe.Pointer{}, false},
{"map[string]int", map[string]int{"key": 1}, true},
{"map[unsafe.Pointer]string", map[unsafe.Pointer]string{}, false},
{"map[string]unsafe.Pointer", map[string]unsafe.Pointer{}, false},
{"chan int", make(chan int), true},
}
for _, tt := range basicTypes {
t.Run(tt.name, func(t *testing.T) {
err := qjs.IsConvertibleToJs(reflect.TypeOf(tt.value), make(map[reflect.Type]bool), "test")
if tt.expected {
assert.NoError(t, err, "Expected %s to be convertible", tt.name)
} else {
assert.Error(t, err, "Expected %s to not be convertible", tt.name)
}
})
}
})
t.Run("UnsupportedTypes", func(t *testing.T) {
t.Run("unsafe_pointer", func(t *testing.T) {
unsafePtr := unsafe.Pointer(&[]byte{1, 2, 3}[0])
err := qjs.IsConvertibleToJs(reflect.TypeOf(unsafePtr), make(map[reflect.Type]bool), "test")
assert.Error(t, err, "unsafe.Pointer should not be convertible")
assert.Contains(t, err.Error(), "unsafe.Pointer", "Error should mention unsafe.Pointer")
})
})
t.Run("StructWithFields", func(t *testing.T) {
t.Run("ExportedFields", func(t *testing.T) {
type TestStruct struct {
PublicField string
AnotherField int
}
err := qjs.IsConvertibleToJs(reflect.TypeOf(TestStruct{}), make(map[reflect.Type]bool), "test")
assert.NoError(t, err, "Struct with exported fields should be convertible")
})
t.Run("UnexportedFieldsIgnored", func(t *testing.T) {
type TestStruct struct {
PublicField string
privateField string
}
err := qjs.IsConvertibleToJs(reflect.TypeOf(TestStruct{}), make(map[reflect.Type]bool), "test")
assert.NoError(t, err, "Struct with unexported fields should be convertible (unexported fields ignored)")
})
t.Run("MixedFieldsWithUnsupportedPrivate", func(t *testing.T) {
type TestStruct struct {
PublicField string
privateField unsafe.Pointer
}
err := qjs.IsConvertibleToJs(reflect.TypeOf(TestStruct{}), make(map[reflect.Type]bool), "test")
assert.NoError(t, err, "Struct should be convertible when unsupported types are in unexported fields")
})
})
t.Run("StructWithJSONTags", func(t *testing.T) {
t.Run("JSONOmitTag", func(t *testing.T) {
type TestStruct struct {
PublicField string
OmittedField unsafe.Pointer `json:"-"`
AnotherField int
}
err := qjs.IsConvertibleToJs(reflect.TypeOf(TestStruct{}), make(map[reflect.Type]bool), "test")
assert.NoError(t, err, "Struct should be convertible when unsupported types have json:\"-\" tag")
})
t.Run("JSONRenameTag", func(t *testing.T) {
type TestStruct struct {
Field string `json:"renamed_field"`
UnsupportedField unsafe.Pointer `json:"-"`
}
err := qjs.IsConvertibleToJs(reflect.TypeOf(TestStruct{}), make(map[reflect.Type]bool), "test")
assert.NoError(t, err, "Struct should be convertible with JSON rename tags")
})
t.Run("UnsupportedFieldWithoutOmitTag", func(t *testing.T) {
type TestStruct struct {
PublicField string
UnsupportedField unsafe.Pointer
}
err := qjs.IsConvertibleToJs(reflect.TypeOf(TestStruct{}), make(map[reflect.Type]bool), "test")
assert.Error(t, err, "Struct should not be convertible with exported unsupported fields")
assert.Contains(t, err.Error(), "TestStruct.UnsupportedField", "Error should mention the problematic field")
})
t.Run("ComplexJSONTags", func(t *testing.T) {
type TestStruct struct {
Field1 string `json:"field1,omitempty"`
Field2 int `json:"field2"`
OmittedField unsafe.Pointer `json:"-"`
AnotherOmitted unsafe.Pointer `json:"-,"`
}
err := qjs.IsConvertibleToJs(reflect.TypeOf(TestStruct{}), make(map[reflect.Type]bool), "test")
assert.NoError(t, err, "Struct should handle complex JSON tags correctly")
})
})
t.Run("RecursiveTypes", func(t *testing.T) {
type RecursiveStruct struct {
Name string
Self *RecursiveStruct
}
err := qjs.IsConvertibleToJs(reflect.TypeOf(RecursiveStruct{}), make(map[reflect.Type]bool), "test")
assert.NoError(t, err, "Recursive struct should be convertible")
})
t.Run("NestedStructs", func(t *testing.T) {
t.Run("ValidNested", func(t *testing.T) {
type Inner struct {
Value int
}
type Outer struct {
Inner Inner
Name string
}
err := qjs.IsConvertibleToJs(reflect.TypeOf(Outer{}), make(map[reflect.Type]bool), "test")
assert.NoError(t, err, "Nested convertible structs should be convertible")
})
t.Run("InvalidNested", func(t *testing.T) {
type Inner struct {
BadField unsafe.Pointer
}
type Outer struct {
Inner Inner
Name string
}
err := qjs.IsConvertibleToJs(reflect.TypeOf(Outer{}), make(map[reflect.Type]bool), "test")
assert.Error(t, err, "Nested struct with unsupported fields should not be convertible")
})
t.Run("NestedWithOmittedField", func(t *testing.T) {
type Inner struct {
GoodField string
BadField chan int `json:"-"`
}
type Outer struct {
Inner Inner
Name string
}
err := qjs.IsConvertibleToJs(reflect.TypeOf(Outer{}), make(map[reflect.Type]bool), "test")
assert.NoError(t, err, "Nested struct with omitted unsupported fields should be convertible")
})
t.Run("EmbeddedPointerStruct", func(t *testing.T) {
type Inner struct {
GoodField string
}
type Outer struct {
*Inner
Name string
}
err := qjs.IsConvertibleToJs(reflect.TypeOf(Outer{}), make(map[reflect.Type]bool), "test")
assert.NoError(t, err, "Struct with embedded pointer to struct should be convertible")
})
t.Run("EmbeddedPointerWithUnsupportedField", func(t *testing.T) {
type Inner struct {
BadField unsafe.Pointer
}
type Outer struct {
*Inner
Name string
}
err := qjs.IsConvertibleToJs(reflect.TypeOf(Outer{}), make(map[reflect.Type]bool), "test")
assert.Error(t, err, "Struct with embedded pointer containing unsupported fields should not be convertible")
})
t.Run("EmbeddedPointerWithOmittedField", func(t *testing.T) {
type Inner struct {
GoodField string
BadField chan int `json:"-"`
}
type Outer struct {
*Inner
Name string
}
err := qjs.IsConvertibleToJs(reflect.TypeOf(Outer{}), make(map[reflect.Type]bool), "test")
assert.NoError(t, err, "Struct with embedded pointer containing omitted unsupported fields should be convertible")
})
})
t.Run("Pointers", func(t *testing.T) {
type TestStruct struct {
Field string
OmittedField unsafe.Pointer `json:"-"`
}
err := qjs.IsConvertibleToJs(reflect.TypeOf(&TestStruct{}), make(map[reflect.Type]bool), "test")
assert.NoError(t, err, "Pointer to convertible struct should be convertible")
})
}
func TestGetGoTypeName(t *testing.T) {
// Test input handling - reflect.Type vs value
assert.Equal(t, "int", qjs.GetGoTypeName(reflect.TypeOf(42)))
assert.Equal(t, "int", qjs.GetGoTypeName(42))
// Test nil input panics
assert.Panics(t, func() { qjs.GetGoTypeName(nil) })
// Test pointer branch
var intPtr *int
assert.Equal(t, "*int", qjs.GetGoTypeName(intPtr))
// Test slice branch
var intSlice []int
assert.Equal(t, "[]int", qjs.GetGoTypeName(intSlice))
// Test array branch
var intArray [5]int
assert.Equal(t, "[5]int", qjs.GetGoTypeName(intArray))
// Test map branch
var stringIntMap map[string]int
assert.Equal(t, "map[string]int", qjs.GetGoTypeName(stringIntMap))
// Test channel branch
var intChan chan int
assert.Equal(t, "chan int", qjs.GetGoTypeName(intChan))
// Test function branch
var simpleFunc func()
assert.Equal(t, "func()", qjs.GetGoTypeName(simpleFunc))
// Test default case (basic types)
assert.Equal(t, "string", qjs.GetGoTypeName("hello"))
assert.Equal(t, "bool", qjs.GetGoTypeName(true))
assert.Equal(t, "float64", qjs.GetGoTypeName(3.14))
var goFunc func(int, ...int) (int, error)
assert.Equal(t, "func(int, ...int) (int, error)", qjs.GetGoTypeName(goFunc))
}