-
Notifications
You must be signed in to change notification settings - Fork 38
/
frame_string_test.go
86 lines (82 loc) · 1.67 KB
/
frame_string_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
package can
import (
"fmt"
"testing"
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
)
func TestFrame_String(t *testing.T) {
for _, tt := range []struct {
frame Frame
str string
}{
{
frame: Frame{
ID: 0x62e,
Length: 2,
Data: Data{0x10, 0x44},
},
str: "62E#1044",
},
{
frame: Frame{
ID: 0x410,
IsRemote: true,
Length: 3,
},
str: "410#R3",
},
{
frame: Frame{
ID: 0xd2,
Length: 2,
Data: Data{0xf0, 0x31},
},
str: "0D2#F031",
},
{
frame: Frame{ID: 0xee},
str: "0EE#",
},
{
frame: Frame{ID: 0},
str: "000#",
},
{
frame: Frame{ID: 0, IsExtended: true},
str: "00000000#",
},
{
frame: Frame{ID: 0x1234abcd, IsExtended: true},
str: "1234ABCD#",
},
} {
tt := tt
t.Run(fmt.Sprintf("String|frame=%v,str=%v", tt.frame, tt.str), func(t *testing.T) {
assert.Check(t, is.Equal(tt.str, tt.frame.String()))
})
t.Run(fmt.Sprintf("UnmarshalString|frame=%v,str=%v", tt.frame, tt.str), func(t *testing.T) {
var actual Frame
if err := actual.UnmarshalString(tt.str); err != nil {
t.Fatal(err)
}
assert.Check(t, is.DeepEqual(actual, tt.frame))
})
}
}
func TestParseFrame_Errors(t *testing.T) {
for _, tt := range []string{
"foo", // invalid
"foo#", // invalid ID
"0D23#F031", // invalid ID length
"62E#104400000000000000", // invalid data length
} {
tt := tt
t.Run(fmt.Sprintf("str=%v", tt), func(t *testing.T) {
var frame Frame
err := frame.UnmarshalString(tt)
assert.ErrorContains(t, err, "invalid")
assert.Check(t, is.DeepEqual(Frame{}, frame))
})
}
}