Test Failed
Push — master ( 1b19dc...4e0cc6 )
by Nikita
01:45
created

encode_test.TestEncodeStruct   A

Complexity

Conditions 1

Size

Total Lines 26
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 19
nop 1
dl 0
loc 26
rs 9.45
c 0
b 0
f 0
1
package encode_test
2
3
import (
4
	"testing"
5
6
	"github.com/et-nik/binngo/binn"
7
	"github.com/et-nik/binngo/encode"
8
	"github.com/stretchr/testify/assert"
9
)
10
11
func TestEncodeStruct(t *testing.T) {
12
	v := struct{
13
		Val1 int64
14
		Val2 string
15
	} {
16
		123,
17
		"value",
18
	}
19
20
	result, err := encode.Marshal(v)
21
22
	assert.Nil(t, err)
23
	assert.Equal(t, []byte{
24
		binn.ObjectType,
25
		23,									// total size
26
		2,									// key/value pairs
27
28
		4, 'V', 'a', 'l', '1',				// key
29
		binn.Uint8Type,						// value type
30
		123,								// value
31
32
		4, 'V', 'a', 'l', '2', 				// key
33
		binn.StringType,					// value type
34
		5, 'v', 'a', 'l', 'u', 'e', 0x00, 	// value
35
36
	}, result)
37
}
38
39
func TestEncodeMapObjectWithIntValue(t *testing.T) {
40
	v := map[string]int{"key": 123}
41
42
	result, err := encode.Marshal(v)
43
44
	assert.Nil(t, err)
45
	assert.Equal(t, []byte{
46
		binn.ObjectType,
47
		9,									// total size
48
		1,									// key/value pairs
49
50
		3, 'k', 'e', 'y',					// key
51
		binn.Uint8Type,						// value type
52
		123,								// value
53
54
	}, result)
55
}
56
57
func TestEncodeMapObjectWithStringValue(t *testing.T) {
58
	v := map[string]string{"hello": "world"}
59
60
	result, err := encode.Marshal(v)
61
62
	assert.Nil(t, err)
63
	assert.Equal(t, []byte{
64
		binn.ObjectType,				// [type] object (container)
65
		0x11,							// [size] container total size
66
		0x01,							// [count] key/value pairs
67
		0x05, 'h', 'e', 'l', 'l', 'o',	// key
68
		0xA0,							// [type] = string
69
		0x05,							// [size]
70
		'w', 'o', 'r', 'l', 'd', 0x00,	// [data] (null terminated)
71
72
	}, result)
73
}
74
75
func TestEncodeMap(t *testing.T) {
76
	v := map[int]int{9: 9}
77
78
	result, err := encode.Marshal(v)
79
80
	assert.Nil(t, err)
81
	assert.Equal(t, []byte{
82
		binn.MapType,
83
		9,									// total size
84
		1,									// key/value pairs
85
86
		0x00, 0x00, 0x00, 0x09,				// key
87
		binn.Uint8Type,						// value type
88
		9,									// value
89
90
	}, result)
91
}
92
93
func TestEncodeUint(t *testing.T) {
94
	v := 123
95
96
	result, err := encode.Marshal(v)
97
98
	assert.Nil(t, err)
99
	assert.Equal(t, []byte{binn.Uint8Type, 123}, result)
100
}
101
102
func TestEncodeInt16(t *testing.T) {
103
	v := -800
104
105
	result, err := encode.Marshal(v)
106
107
	assert.Nil(t, err)
108
	assert.Equal(t, []byte{binn.Int16Type, 0xFC, 0xE0}, result)
109
}
110
111
func TestEncodeString(t *testing.T) {
112
	v := "test"
113
114
	result, err := encode.Marshal(v)
115
116
	assert.Nil(t, err)
117
	assert.Equal(t, []byte{binn.StringType, 4, 't', 'e', 's', 't', 0x00}, result)
118
}
119
120
func TestEncodeList(t *testing.T) {
121
	v := []int{123, -456, 789}
122
123
	result, err := encode.Marshal(v)
124
125
	assert.Nil(t, err)
126
	assert.Equal(t, []byte{
127
		binn.ListType,	// [type] list (container)
128
		0x0B,			// [size] container total size
129
		0x03,			// [count] items
130
		0x20, 			// [type] = uint8
131
		0x7B,			// [data] (123)
132
		0x41,			// [type] = int16
133
		0xFE, 0x38,		// [data] (-456)
134
		0x40,			// [type] = uint16
135
		0x03, 0x15,		// [data] (789)
136
137
	}, result)
138
}
139
140
func TestListInsideMap(t *testing.T) {
141
	v := map[int][]int{
142
		1: {123},
143
		2: {-12345, 6789},
144
	}
145
146
	result, err := encode.Marshal(v)
147
148
	assert.Nil(t, err)
149
	assert.Equal(t, []byte{
150
		binn.MapType,				// [type] list (container)
151
		0x19,						// [size] container total size
152
		0x02,						// [count] items
153
		0x00, 0x00, 0x00, 0x01, 	// key
154
155
			binn.ListType,			// [type] list (container)
156
			0x05, 					// [size] list total size
157
			0x01, 					// [count] list items
158
			binn.Uint8Type,			// [type] = uint8
159
			0x7B,					// [data] (123)
160
161
		0x00, 0x00, 0x00, 0x02, 	// key
162
163
			binn.ListType,			// [type] list (container)
164
			0x09,					// [size] container total size
165
			0x02,					// [count] items
166
			binn.Int16Type,			// [type] = int16
167
			0xCF, 0xC7,				// [data] (-12345)
168
			binn.Uint16Type,		// [type] = uint16
169
			0x1A, 0x85,				// [data] (6789)
170
	}, result)
171
}
172
173
func TestListInterface(t *testing.T) {
174
	var v [2]interface{}
175
	v[0] = 123
176
	v[1] = "string"
177
178
	result, err := encode.Marshal(v)
179
180
	assert.Nil(t, err)
181
	assert.Equal(t, []byte{
182
		binn.ListType,							// [type] list (container)
183
		14,										// [size] container total size
184
		0x02,									// [count] items
185
		binn.Uint8Type,							// [type] = uint8
186
		0x7B,									// [data] (123)
187
		binn.StringType,						// [type] = string
188
		0x06,									// [size] string len,
189
		's', 't', 'r', 'i', 'n', 'g', 0x00, 	// [data] null terminated
190
	}, result)
191
}
192