vail/message_test.go

53 lines
1.0 KiB
Go
Raw Normal View History

package main
import (
"bytes"
"testing"
2020-05-30 22:23:53 -06:00
"time"
)
func TestMessage(t *testing.T) {
2020-05-30 22:23:53 -06:00
m := Message{0x1122334455, []uint8{0xaa, 0xbb, 0xcc}}
m2 := Message{12, []uint8{1}}
2020-05-30 22:23:53 -06:00
if !m.Equal(m) {
t.Error("Equal messages did not compare equal")
}
if m.Equal(m2) {
t.Error("Unequal messages compared equal")
}
2020-05-30 22:23:53 -06:00
if m.Equal(Message{m.Timestamp, []uint8{1, 2, 3}}) {
t.Error("Messages with different payloads compared equal")
}
2020-05-30 22:23:53 -06:00
bm, err := m.MarshalBinary()
if err != nil {
t.Error(err)
}
2020-05-30 22:23:53 -06:00
if !bytes.Equal(bm, []byte("\x00\x00\x00\x11\x22\x33\x44\x55\xaa\xbb\xcc")) {
t.Error("Encoded wrong:", bm)
}
2020-05-30 22:23:53 -06:00
if err := m2.UnmarshalBinary(bm); err != nil {
t.Error(err)
}
2020-05-30 22:23:53 -06:00
if !m.Equal(m2) {
t.Error("Decoded wrong", m2)
}
2020-05-30 22:23:53 -06:00
m3 := NewMessage(
time.Unix(
0,
m.Timestamp*time.Millisecond.Nanoseconds(),
),
[]time.Duration{
time.Duration(m.Duration[0]) * time.Millisecond,
time.Duration(m.Duration[1]) * time.Millisecond,
time.Duration(m.Duration[2]) * time.Millisecond,
},
)
if !m.Equal(m3) {
t.Error("NewMessage didn't work", m, m3)
}
}