mirror of
https://github.com/jwetzell/showbridge-go.git
synced 2026-04-27 05:15:47 +00:00
rework framer tests
This commit is contained in:
15
internal/framer/framer_test.go
Normal file
15
internal/framer/framer_test.go
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
package framer_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/framer"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestNilGetFramer(t *testing.T) {
|
||||||
|
nilFramer := framer.GetFramer("asldfiudchuehrkbjbkjrbb")
|
||||||
|
|
||||||
|
if nilFramer != nil {
|
||||||
|
t.Errorf("Expected nil framer, got %v", nilFramer)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,7 +7,7 @@ import (
|
|||||||
"github.com/jwetzell/showbridge-go/internal/framer"
|
"github.com/jwetzell/showbridge-go/internal/framer"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestGoodRawFramer(t *testing.T) {
|
func TestGoodRawFramerDecode(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
framer framer.Framer
|
framer framer.Framer
|
||||||
@@ -16,7 +16,7 @@ func TestGoodRawFramer(t *testing.T) {
|
|||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "basic raw framer",
|
name: "basic raw framer",
|
||||||
framer: framer.NewRawFramer(),
|
framer: framer.GetFramer("RAW"),
|
||||||
input: []byte("Hello\nWorld\nThis is a test\n"),
|
input: []byte("Hello\nWorld\nThis is a test\n"),
|
||||||
expected: [][]byte{
|
expected: [][]byte{
|
||||||
[]byte("Hello\nWorld\nThis is a test\n"),
|
[]byte("Hello\nWorld\nThis is a test\n"),
|
||||||
@@ -38,3 +38,44 @@ func TestGoodRawFramer(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGoodRawFramerEncode(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
framer framer.Framer
|
||||||
|
expected []byte
|
||||||
|
input []byte
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "basic raw framer",
|
||||||
|
framer: framer.GetFramer("RAW"),
|
||||||
|
expected: []byte("Hello\nWorld\nThis is a test\n"),
|
||||||
|
input: []byte("Hello\nWorld\nThis is a test\n"),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
t.Run(test.name, func(t *testing.T) {
|
||||||
|
frame := test.framer.Encode(test.input)
|
||||||
|
if len(frame) != len(test.expected) {
|
||||||
|
t.Errorf("raw framer got %d frames, expected %d", len(frame), len(test.expected))
|
||||||
|
}
|
||||||
|
if !slices.Equal(frame, test.expected) {
|
||||||
|
t.Errorf("raw frame got %s, expected %s", frame, test.expected)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRawFramerBuffer(t *testing.T) {
|
||||||
|
framer := framer.GetFramer("RAW")
|
||||||
|
framer.Decode([]byte("Hello, World!"))
|
||||||
|
|
||||||
|
if !slices.Equal(framer.Buffer(), []byte{}) {
|
||||||
|
t.Errorf("raw framer buffer got %s, expected empty", framer.Buffer())
|
||||||
|
}
|
||||||
|
framer.Clear()
|
||||||
|
if !slices.Equal(framer.Buffer(), []byte{}) {
|
||||||
|
t.Errorf("raw framer buffer got %s, expected empty after clear", framer.Buffer())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import (
|
|||||||
"github.com/jwetzell/showbridge-go/internal/framer"
|
"github.com/jwetzell/showbridge-go/internal/framer"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestGoodSeparatorFramer(t *testing.T) {
|
func TestGoodSeparatorFramerDecode(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
framer framer.Framer
|
framer framer.Framer
|
||||||
@@ -78,3 +78,53 @@ func TestGoodSeparatorFramer(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGoodSeparatorFramerEncode(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
framer framer.Framer
|
||||||
|
input []byte
|
||||||
|
expected []byte
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "new line separator",
|
||||||
|
framer: framer.GetFramer("LF"),
|
||||||
|
input: []byte("Hello"),
|
||||||
|
expected: []byte("Hello\n"),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "CR separator",
|
||||||
|
framer: framer.GetFramer("CR"),
|
||||||
|
input: []byte("Hello"),
|
||||||
|
expected: []byte("Hello\r"),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "CRLF separator",
|
||||||
|
framer: framer.GetFramer("CRLF"),
|
||||||
|
input: []byte("Hello"),
|
||||||
|
expected: []byte("Hello\r\n"),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
t.Run(test.name, func(t *testing.T) {
|
||||||
|
frame := test.framer.Encode(test.input)
|
||||||
|
if !slices.Equal(frame, test.expected) {
|
||||||
|
t.Errorf("separator framer got %s, expected %s", frame, test.expected)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSeparatorFrameBuffer(t *testing.T) {
|
||||||
|
framer := framer.GetFramer("LF")
|
||||||
|
framer.Decode([]byte("Hello\nWorld\nThis is a test\nextra"))
|
||||||
|
if !slices.Equal(framer.Buffer(), []byte("extra")) {
|
||||||
|
t.Errorf("separator framer buffer got %s, expected %s", framer.Buffer(), []byte("extra"))
|
||||||
|
}
|
||||||
|
|
||||||
|
framer.Clear()
|
||||||
|
if !slices.Equal(framer.Buffer(), []byte{}) {
|
||||||
|
t.Errorf("separator framer buffer got %s, expected empty slice", framer.Buffer())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import (
|
|||||||
"github.com/jwetzell/showbridge-go/internal/framer"
|
"github.com/jwetzell/showbridge-go/internal/framer"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestGoodSLIPFramer(t *testing.T) {
|
func TestGoodSLIPFramerDecode(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
framer framer.Framer
|
framer framer.Framer
|
||||||
@@ -17,13 +17,27 @@ func TestGoodSLIPFramer(t *testing.T) {
|
|||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "OSC SLIP messages",
|
name: "OSC SLIP messages",
|
||||||
framer: framer.NewSlipFramer(),
|
framer: framer.GetFramer("SLIP"),
|
||||||
input: []byte{0xc0, 0x2f, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00, 0x00, 0x2c, 0x00, 0x00, 0x00, 0xc0},
|
input: []byte{0xc0, 0x2f, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00, 0x00, 0x2c, 0x00, 0x00, 0x00, 0xc0},
|
||||||
expected: [][]byte{
|
expected: [][]byte{
|
||||||
{0x2f, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00, 0x00, 0x2c, 0x00, 0x00, 0x00},
|
{0x2f, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00, 0x00, 0x2c, 0x00, 0x00, 0x00},
|
||||||
},
|
},
|
||||||
buffer: []byte{},
|
buffer: []byte{},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "SLIP decode escaped end",
|
||||||
|
framer: framer.GetFramer("SLIP"),
|
||||||
|
expected: [][]byte{{0xc0}},
|
||||||
|
input: []byte{0xc0, 0xdb, 0xdc, 0xc0},
|
||||||
|
buffer: []byte{},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "SLIP decode escaped escape",
|
||||||
|
framer: framer.GetFramer("SLIP"),
|
||||||
|
expected: [][]byte{{0xdb}},
|
||||||
|
input: []byte{0xc0, 0xdb, 0xdd, 0xc0},
|
||||||
|
buffer: []byte{},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
@@ -43,3 +57,54 @@ func TestGoodSLIPFramer(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGoodSLIPFramerEncode(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
framer framer.Framer
|
||||||
|
input []byte
|
||||||
|
expected []byte
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "OSC SLIP messages",
|
||||||
|
framer: framer.GetFramer("SLIP"),
|
||||||
|
input: []byte{
|
||||||
|
0x2f, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00, 0x00, 0x2c, 0x00, 0x00, 0x00,
|
||||||
|
},
|
||||||
|
expected: []byte{0xc0, 0x2f, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00, 0x00, 0x2c, 0x00, 0x00, 0x00, 0xc0},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "SLIP encode end",
|
||||||
|
framer: framer.GetFramer("SLIP"),
|
||||||
|
input: []byte{0xc0},
|
||||||
|
expected: []byte{0xc0, 0xdb, 0xdc, 0xc0},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "SLIP encode esc",
|
||||||
|
framer: framer.GetFramer("SLIP"),
|
||||||
|
input: []byte{0xdb},
|
||||||
|
expected: []byte{0xc0, 0xdb, 0xdd, 0xc0},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
t.Run(test.name, func(t *testing.T) {
|
||||||
|
frame := test.framer.Encode(test.input)
|
||||||
|
if !slices.Equal(frame, test.expected) {
|
||||||
|
t.Errorf("SLIP framer frame got %s, expected %s", frame, test.expected)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSlipFramerBuffer(t *testing.T) {
|
||||||
|
framer := framer.GetFramer("SLIP")
|
||||||
|
framer.Decode([]byte{0xc0, 0x2f, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00, 0x00, 0x2c, 0x00, 0x00, 0x00, 0xc0, 0xc0, 0x45})
|
||||||
|
if !slices.Equal(framer.Buffer(), []byte{0x45}) {
|
||||||
|
t.Errorf("SLIP framer buffer got %s, expected %s", framer.Buffer(), []byte{0x45})
|
||||||
|
}
|
||||||
|
framer.Clear()
|
||||||
|
if !slices.Equal(framer.Buffer(), []byte{}) {
|
||||||
|
t.Errorf("SLIP framer buffer got %s, expected empty slice", framer.Buffer())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user