-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecoder_test.go
109 lines (101 loc) · 2.05 KB
/
decoder_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
package alarmdecoder
import (
"bytes"
"io"
"reflect"
"testing"
"github.com/pkg/errors"
)
func TestParse(t *testing.T) {
cases := []struct {
raw string
want Message
}{
{
`[10000601100000003A--],045,[f71f00000045001c28020000000000],"****DISARMED**** READY TO ARM "`,
Message{
Ready: true,
Beeps: 6,
ACPower: true,
ChimeEnabled: true,
Mode: "A",
Zone: "045",
RawData: "[f71f00000045001c28020000000000]",
KeypadMessage: "****DISARMED**** READY TO ARM",
},
},
{
`[00000000011000003A--],,,"test"`,
Message{
AlarmHasOccured: true,
AlarmSounding: true,
Mode: "A",
KeypadMessage: "test",
},
},
{
`[00000000000001003A--],,,"test"`,
Message{
Fire: true,
Mode: "A",
KeypadMessage: "test",
},
},
{
`[00000000000000103A--],,,"test"`,
Message{
SystemIssue: true,
Mode: "A",
KeypadMessage: "test",
},
},
}
for i, c := range cases {
c.want.UnparsedMessage = c.raw
out, err := ParseMessage(c.raw)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(out, c.want) {
t.Errorf("%d. ParseMessage(%q) = %#v; not %#v", i, c.raw, out, c.want)
}
}
}
type dummyRW struct {
r io.Reader
w io.Writer
}
func (rw *dummyRW) Read(p []byte) (n int, err error) {
if rw.r != nil {
return rw.r.Read(p)
}
return 0, errors.Errorf("unimplemented")
}
func (rw *dummyRW) Write(p []byte) (n int, err error) {
if rw.w != nil {
return rw.w.Write(p)
}
return 0, errors.Errorf("unimplemented")
}
func TestRead(t *testing.T) {
var buf bytes.Buffer
buf.WriteString("[00000000011000003A--],,,\"test\"\n")
rw := dummyRW{
r: &buf,
}
ad := New(&rw)
out, err := ad.Read()
if err != nil {
t.Fatal(err)
}
want := Message{
UnparsedMessage: "[00000000011000003A--],,,\"test\"",
AlarmHasOccured: true,
AlarmSounding: true,
Mode: "A",
KeypadMessage: "test",
}
if !reflect.DeepEqual(out, want) {
t.Errorf("got %+v; wanted %+v", out, want)
}
}