-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathheterogeneous_test.go
134 lines (116 loc) · 2.23 KB
/
heterogeneous_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
package memdump
import (
"bytes"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func testEncodeDecode(t *testing.T, src interface{}, dest interface{}) {
var b bytes.Buffer
enc := NewHeterogeneousEncoder(&b)
err := enc.Encode(src)
require.NoError(t, err)
dec := NewHeterogeneousDecoder(&b)
err = dec.Decode(dest)
require.NoError(t, err)
}
func TestHeterogeneous_Int(t *testing.T) {
var dest int
var src int = 3
testEncodeDecode(t, &src, &dest)
assert.EqualValues(t, src, dest)
}
func TestHeterogeneous_String(t *testing.T) {
var dest string
src := "abc"
testEncodeDecode(t, &src, &dest)
assert.EqualValues(t, src, dest)
}
func TestHeterogeneous_Slice(t *testing.T) {
var dest []int16
src := []int16{5, 4, 3}
testEncodeDecode(t, &src, &dest)
assert.EqualValues(t, src, dest)
}
func TestHeterogeneous_Large(t *testing.T) {
type T struct {
A string
B int
}
src := make([]T, 1000)
for i := range src {
src[i].A = strings.Repeat("123", 100)
src[i].B = 123
}
var dest []T
testEncodeDecode(t, &src, &dest)
assert.EqualValues(t, src, dest)
}
func TestHeterogeneous_Struct(t *testing.T) {
type U struct {
W string
X *string
}
type T struct {
U
A int
B byte
C []U
D []*U
E *[]U
F *U
G *T
}
s := "hello"
u := U{W: s, X: &s}
src := T{
U: u,
A: 5,
B: 6,
C: []U{u},
D: []*U{&u},
F: &u,
}
src.E = &src.C
src.G = &src
var dest T
testEncodeDecode(t, &src, &dest)
assert.EqualValues(t, src, dest)
}
func TestHeterogeneous_Incompatible(t *testing.T) {
type U struct {
X int
Y string
}
type V struct {
X string
Y int
}
src := U{3, "abc"}
var dest V
var b bytes.Buffer
enc := NewHeterogeneousEncoder(&b)
err := enc.Encode(&src)
require.NoError(t, err)
dec := NewHeterogeneousDecoder(&b)
err = dec.Decode(&dest)
assert.Equal(t, ErrIncompatibleLayout, err)
}
func TestHeterogeneousEncodeUnsupportedTypes(t *testing.T) {
var buf bytes.Buffer
enc := NewHeterogeneousEncoder(&buf)
assert.Panics(t, func() {
enc.Encode(&map[string]int{})
})
assert.Panics(t, func() {
enc.Encode(func() {})
})
assert.Panics(t, func() {
enc.Encode(make(chan int))
})
assert.Panics(t, func() {
var x interface{} = "abc"
enc.Encode(&x)
})
}