forked from einride/can-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
frame_json_test.go
126 lines (120 loc) · 2.69 KB
/
frame_json_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
package can
import (
"encoding/json"
"fmt"
"math/rand"
"reflect"
"testing"
"testing/quick"
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
)
func TestFrame_JSON(t *testing.T) {
for _, tt := range []struct {
jsonFrame string
frame Frame
}{
{
// Standard frame
jsonFrame: `{"id":42,"data":"00010203"}`,
frame: Frame{
ID: 42,
Length: 4,
Data: Data{0x00, 0x01, 0x02, 0x03},
},
},
{
// Standard frame, no data
jsonFrame: `{"id":42}`,
frame: Frame{ID: 42},
},
{
// Standard remote frame
jsonFrame: `{"id":42,"remote":true,"length":4}`,
frame: Frame{
ID: 42,
IsRemote: true,
Length: 4,
},
},
{
// Extended frame
jsonFrame: `{"id":42,"data":"0001020304050607","extended":true}`,
frame: Frame{
ID: 42,
IsExtended: true,
Length: 8,
Data: Data{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07},
},
},
{
// Extended frame, no data
jsonFrame: `{"id":42,"extended":true}`,
frame: Frame{ID: 42, IsExtended: true},
},
{
// Extended remote frame
jsonFrame: `{"id":42,"extended":true,"remote":true,"length":8}`,
frame: Frame{
ID: 42,
IsExtended: true,
IsRemote: true,
Length: 8,
},
},
} {
tt := tt
t.Run(fmt.Sprintf("JSON|frame=%v", tt.frame), func(t *testing.T) {
assert.Check(t, is.Equal(tt.jsonFrame, tt.frame.JSON()))
})
t.Run(fmt.Sprintf("UnmarshalJSON|frame=%v", tt.frame), func(t *testing.T) {
var frame Frame
if err := json.Unmarshal([]byte(tt.jsonFrame), &frame); err != nil {
t.Fatal(err)
}
assert.Check(t, is.DeepEqual(tt.frame, frame))
})
}
}
func TestFrame_UnmarshalJSON_Invalid(t *testing.T) {
var f Frame
t.Run("invalid JSON", func(t *testing.T) {
data := `foobar`
assert.Check(t, f.UnmarshalJSON([]uint8(data)) != nil)
})
t.Run("invalid payload", func(t *testing.T) {
data := `{"id":1,"data":"foobar","extended":false,"remote":false}`
assert.Check(t, f.UnmarshalJSON([]uint8(data)) != nil)
})
}
func (Frame) Generate(rand *rand.Rand, size int) reflect.Value {
f := Frame{
IsExtended: rand.Intn(2) == 0,
IsRemote: rand.Intn(2) == 0,
}
if f.IsExtended {
f.ID = rand.Uint32() & MaxExtendedID
} else {
f.ID = rand.Uint32() & MaxID
}
f.Length = uint8(rand.Intn(9))
if !f.IsRemote {
_, _ = rand.Read(f.Data[:f.Length])
}
return reflect.ValueOf(f)
}
func TestPropertyFrame_MarshalUnmarshalJSON(t *testing.T) {
f := func(f Frame) Frame {
return f
}
g := func(f Frame) Frame {
f2 := Frame{}
if err := json.Unmarshal([]uint8(f.JSON()), &f2); err != nil {
t.Fatal(err)
}
return f2
}
if err := quick.CheckEqual(f, g, nil); err != nil {
t.Fatal(err)
}
}