mirror of
https://github.com/jwetzell/showbridge-go.git
synced 2026-04-27 05:15:47 +00:00
add tests for freed processors
This commit is contained in:
@@ -19,7 +19,7 @@ func (fe *FreeDEncode) Process(ctx context.Context, wrappedPayload common.Wrappe
|
||||
|
||||
if !ok {
|
||||
wrappedPayload.End = true
|
||||
return wrappedPayload, errors.New("freed.decode processor only accepts a FreeDEncode")
|
||||
return wrappedPayload, errors.New("freed.encode processor only accepts a FreeDPosition")
|
||||
}
|
||||
|
||||
payloadBytes := freeD.Encode(payloadPosition)
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
package processor_test
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
freeD "github.com/jwetzell/free-d-go"
|
||||
"github.com/jwetzell/showbridge-go/internal/common"
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/processor"
|
||||
)
|
||||
@@ -36,3 +39,102 @@ func TestFreeDCreateFromRegistry(t *testing.T) {
|
||||
t.Fatalf("freed.create processor has wrong type: %s", processorInstance.Type())
|
||||
}
|
||||
}
|
||||
|
||||
func TestGoodFreeDCreate(t *testing.T) {
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
expected freeD.FreeDPosition
|
||||
params map[string]any
|
||||
payload any
|
||||
}{
|
||||
{
|
||||
name: "basic freed",
|
||||
params: map[string]any{
|
||||
"id": "{{.Payload.id}}",
|
||||
"pan": "{{.Payload.pan}}",
|
||||
"tilt": "{{.Payload.tilt}}",
|
||||
"roll": "{{.Payload.roll}}",
|
||||
"posX": "{{.Payload.posX}}",
|
||||
"posY": "{{.Payload.posY}}",
|
||||
"posZ": "{{.Payload.posZ}}",
|
||||
"zoom": "{{.Payload.zoom}}",
|
||||
"focus": "{{.Payload.focus}}",
|
||||
},
|
||||
payload: map[string]any{
|
||||
"id": "1",
|
||||
"pan": "180",
|
||||
"tilt": "90",
|
||||
"roll": "-180",
|
||||
"posX": "131069",
|
||||
"posY": "131070",
|
||||
"posZ": "131071",
|
||||
"zoom": "66051",
|
||||
"focus": "263430",
|
||||
},
|
||||
expected: freeD.FreeDPosition{
|
||||
ID: 1,
|
||||
Pan: 180,
|
||||
Tilt: 90,
|
||||
Roll: -180,
|
||||
PosX: 131069,
|
||||
PosY: 131070,
|
||||
PosZ: 131071,
|
||||
Zoom: 66051,
|
||||
Focus: 263430,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
|
||||
registration, ok := processor.ProcessorRegistry["freed.create"]
|
||||
if !ok {
|
||||
t.Fatalf("freed.create processor not registered")
|
||||
}
|
||||
|
||||
processorInstance, err := registration.New(config.ProcessorConfig{
|
||||
Type: "freed.create",
|
||||
Params: test.params,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("freed.create failed to create processor: %s", err)
|
||||
}
|
||||
|
||||
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload))
|
||||
if err != nil {
|
||||
t.Fatalf("freed.create processing failed: %s", err)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(got.Payload, test.expected) {
|
||||
t.Fatalf("freed.create got %+v (%T), expected %+v (%T)", got.Payload, got.Payload, test.expected, test.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestBadFreeDCreate(t *testing.T) {
|
||||
packetEncoder := processor.FreeDCreate{}
|
||||
tests := []struct {
|
||||
name string
|
||||
payload any
|
||||
errorString string
|
||||
}{}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
|
||||
got, err := packetEncoder.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload))
|
||||
|
||||
if err == nil {
|
||||
t.Fatalf("freed.create expected to fail but succeeded, got: %v", got)
|
||||
|
||||
}
|
||||
if err.Error() != test.errorString {
|
||||
t.Fatalf("freed.create got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
package processor_test
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
freeD "github.com/jwetzell/free-d-go"
|
||||
"github.com/jwetzell/showbridge-go/internal/common"
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/processor"
|
||||
)
|
||||
@@ -25,3 +28,77 @@ func TestFreeDDecodeFromRegistry(t *testing.T) {
|
||||
t.Fatalf("freed.decode processor has wrong type: %s", processorInstance.Type())
|
||||
}
|
||||
}
|
||||
|
||||
func TestGoodFreeDDecode(t *testing.T) {
|
||||
packetEncoder := processor.FreeDDecode{}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
payload []byte
|
||||
expected any
|
||||
}{
|
||||
{
|
||||
name: "basic freed",
|
||||
payload: []byte{0xd1, 0x01, 0x5a, 0x00, 0x00, 0x2d, 0x00, 0x00, 0xa6, 0x00, 0x00, 0x7f, 0xff, 0x40, 0x7f, 0xff, 0x80, 0x7f, 0xff,
|
||||
0xc0, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x00, 0x00, 50,
|
||||
},
|
||||
expected: freeD.FreeDPosition{
|
||||
ID: 1,
|
||||
Pan: 180,
|
||||
Tilt: 90,
|
||||
Roll: -180,
|
||||
PosX: 131069,
|
||||
PosY: 131070,
|
||||
PosZ: 131071,
|
||||
Zoom: 66051,
|
||||
Focus: 263430,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
|
||||
got, err := packetEncoder.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload))
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("freed.decode processing failed: %s", err)
|
||||
}
|
||||
|
||||
//TODO(jwetzell): work out better way to compare the any/any
|
||||
if !reflect.DeepEqual(got.Payload, test.expected) {
|
||||
t.Fatalf("freed.decode got %+v (%T), expected %+v (%T)", got.Payload, got.Payload, test.expected, test.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestBadFreeDDecode(t *testing.T) {
|
||||
packetEncoder := processor.FreeDDecode{}
|
||||
tests := []struct {
|
||||
name string
|
||||
payload any
|
||||
errorString string
|
||||
}{
|
||||
{
|
||||
name: "not a byte slice",
|
||||
payload: "test",
|
||||
errorString: "freed.decode processor only accepts a []byte",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
|
||||
got, err := packetEncoder.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload))
|
||||
|
||||
if err == nil {
|
||||
t.Fatalf("freed.decode expected to fail but succeeded, got: %v", got)
|
||||
|
||||
}
|
||||
if err.Error() != test.errorString {
|
||||
t.Fatalf("freed.decode got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
package processor_test
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
freeD "github.com/jwetzell/free-d-go"
|
||||
"github.com/jwetzell/showbridge-go/internal/common"
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/processor"
|
||||
)
|
||||
@@ -25,3 +28,77 @@ func TestFreeDEncodeFromRegistry(t *testing.T) {
|
||||
t.Fatalf("freed.encode processor has wrong type: %s", processorInstance.Type())
|
||||
}
|
||||
}
|
||||
|
||||
func TestGoodFreeDEncode(t *testing.T) {
|
||||
packetEncoder := processor.FreeDEncode{}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
expected []byte
|
||||
payload any
|
||||
}{
|
||||
{
|
||||
name: "basic freed",
|
||||
expected: []byte{0xd1, 0x01, 0x5a, 0x00, 0x00, 0x2d, 0x00, 0x00, 0xa6, 0x00, 0x00, 0x7f, 0xff, 0x40, 0x7f, 0xff, 0x80, 0x7f, 0xff,
|
||||
0xc0, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x00, 0x00, 50,
|
||||
},
|
||||
payload: freeD.FreeDPosition{
|
||||
ID: 1,
|
||||
Pan: 180,
|
||||
Tilt: 90,
|
||||
Roll: -180,
|
||||
PosX: 131069,
|
||||
PosY: 131070,
|
||||
PosZ: 131071,
|
||||
Zoom: 66051,
|
||||
Focus: 263430,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
|
||||
got, err := packetEncoder.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload))
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("freed.encode processing failed: %s", err)
|
||||
}
|
||||
|
||||
//TODO(jwetzell): work out better way to compare the any/any
|
||||
if !reflect.DeepEqual(got.Payload, test.expected) {
|
||||
t.Fatalf("freed.encode got %+v (%T), expected %+v (%T)", got.Payload, got.Payload, test.expected, test.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestBadFreeDEncode(t *testing.T) {
|
||||
packetEncoder := processor.FreeDEncode{}
|
||||
tests := []struct {
|
||||
name string
|
||||
payload any
|
||||
errorString string
|
||||
}{
|
||||
{
|
||||
name: "not a FreeD packet",
|
||||
payload: "test",
|
||||
errorString: "freed.encode processor only accepts a FreeDPosition",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
|
||||
got, err := packetEncoder.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload))
|
||||
|
||||
if err == nil {
|
||||
t.Fatalf("freed.encode expected to fail but succeeded, got: %v", got)
|
||||
|
||||
}
|
||||
if err.Error() != test.errorString {
|
||||
t.Fatalf("freed.encode got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user