-
Notifications
You must be signed in to change notification settings - Fork 9
/
slice_test.go
63 lines (48 loc) · 1.39 KB
/
slice_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
package coven
import (
"reflect"
"testing"
"unsafe"
)
func TestSliceConverter_Convert(t *testing.T) {
type foo struct {
A int
}
type bar struct {
A *int
}
c := newSliceConverter(&convertType{reflect.TypeOf([]bar{}), reflect.TypeOf([]foo{}), nil})
s := []foo{foo{1}, foo{2}, foo{3}}
d := make([]bar, 0)
d = nil
c.convert(unsafe.Pointer(dereferencedValue(&d).UnsafeAddr()), unsafe.Pointer(dereferencedValue(&s).UnsafeAddr()))
if expected := `[{"A":1},{"A":2},{"A":3}]`; !reflect.DeepEqual(expected, jsonEncode(d)) {
t.Fatalf("[expected:%v] [actual:%v]", expected, jsonEncode(d))
}
a := []int{1, 2, 3}
b := []*byte{}
c = newSliceConverter(&convertType{reflect.TypeOf([]*byte{}), reflect.TypeOf([]int{}), nil})
c.convert(unsafe.Pointer(&b), unsafe.Pointer(&a))
if expected := `[1,2,3]`; !reflect.DeepEqual(expected, jsonEncode(b)) {
t.Fatalf("[expected:%v] [actual:%v]", expected, jsonEncode(b))
}
}
func TestSameSliceConvert(t *testing.T) {
a := []int{1, 2, 3}
b := []int{4}
c := newSliceConverter(&convertType{reflect.TypeOf([]int{}), reflect.TypeOf([]int{}), nil})
c.convert(unsafe.Pointer(&b), unsafe.Pointer(&a))
if expected := []int{1, 2, 3}; !reflect.DeepEqual(expected, b) {
t.Fatalf("[expected:%v] [actual:%v]", expected, b)
}
}
func TestOptionSliceStructConvert(t *testing.T) {
type Foo struct {
A string
B1 string
}
type Bar struct {
A string
B string
}
}