-
Notifications
You must be signed in to change notification settings - Fork 2
/
dictionary_test.go
137 lines (116 loc) · 3.25 KB
/
dictionary_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
package goscale
import (
"bytes"
"io"
"testing"
"github.com/stretchr/testify/assert"
)
func Test_EncodeDictionaryStrBool(t *testing.T) {
var testExamples = []struct {
label string
input Dictionary[Str, Bool]
expectation []byte
}{
{
label: "Dictionary(aaa: true, bbb: false, ccc: true)",
input: Dictionary[Str, Bool]{Str("aaa"): true, Str("bbb"): false, Str("ccc"): true},
expectation: []byte{
0x0c,
0x0c, 0x61, 0x61, 0x61, 0x01,
0x0c, 0x62, 0x62, 0x62, 0x00,
0x0c, 0x63, 0x63, 0x63, 0x01,
},
},
}
for _, testExample := range testExamples {
t.Run(testExample.label, func(t *testing.T) {
buffer := &bytes.Buffer{}
err := testExample.input.Encode(buffer)
assert.NoError(t, err)
assert.Equal(t, testExample.expectation, buffer.Bytes())
assert.Equal(t, testExample.expectation, testExample.input.Bytes())
})
}
}
func Test_DecodeDictionaryStrBool(t *testing.T) {
var testExamples = []struct {
label string
input []byte
expectation Dictionary[Str, Bool]
}{
{
label: "(0x0c0c636363010c626262000c61616101)",
input: []byte{0x0c, 0x0c, 0x63, 0x63, 0x63, 0x01, 0x0c, 0x62, 0x62, 0x62, 0x00, 0x0c, 0x61, 0x61, 0x61, 0x01},
expectation: Dictionary[Str, Bool]{Str("aaa"): true, Str("bbb"): false, Str("ccc"): true},
},
}
for _, testExample := range testExamples {
t.Run(testExample.label, func(t *testing.T) {
buffer := &bytes.Buffer{}
buffer.Write(testExample.input)
result, err := DecodeDictionary[Str, Bool](buffer)
assert.NoError(t, err)
assert.Equal(t, testExample.expectation, result)
})
}
}
func Test_EncodeDictionaryU8Str(t *testing.T) {
var testExamples = []struct {
label string
input Dictionary[U8, Str]
expectation []byte
}{
{
label: "Dictionary(1: aaa, 2: bbb, 3: ccc)",
input: Dictionary[U8, Str]{1: Str("aaa"), 2: Str("bbb"), 3: Str("ccc")},
expectation: []byte{
0x0c,
0x01, 0x0c, 0x61, 0x61, 0x61,
0x02, 0x0c, 0x62, 0x62, 0x62,
0x03, 0x0c, 0x63, 0x63, 0x63,
},
},
}
for _, testExample := range testExamples {
t.Run(testExample.label, func(t *testing.T) {
buffer := &bytes.Buffer{}
err := testExample.input.Encode(buffer)
assert.NoError(t, err)
assert.Equal(t, testExample.expectation, buffer.Bytes())
assert.Equal(t, testExample.expectation, testExample.input.Bytes())
})
}
}
func Test_DecodeDictionaryU8Str(t *testing.T) {
var testExamples = []struct {
label string
input []byte
expectation Dictionary[U8, Str]
}{
{
label: "()",
input: []byte{
0x0c,
0x03, 0x0c, 0x63, 0x63, 0x63,
0x02, 0x0c, 0x62, 0x62, 0x62,
0x01, 0x0c, 0x61, 0x61, 0x61,
},
expectation: Dictionary[U8, Str]{1: Str("aaa"), 2: Str("bbb"), 3: Str("ccc")},
},
}
for _, testExample := range testExamples {
t.Run(testExample.label, func(t *testing.T) {
buffer := &bytes.Buffer{}
buffer.Write(testExample.input)
result, err := DecodeDictionary[U8, Str](buffer)
assert.NoError(t, err)
assert.Equal(t, testExample.expectation, result)
})
}
}
func Test_DecodeDictionary_Empty(t *testing.T) {
buffer := &bytes.Buffer{}
result, err := DecodeDictionary[U8, Str](buffer)
assert.Equal(t, io.EOF, err)
assert.Equal(t, Dictionary[U8, Str](nil), result)
}