-
Notifications
You must be signed in to change notification settings - Fork 3
/
structure_encoder_test.go
159 lines (136 loc) · 5.96 KB
/
structure_encoder_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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package y3
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/yomorun/y3-codec-golang/internal/utils"
)
func TestStructEncoderWithSignals(t *testing.T) {
input := exampleData{
Name: "yomo",
Noise: float32(456),
Therm: thermometer{Temperature: float32(30), Humidity: float32(40)},
}
encoder := newStructEncoder(0x30, structEncoderOptionRoot(utils.RootToken),
structEncoderOptionConfig(&structEncoderConfig{
ZeroFields: true,
TagName: "y3",
}))
inputBuf, _ := encoder.Encode(input,
createSignal(0x02).SetString("a"),
createSignal(0x03).SetString("b"))
testPrintf("inputBuf=%v\n", utils.FormatBytes(inputBuf))
var mold exampleData
err := ToObject(inputBuf[2+3+3:], &mold)
assert.NoError(t, err, fmt.Sprintf("decode error:%v", err))
assert.Equal(t, input.Name, mold.Name, fmt.Sprintf("value does not match(%v): %v", input.Name, mold.Name))
assert.Equal(t, input.Noise, mold.Noise, fmt.Sprintf("value does not match(%v): %v", input.Noise, mold.Noise))
assert.Equal(t, input.Therm.Temperature, mold.Therm.Temperature, fmt.Sprintf("value does not match(%v): %v", input.Therm.Temperature, mold.Therm.Temperature))
assert.Equal(t, input.Therm.Humidity, mold.Therm.Humidity, fmt.Sprintf("value does not match(%v): %v", input.Therm.Humidity, mold.Therm.Humidity))
}
func TestStructEncoderWithSignalsNoRoot(t *testing.T) {
input := exampleData{
Name: "yomo",
Noise: float32(456),
Therm: thermometer{Temperature: float32(30), Humidity: float32(40)},
}
encoder := newStructEncoder(0x30)
inputBuf, _ := encoder.Encode(input,
createSignal(0x02).SetString("a"),
createSignal(0x03).SetString("b"))
testPrintf("inputBuf=%v\n", utils.FormatBytes(inputBuf))
var mold exampleData
err := ToObject(inputBuf[3+3:], &mold)
assert.NoError(t, err, fmt.Sprintf("decode error:%v", err))
assert.Equal(t, input.Name, mold.Name, fmt.Sprintf("value does not match(%v): %v", input.Name, mold.Name))
assert.Equal(t, input.Noise, mold.Noise, fmt.Sprintf("value does not match(%v): %v", input.Noise, mold.Noise))
assert.Equal(t, input.Therm.Temperature, mold.Therm.Temperature, fmt.Sprintf("value does not match(%v): %v", input.Therm.Temperature, mold.Therm.Temperature))
assert.Equal(t, input.Therm.Humidity, mold.Therm.Humidity, fmt.Sprintf("value does not match(%v): %v", input.Therm.Humidity, mold.Therm.Humidity))
}
func TestStructSliceEncoderWithSignals(t *testing.T) {
input := exampleSlice{
Therms: []thermometer{
{Temperature: float32(30), Humidity: float32(40)},
{Temperature: float32(50), Humidity: float32(60)},
},
}
encoder := newStructEncoder(0x30, structEncoderOptionRoot(utils.RootToken))
inputBuf, _ := encoder.Encode(input,
createSignal(0x02).SetString("a"),
createSignal(0x03).SetString("b"))
testPrintf("inputBuf=%v\n", utils.FormatBytes(inputBuf))
var mold exampleSlice
err := ToObject(inputBuf[2+3+3:], &mold)
assert.NoError(t, err, fmt.Sprintf("decode error:%v", err))
assert.Equal(t, float32(30), mold.Therms[0].Temperature, fmt.Sprintf("value does not match(%v): %v", float32(30), mold.Therms[0].Temperature))
assert.Equal(t, float32(40), mold.Therms[0].Humidity, fmt.Sprintf("value does not match(%v): %v", float32(40), mold.Therms[0].Humidity))
assert.Equal(t, float32(50), mold.Therms[1].Temperature, fmt.Sprintf("value does not match(%v): %v", float32(50), mold.Therms[1].Temperature))
assert.Equal(t, float32(60), mold.Therms[1].Humidity, fmt.Sprintf("value does not match(%v): %v", float32(60), mold.Therms[1].Humidity))
}
func TestStructSliceEncoderWithSignalsNoRoot(t *testing.T) {
input := exampleSlice{
Therms: []thermometer{
{Temperature: float32(30), Humidity: float32(40)},
{Temperature: float32(50), Humidity: float32(60)},
},
}
encoder := newStructEncoder(0x30)
inputBuf, _ := encoder.Encode(input,
createSignal(0x02).SetString("a"),
createSignal(0x03).SetString("b"))
testPrintf("inputBuf=%v\n", utils.FormatBytes(inputBuf))
var mold exampleSlice
err := ToObject(inputBuf[3+3:], &mold)
assert.NoError(t, err, fmt.Sprintf("decode error:%v", err))
assert.Equal(t, float32(30), mold.Therms[0].Temperature, fmt.Sprintf("value does not match(%v): %v", float32(30), mold.Therms[0].Temperature))
assert.Equal(t, float32(40), mold.Therms[0].Humidity, fmt.Sprintf("value does not match(%v): %v", float32(40), mold.Therms[0].Humidity))
assert.Equal(t, float32(50), mold.Therms[1].Temperature, fmt.Sprintf("value does not match(%v): %v", float32(50), mold.Therms[1].Temperature))
assert.Equal(t, float32(60), mold.Therms[1].Humidity, fmt.Sprintf("value does not match(%v): %v", float32(60), mold.Therms[1].Humidity))
}
func TestStructForbidUserKey(t *testing.T) {
input := exampleData{
Name: "yomo",
Noise: float32(456),
Therm: thermometer{Temperature: float32(30), Humidity: float32(40)},
}
var key byte = 0x02
assert.Panics(t, func() {
newStructEncoder(key,
structEncoderOptionRoot(utils.RootToken),
structEncoderOptionConfig(&structEncoderConfig{
ZeroFields: true,
TagName: "y3",
}),
structEncoderOptionForbidUserKey(utils.ForbidUserKey)).
Encode(input)
}, "should forbid this Key: %#x", key)
key = 0x0f
assert.Panics(t, func() {
newStructEncoder(key,
structEncoderOptionRoot(utils.RootToken),
structEncoderOptionConfig(&structEncoderConfig{
ZeroFields: true,
TagName: "y3",
}),
structEncoderOptionForbidUserKey(utils.ForbidUserKey)).
Encode(input)
}, "should forbid this Key: %#x", key)
}
func TestStructAllowSignalKey(t *testing.T) {
input := exampleData{
Name: "yomo",
Noise: float32(456),
Therm: thermometer{Temperature: float32(30), Humidity: float32(40)},
}
var signalKey byte = 0x02
assert.NotPanics(t, func() {
newStructEncoder(0x30,
structEncoderOptionRoot(utils.RootToken),
structEncoderOptionConfig(&structEncoderConfig{
ZeroFields: true,
TagName: "y3",
}),
structEncoderOptionAllowSignalKey(utils.AllowSignalKey)).
Encode(input, createSignal(signalKey).SetString("a"))
}, "should allow this Signal Key: %#x", signalKey)
}