-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbasic_test.go
114 lines (91 loc) · 3.46 KB
/
basic_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
package y3
import (
"fmt"
"reflect"
"testing"
"github.com/stretchr/testify/assert"
"github.com/yomorun/y3-codec-golang/internal/utils"
)
func TestBasicEncoderWithSignals(t *testing.T) {
input := int32(456)
encoder := newBasicEncoder(0x10, basicEncoderOptionRoot(utils.RootToken))
inputBuf, _ := encoder.Encode(input,
createSignal(0x02).SetString("a"),
createSignal(0x03).SetString("b"))
testPrintf("inputBuf=%v\n", utils.FormatBytes(inputBuf))
value, err := ToInt32(inputBuf[2+3+3:])
assert.NoError(t, err, fmt.Sprintf("decode error:%v", err))
assert.Equal(t, input, value, fmt.Sprintf("value does not match(%v): %v", input, value))
}
func TestBasicEncoderWithSignalsNoRoot(t *testing.T) {
input := int32(456)
encoder := newBasicEncoder(0x10)
inputBuf, _ := encoder.Encode(input,
createSignal(0x02).SetString("a"),
createSignal(0x03).SetString("b"))
testPrintf("inputBuf=%v\n", utils.FormatBytes(inputBuf))
value, err := ToInt32(inputBuf[3+3:])
assert.NoError(t, err, fmt.Sprintf("decode error:%v", err))
assert.Equal(t, input, value, fmt.Sprintf("value does not match(%v): %v", input, value))
}
func TestBasicSliceEncoderWithSignals(t *testing.T) {
input := []int32{123, 456}
encoder := newBasicEncoder(0x10, basicEncoderOptionRoot(utils.RootToken))
inputBuf, _ := encoder.Encode(input,
createSignal(0x02).SetString("a"),
createSignal(0x03).SetString("b"))
testPrintf("inputBuf=%v\n", utils.FormatBytes(inputBuf))
value, err := ToInt32Slice(inputBuf[2+3+3:])
assert.NoError(t, err, fmt.Sprintf("decode error:%v", err))
expectedValue := reflect.ValueOf(input)
resultValue := reflect.ValueOf(value)
for i := 0; i < expectedValue.Len(); i++ {
assert.Equal(t, expectedValue.Index(i).Interface(), resultValue.Index(i).Interface(),
fmt.Sprintf("Item values are not equal %v: %v",
expectedValue.Index(i).Interface(), resultValue.Index(i).Interface()))
}
}
func TestBasicSliceEncoderWithSignalsNoRoot(t *testing.T) {
input := []int32{123, 456}
encoder := newBasicEncoder(0x10)
inputBuf, _ := encoder.Encode(input,
createSignal(0x02).SetString("a"),
createSignal(0x03).SetString("b"))
testPrintf("inputBuf=%v\n", utils.FormatBytes(inputBuf))
value, err := ToInt32Slice(inputBuf[3+3:])
assert.NoError(t, err, fmt.Sprintf("decode error:%v", err))
expectedValue := reflect.ValueOf(input)
resultValue := reflect.ValueOf(value)
for i := 0; i < expectedValue.Len(); i++ {
assert.Equal(t, expectedValue.Index(i).Interface(), resultValue.Index(i).Interface(),
fmt.Sprintf("Item values are not equal %v: %v",
expectedValue.Index(i).Interface(), resultValue.Index(i).Interface()))
}
}
func TestBasicForbidUserKey(t *testing.T) {
input := int32(456)
var key byte = 0x02
assert.Panics(t, func() {
newBasicEncoder(key,
basicEncoderOptionRoot(utils.RootToken),
basicEncoderOptionForbidUserKey(utils.ForbidUserKey)).
Encode(input)
}, "should forbid this Key: %#x", key)
key = 0x0f
assert.Panics(t, func() {
newBasicEncoder(key,
basicEncoderOptionRoot(utils.RootToken),
basicEncoderOptionForbidUserKey(utils.ForbidUserKey)).
Encode(input)
}, "should forbid this Key: %#x", key)
}
func TestBasicAllowSignalKey(t *testing.T) {
input := int32(456)
var signalKey byte = 0x02
assert.NotPanics(t, func() {
newBasicEncoder(0x10,
basicEncoderOptionRoot(utils.RootToken),
basicEncoderOptionAllowSignalKey(utils.AllowSignalKey)).
Encode(input, createSignal(signalKey).SetString("a"))
}, "should allow this Signal Key: %#x", signalKey)
}