move tests to sub folders

This commit is contained in:
Joel Wetzell
2026-02-09 14:38:13 -06:00
parent 3c6d98638e
commit 7015a9d7a2
22 changed files with 0 additions and 0 deletions

View File

@@ -0,0 +1,239 @@
package processor_test
import (
"reflect"
"testing"
"github.com/jwetzell/artnet-go"
"github.com/jwetzell/showbridge-go/internal/config"
"github.com/jwetzell/showbridge-go/internal/processor"
)
func TestArtnetPacketFilterFromRegistry(t *testing.T) {
registration, ok := processor.ProcessorRegistry["artnet.packet.filter"]
if !ok {
t.Fatalf("artnet.packet.filter processor not registered")
}
processorInstance, err := registration.New(config.ProcessorConfig{
Type: "artnet.packet.filter",
Params: map[string]any{
"opCode": float64(artnet.OpTimeCode),
},
})
if err != nil {
t.Fatalf("failed to create artnet.packet.filter processor: %s", err)
}
if processorInstance.Type() != "artnet.packet.filter" {
t.Fatalf("artnet.packet.filter processor has wrong type: %s", processorInstance.Type())
}
payload := &artnet.ArtTimeCode{
ID: []byte{'A', 'r', 't', '-', 'N', 'e', 't', 0x00},
OpCode: artnet.OpTimeCode,
ProtVerHi: 0,
ProtVerLo: 14,
Filler1: 0,
StreamId: 0,
Frames: 11,
Seconds: 17,
Minutes: 3,
Hours: 0,
Type: 0,
}
expected := &artnet.ArtTimeCode{
ID: []byte{'A', 'r', 't', '-', 'N', 'e', 't', 0x00},
OpCode: artnet.OpTimeCode,
ProtVerHi: 0,
ProtVerLo: 14,
Filler1: 0,
StreamId: 0,
Frames: 11,
Seconds: 17,
Minutes: 3,
Hours: 0,
Type: 0,
}
got, err := processorInstance.Process(t.Context(), payload)
if err != nil {
t.Fatalf("artnet.packet.filter processing failed: %s", err)
}
if !reflect.DeepEqual(got, expected) {
t.Fatalf("artnet.packet.filter got %+v, expected %+v", got, expected)
}
}
func TestGoodArtnetPacketFilter(t *testing.T) {
tests := []struct {
name string
payload any
opCode uint16
expected artnet.ArtNetPacket
}{
{
name: "tiemcode packet with matching opCode",
payload: &artnet.ArtTimeCode{
ID: []byte{'A', 'r', 't', '-', 'N', 'e', 't', 0x00},
OpCode: artnet.OpTimeCode,
ProtVerHi: 0,
ProtVerLo: 14,
Filler1: 0,
StreamId: 0,
Frames: 11,
Seconds: 17,
Minutes: 3,
Hours: 0,
Type: 0,
},
opCode: artnet.OpTimeCode,
expected: &artnet.ArtTimeCode{
ID: []byte{'A', 'r', 't', '-', 'N', 'e', 't', 0x00},
OpCode: artnet.OpTimeCode,
ProtVerHi: 0,
ProtVerLo: 14,
Filler1: 0,
StreamId: 0,
Frames: 11,
Seconds: 17,
Minutes: 3,
Hours: 0,
Type: 0,
},
},
{
name: "tiemcode packet with mismatching opCode",
payload: &artnet.ArtTimeCode{
ID: []byte{'A', 'r', 't', '-', 'N', 'e', 't', 0x00},
OpCode: artnet.OpTimeCode,
ProtVerHi: 0,
ProtVerLo: 14,
Filler1: 0,
StreamId: 0,
Frames: 11,
Seconds: 17,
Minutes: 3,
Hours: 0,
Type: 0,
},
opCode: artnet.OpDmx,
expected: nil,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
artnetPacketFilter := processor.ArtNetPacketFilter{
OpCode: test.opCode,
}
got, err := artnetPacketFilter.Process(t.Context(), test.payload)
if err != nil {
t.Fatalf("artnet.packet.filter failed: %s", err)
}
if test.expected == nil {
if got != nil {
t.Fatalf("artnet.packet.filter got %+v, expected nil", got)
}
return
}
gotPacket, ok := got.(artnet.ArtNetPacket)
if !ok {
t.Fatalf("artnet.packet.filter returned a %T payload: %s", got, got)
}
if !reflect.DeepEqual(gotPacket, test.expected) {
t.Fatalf("artnet.packet.filter got %+v, expected %+v", gotPacket, test.expected)
}
})
}
}
func TestBadArtnetPacketFilter(t *testing.T) {
tests := []struct {
name string
payload any
params map[string]any
errorString string
}{
{
name: "non-artnet input",
payload: []byte{0x01},
params: map[string]any{"opCode": float64(artnet.OpTimeCode)},
errorString: "artnet.packet.filter processor only accepts an ArtNetPacket",
},
{
name: "no opCode param",
payload: &artnet.ArtTimeCode{
ID: []byte{'A', 'r', 't', '-', 'N', 'e', 't', 0x00},
OpCode: artnet.OpTimeCode,
ProtVerHi: 0,
ProtVerLo: 14,
Filler1: 0,
StreamId: 0,
Frames: 11,
Seconds: 17,
Minutes: 3,
Hours: 0,
Type: 0,
},
params: map[string]any{},
errorString: "artnet.packet.filter requires an opCode parameter",
},
{
name: "opCode not a number",
payload: &artnet.ArtTimeCode{
ID: []byte{'A', 'r', 't', '-', 'N', 'e', 't', 0x00},
OpCode: artnet.OpTimeCode,
ProtVerHi: 0,
ProtVerLo: 14,
Filler1: 0,
StreamId: 0,
Frames: 11,
Seconds: 17,
Minutes: 3,
Hours: 0,
Type: 0,
},
params: map[string]any{"opCode": "100"},
errorString: "artnet.packet.filter opCode must be a number",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
registration, ok := processor.ProcessorRegistry["artnet.packet.filter"]
if !ok {
t.Fatalf("artnet.packet.filter processor not registered")
}
processorInstance, err := registration.New(config.ProcessorConfig{
Type: "artnet.packet.filter",
Params: test.params,
})
if err != nil {
if test.errorString != err.Error() {
t.Fatalf("artnet.packet.filter got error '%s', expected '%s'", err.Error(), test.errorString)
}
return
}
got, err := processorInstance.Process(t.Context(), test.payload)
if err == nil {
t.Fatalf("artnet.packet.filter expected to fail but got payload: %s", got)
}
if err.Error() != test.errorString {
t.Fatalf("artnet.packet.filter got error '%s', expected '%s'", err.Error(), test.errorString)
}
})
}
}

View File

@@ -0,0 +1,39 @@
package processor_test
import (
"testing"
"github.com/jwetzell/showbridge-go/internal/config"
"github.com/jwetzell/showbridge-go/internal/processor"
)
func TestDebugLogFromRegistry(t *testing.T) {
registration, ok := processor.ProcessorRegistry["debug.log"]
if !ok {
t.Fatalf("debug.log processor not registered")
}
processorInstance, err := registration.New(config.ProcessorConfig{
Type: "debug.log",
})
if err != nil {
t.Fatalf("failed to create debug.log processor: %s", err)
}
if processorInstance.Type() != "debug.log" {
t.Fatalf("debug.log processor has wrong type: %s", processorInstance.Type())
}
payload := "test"
expected := "test"
got, err := processorInstance.Process(t.Context(), payload)
if err != nil {
t.Fatalf("debug.log processing failed: %s", err)
}
if got != expected {
t.Fatalf("debug.log got %+v, expected %+v", got, expected)
}
}

View File

@@ -0,0 +1,173 @@
package processor_test
import (
"testing"
"github.com/jwetzell/showbridge-go/internal/config"
"github.com/jwetzell/showbridge-go/internal/processor"
)
func TestFloatParseFromRegistry(t *testing.T) {
registration, ok := processor.ProcessorRegistry["float.parse"]
if !ok {
t.Fatalf("float.parse processor not registered")
}
processorInstance, err := registration.New(config.ProcessorConfig{
Type: "float.parse",
})
if err != nil {
t.Fatalf("failed to create float.parse processor: %s", err)
}
if processorInstance.Type() != "float.parse" {
t.Fatalf("float.parse processor has wrong type: %s", processorInstance.Type())
}
}
func TestFloatParseBadConfigBitSizeString(t *testing.T) {
registration, ok := processor.ProcessorRegistry["float.parse"]
if !ok {
t.Fatalf("float.parse processor not registered")
}
_, err := registration.New(config.ProcessorConfig{
Type: "float.parse",
Params: map[string]any{
"bitSize": "64",
},
})
if err == nil {
t.Fatalf("float.parse should have returned an error for bad bitSize config")
}
}
func TestFloatParseGoodConfig(t *testing.T) {
registration, ok := processor.ProcessorRegistry["float.parse"]
if !ok {
t.Fatalf("float.parse processor not registered")
}
processorInstance, err := registration.New(config.ProcessorConfig{
Type: "float.parse",
Params: map[string]any{
"bitSize": 64.0,
},
})
if err != nil {
t.Fatalf("float.parse should have created processor but got error: %s", err)
}
payload := "12345.0"
expected := float64(12345.0)
got, err := processorInstance.Process(t.Context(), payload)
if err != nil {
t.Fatalf("float.parse processing failed: %s", err)
}
gotFloat, ok := got.(float64)
if !ok {
t.Fatalf("float.parse returned a %T payload: %s", got, got)
}
if gotFloat != expected {
t.Fatalf("float.parse got %f, expected %f", gotFloat, expected)
}
}
func TestGoodFloatParse(t *testing.T) {
floatParser := processor.FloatParse{}
tests := []struct {
processor processor.Processor
name string
payload any
bitSize int
expected float64
}{
{
name: "positive number",
payload: "12345.67",
bitSize: 64,
expected: 12345.67,
},
{
name: "negative number",
payload: "-12345.67",
bitSize: 64,
expected: -12345.67,
},
{
name: "zero",
payload: "0",
bitSize: 64,
expected: 0,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got, err := floatParser.Process(t.Context(), test.payload)
gotFloat, ok := got.(float64)
if !ok {
t.Fatalf("float.parse returned a %T payload: %s", got, got)
}
if err != nil {
t.Fatalf("float.parse failed: %s", err)
}
if gotFloat != test.expected {
t.Fatalf("float.parse got %f, expected %f", gotFloat, test.expected)
}
})
}
}
func TestBadFloatParse(t *testing.T) {
tests := []struct {
processor processor.Processor
name string
payload any
bitSize int
errorString string
}{
{
name: "non-string input",
payload: []byte{0x01},
bitSize: 64,
errorString: "float.parse processor only accepts a string",
},
{
name: "not float string",
payload: "abcd",
bitSize: 64,
errorString: "strconv.ParseFloat: parsing \"abcd\": invalid syntax",
},
{
name: "bit size overflow",
payload: "1.79e+64",
bitSize: 32,
errorString: "strconv.ParseFloat: parsing \"1.79e+64\": value out of range",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
floatParser := processor.FloatParse{
BitSize: test.bitSize,
}
got, err := floatParser.Process(t.Context(), test.payload)
if err == nil {
t.Fatalf("float.parse expected to fail but succeeded, got: %v", got)
}
if err.Error() != test.errorString {
t.Fatalf("float.parse got error '%s', expected '%s'", err.Error(), test.errorString)
}
})
}
}

View File

@@ -0,0 +1,219 @@
package processor_test
import (
"testing"
"github.com/jwetzell/showbridge-go/internal/config"
"github.com/jwetzell/showbridge-go/internal/processor"
)
func TestIntParseFromRegistry(t *testing.T) {
registration, ok := processor.ProcessorRegistry["int.parse"]
if !ok {
t.Fatalf("int.parse processor not registered")
}
processorInstance, err := registration.New(config.ProcessorConfig{
Type: "int.parse",
})
if err != nil {
t.Fatalf("failed to create int.parse processor: %s", err)
}
if processorInstance.Type() != "int.parse" {
t.Fatalf("int.parse processor has wrong type: %s", processorInstance.Type())
}
}
func TestIntParseBadConfigBaseString(t *testing.T) {
registration, ok := processor.ProcessorRegistry["int.parse"]
if !ok {
t.Fatalf("int.parse processor not registered")
}
_, err := registration.New(config.ProcessorConfig{
Type: "int.parse",
Params: map[string]any{
"base": "10",
},
})
if err == nil {
t.Fatalf("int.parse should have returned an error for bad base config")
}
}
func TestIntParseBadConfigBitSizeString(t *testing.T) {
registration, ok := processor.ProcessorRegistry["int.parse"]
if !ok {
t.Fatalf("int.parse processor not registered")
}
_, err := registration.New(config.ProcessorConfig{
Type: "int.parse",
Params: map[string]any{
"bitSize": "64",
},
})
if err == nil {
t.Fatalf("int.parse should have returned an error for bad bitSize config")
}
}
func TestIntParseGoodConfig(t *testing.T) {
registration, ok := processor.ProcessorRegistry["int.parse"]
if !ok {
t.Fatalf("int.parse processor not registered")
}
processorInstance, err := registration.New(config.ProcessorConfig{
Type: "int.parse",
Params: map[string]any{
"base": 10.0,
"bitSize": 64.0,
},
})
if err != nil {
t.Fatalf("int.parse should have created processor but got error: %s", err)
}
payload := "12345"
expected := int64(12345)
got, err := processorInstance.Process(t.Context(), payload)
if err != nil {
t.Fatalf("int.parse processing failed: %s", err)
}
gotInt, ok := got.(int64)
if !ok {
t.Fatalf("int.parse returned a %T payload: %s", got, got)
}
if gotInt != expected {
t.Fatalf("int.parse got %d, expected %d", gotInt, expected)
}
}
func TestGoodIntParse(t *testing.T) {
tests := []struct {
processor processor.Processor
name string
payload any
expected int64
base int
bitSize int
}{
{
name: "positive number",
payload: "12345",
expected: 12345,
base: 10,
bitSize: 64,
},
{
name: "negative number",
payload: "-12345",
expected: -12345,
base: 10,
bitSize: 64,
},
{
name: "zero",
payload: "0",
expected: 0,
base: 10,
bitSize: 64,
},
{
name: "binary",
payload: "1010101",
expected: 85,
base: 2,
bitSize: 64,
},
{
name: "hex",
payload: "15F",
expected: 351,
base: 16,
bitSize: 64,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
intParser := processor.IntParse{
Base: test.base,
BitSize: test.bitSize,
}
got, err := intParser.Process(t.Context(), test.payload)
gotInt, ok := got.(int64)
if !ok {
t.Fatalf("int.parse returned a %T payload: %s", got, got)
}
if err != nil {
t.Fatalf("int.parse failed: %s", err)
}
if gotInt != test.expected {
t.Fatalf("int.parse got %d, expected %d", gotInt, test.expected)
}
})
}
}
func TestBadIntParse(t *testing.T) {
tests := []struct {
processor processor.Processor
name string
payload any
base int
bitSize int
errorString string
}{
{
name: "non-string input",
payload: []byte{0x01},
base: 10,
bitSize: 64,
errorString: "int.parse processor only accepts a string",
},
{
name: "not int string",
payload: "123.46",
base: 10,
bitSize: 64,
errorString: "strconv.ParseInt: parsing \"123.46\": invalid syntax",
},
{
name: "bit overflow",
payload: "12345678901234567890",
base: 10,
bitSize: 32,
errorString: "strconv.ParseInt: parsing \"12345678901234567890\": value out of range",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
intParser := processor.IntParse{
Base: test.base,
BitSize: test.bitSize,
}
got, err := intParser.Process(t.Context(), test.payload)
if err == nil {
t.Fatalf("int.parse expected to fail but succeeded, got: %v", got)
}
if err.Error() != test.errorString {
t.Fatalf("int.parse got error '%s', expected '%s'", err.Error(), test.errorString)
}
})
}
}

View File

@@ -0,0 +1,175 @@
package processor_test
import (
"testing"
"github.com/jwetzell/showbridge-go/internal/config"
"github.com/jwetzell/showbridge-go/internal/processor"
)
func TestIntRandomFromRegistry(t *testing.T) {
registration, ok := processor.ProcessorRegistry["int.random"]
if !ok {
t.Fatalf("int.random processor not registered")
}
processorInstance, err := registration.New(config.ProcessorConfig{
Type: "int.random",
Params: map[string]any{
"min": 1.0,
"max": 10.0,
},
})
if err != nil {
t.Fatalf("failed to create int.random processor: %s", err)
}
if processorInstance.Type() != "int.random" {
t.Fatalf("int.random processor has wrong type: %s", processorInstance.Type())
}
}
func TestIntRandomGoodConfig(t *testing.T) {
registration, ok := processor.ProcessorRegistry["int.random"]
if !ok {
t.Fatalf("int.random processor not registered")
}
processorInstance, err := registration.New(config.ProcessorConfig{
Type: "int.random",
Params: map[string]any{
"min": 1.0,
"max": 10.0,
},
})
if err != nil {
t.Fatalf("int.random should have created processor but got error: %s", err)
}
payload := "12345"
got, err := processorInstance.Process(t.Context(), payload)
if err != nil {
t.Fatalf("int.random processing failed: %s", err)
}
gotInt, ok := got.(int)
if !ok {
t.Fatalf("int.random returned a %T payload: %s", got, got)
}
if gotInt < 1 || gotInt > 10 {
t.Fatalf("int.random got %d, expected between %d and %d", gotInt, 1, 10)
}
}
func TestGoodIntRandom(t *testing.T) {
tests := []struct {
processor processor.Processor
name string
payload any
min int
max int
}{
{
name: "1-10",
payload: "12345",
min: 1,
max: 10,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
intRandom := processor.IntRandom{
Min: test.min,
Max: test.max,
}
got, err := intRandom.Process(t.Context(), test.payload)
gotInt, ok := got.(int)
if !ok {
t.Fatalf("int.random returned a %T payload: %s", got, got)
}
if err != nil {
t.Fatalf("int.random failed: %s", err)
}
if gotInt < test.min || gotInt > test.max {
t.Fatalf("int.random got %d, expected between %d and %d", gotInt, test.min, test.max)
}
})
}
}
func TestBadIntRandom(t *testing.T) {
tests := []struct {
name string
params map[string]any
payload any
errorString string
}{
{
name: "no min param",
payload: "hello",
params: map[string]any{"max": 10.0},
errorString: "int.random requires a min parameter",
},
{
name: "no max param",
payload: "hello",
params: map[string]any{"min": 1.0},
errorString: "int.random requires a max parameter",
},
{
name: "min param not a number",
payload: "hello",
params: map[string]any{"min": "1", "max": 10.0},
errorString: "int.random min must be a number",
},
{
name: "max param not a number",
payload: "hello",
params: map[string]any{"min": 1.0, "max": "10"},
errorString: "int.random max must be a number",
},
{
name: "max less than min",
payload: "hello",
params: map[string]any{"min": 1.0, "max": 0.0},
errorString: "int.random max must be greater than min",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
registration, ok := processor.ProcessorRegistry["int.random"]
if !ok {
t.Fatalf("int.random processor not registered")
}
processorInstance, err := registration.New(config.ProcessorConfig{
Type: "int.random",
Params: test.params,
})
if err != nil {
if test.errorString != err.Error() {
t.Fatalf("int.random got error '%s', expected '%s'", err.Error(), test.errorString)
}
return
}
got, err := processorInstance.Process(t.Context(), test.payload)
if err == nil {
t.Fatalf("int.random expected to fail but got payload: %s", got)
}
if err.Error() != test.errorString {
t.Fatalf("int.random got error '%s', expected '%s'", err.Error(), test.errorString)
}
})
}
}

View File

@@ -0,0 +1,91 @@
package processor_test
import (
"reflect"
"testing"
"github.com/jwetzell/showbridge-go/internal/config"
"github.com/jwetzell/showbridge-go/internal/processor"
)
func TestJsonDecodeFromRegistry(t *testing.T) {
registration, ok := processor.ProcessorRegistry["json.decode"]
if !ok {
t.Fatalf("json.decode processor not registered")
}
processorInstance, err := registration.New(config.ProcessorConfig{
Type: "json.decode",
})
if err != nil {
t.Fatalf("failed to create json.decode processor: %s", err)
}
if processorInstance.Type() != "json.decode" {
t.Fatalf("json.decode processor has wrong type: %s", processorInstance.Type())
}
payload := "{\"property\":\"hello\"}"
expected := map[string]any{
"property": "hello",
}
got, err := processorInstance.Process(t.Context(), payload)
if err != nil {
t.Fatalf("json.decode processing failed: %s", err)
}
gotMap, ok := got.(map[string]any)
if !ok {
t.Fatalf("json.decode should return byte slice")
}
if !reflect.DeepEqual(gotMap, expected) {
t.Fatalf("json.decode got %+v, expected %+v", got, expected)
}
}
func TestGoodJsonDecode(t *testing.T) {
jsonDecoder := processor.JsonDecode{}
tests := []struct {
name string
payload string
expected map[string]any
}{
{
name: "basic json",
payload: "{\"address\":\"/hello\",\"args\":null}",
expected: map[string]any{
"address": "/hello",
"args": nil,
},
},
{
name: "array",
payload: "{\"address\":\"/hello\",\"args\":[1,2,3]}",
expected: map[string]any{
"address": "/hello",
"args": []any{1.0, 2.0, 3.0},
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got, err := jsonDecoder.Process(t.Context(), test.payload)
gotMap, ok := got.(map[string]any)
if !ok {
t.Fatalf("json.decode returned a %T payload: %s", got, got)
}
if err != nil {
t.Fatalf("json.decode failed: %s", err)
}
if !reflect.DeepEqual(gotMap, test.expected) {
t.Fatalf("json.decode got %x, expected %s", got, test.expected)
}
})
}
}

View File

@@ -0,0 +1,85 @@
package processor_test
import (
"slices"
"testing"
"github.com/jwetzell/osc-go"
"github.com/jwetzell/showbridge-go/internal/config"
"github.com/jwetzell/showbridge-go/internal/processor"
)
func TestJsonEncodeFromRegistry(t *testing.T) {
registration, ok := processor.ProcessorRegistry["json.encode"]
if !ok {
t.Fatalf("json.encode processor not registered")
}
processorInstance, err := registration.New(config.ProcessorConfig{
Type: "json.encode",
})
if err != nil {
t.Fatalf("failed to create json.encode processor: %s", err)
}
if processorInstance.Type() != "json.encode" {
t.Fatalf("json.encode processor has wrong type: %s", processorInstance.Type())
}
payload := struct {
Property string `json:"property"`
}{
Property: "hello",
}
expected := []byte("{\"property\":\"hello\"}")
got, err := processorInstance.Process(t.Context(), payload)
if err != nil {
t.Fatalf("json.encode processing failed: %s", err)
}
gotBytes, ok := got.([]byte)
if !ok {
t.Fatalf("json.encode should return byte slice")
}
if !slices.Equal(gotBytes, expected) {
t.Fatalf("json.encode got %+v, expected %+v", got, expected)
}
}
func TestGoodJsonEncode(t *testing.T) {
jsonEncoder := processor.JsonEncode{}
tests := []struct {
name string
payload any
expected []byte
}{
{
name: "basic struct",
payload: osc.OSCMessage{
Address: "/hello",
},
expected: []byte("{\"address\":\"/hello\",\"args\":null}"),
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got, err := jsonEncoder.Process(t.Context(), test.payload)
gotBytes, ok := got.([]byte)
if !ok {
t.Fatalf("json.encode returned a %T payload: %s", got, got)
}
if err != nil {
t.Fatalf("json.encode failed: %s", err)
}
if !slices.Equal(gotBytes, test.expected) {
t.Fatalf("json.encode got %x, expected %s", got, test.expected)
}
})
}
}

View File

@@ -0,0 +1,62 @@
package processor_test
import (
"context"
"testing"
"github.com/jwetzell/showbridge-go/internal/config"
"github.com/jwetzell/showbridge-go/internal/processor"
)
type TestProcessor struct {
}
func (p *TestProcessor) Type() string {
return "test"
}
func (p *TestProcessor) Process(ctx context.Context, input any) (any, error) {
return input, nil
}
func TestProcessorBadRegistrationNoType(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Fatalf("processor registration should have panicked but did not")
}
}()
processor.RegisterProcessor(processor.ProcessorRegistration{
Type: "",
New: func(config config.ProcessorConfig) (processor.Processor, error) {
return &TestProcessor{}, nil
},
})
}
func TestProcessorBadRegistrationNoNew(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Fatalf("processor registration should have panicked but did not")
}
}()
processor.RegisterProcessor(processor.ProcessorRegistration{
Type: "test",
New: nil,
})
}
func TestProcessorBadRegistrationExistingType(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Fatalf("processor registration should have panicked but did not")
}
}()
processor.RegisterProcessor(processor.ProcessorRegistration{
Type: "string.create",
New: func(config config.ProcessorConfig) (processor.Processor, error) {
return &TestProcessor{}, nil
},
})
}

View File

@@ -0,0 +1,171 @@
package processor_test
import (
"testing"
"github.com/expr-lang/expr"
"github.com/jwetzell/showbridge-go/internal/config"
"github.com/jwetzell/showbridge-go/internal/processor"
)
func TestScriptExprFromRegistry(t *testing.T) {
registration, ok := processor.ProcessorRegistry["script.expr"]
if !ok {
t.Fatalf("script.expr processor not registered")
}
processorInstance, err := registration.New(config.ProcessorConfig{
Type: "script.expr",
Params: map[string]any{
"expression": "foo + bar",
},
})
if err != nil {
t.Fatalf("failed to create script.expr processor: %s", err)
}
if processorInstance.Type() != "script.expr" {
t.Fatalf("script.expr processor has wrong type: %s", processorInstance.Type())
}
}
func TestScriptExprNoProgram(t *testing.T) {
registration, ok := processor.ProcessorRegistry["script.expr"]
if !ok {
t.Fatalf("script.expr processor not registered")
}
_, err := registration.New(config.ProcessorConfig{
Type: "script.expr",
Params: map[string]any{},
})
if err == nil {
t.Fatalf("script.expr processor should have thrown an error when creating")
}
}
func TestScriptExprBadConfigWrongExpressionType(t *testing.T) {
registration, ok := processor.ProcessorRegistry["script.expr"]
if !ok {
t.Fatalf("script.expr processor not registered")
}
_, err := registration.New(config.ProcessorConfig{
Type: "script.expr",
Params: map[string]any{
"expression": 12345,
},
})
if err == nil {
t.Fatalf("script.expr processor should have thrown an error when creating with non-string expression")
}
}
func TestScriptExprBadConfigNonCompilingExpression(t *testing.T) {
registration, ok := processor.ProcessorRegistry["script.expr"]
if !ok {
t.Fatalf("script.expr processor not registered")
}
_, err := registration.New(config.ProcessorConfig{
Type: "script.expr",
Params: map[string]any{
"expression": "foo + ",
},
})
if err == nil {
t.Fatalf("script.expr processor should have thrown an error when creating with non-compiling expression")
}
}
func TestGoodScriptExpr(t *testing.T) {
tests := []struct {
program string
name string
payload map[string]any
expected any
}{
{
program: "foo + bar",
name: "number",
payload: map[string]any{
"foo": 1,
"bar": 1,
},
expected: 2,
},
{
program: "foo + bar",
name: "string",
payload: map[string]any{
"foo": "1",
"bar": "1",
},
expected: "11",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
program, err := expr.Compile(test.program)
if err != nil {
t.Fatalf("script.expr failed to compile program: %s", err)
}
exprProcessor := &processor.ScriptExpr{Program: program}
got, err := exprProcessor.Process(t.Context(), test.payload)
if err != nil {
t.Fatalf("script.expr failed: %s", err)
}
//TODO(jwetzell): work out better way to compare the any/any
if got != test.expected {
t.Fatalf("script.expr got %+v (%T), expected %+v (%T)", got, got, test.expected, test.expected)
}
})
}
}
func TestBadScriptExpr(t *testing.T) {
tests := []struct {
program string
name string
payload map[string]any
errorString string
}{
{
name: "accessing missing field",
program: "foo + bar",
payload: map[string]any{
"foo": 1,
},
errorString: "invalid operation: int + <nil> (1:5)\n | foo + bar\n | ....^",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
program, err := expr.Compile(test.program)
if err != nil {
t.Fatalf("script.expr failed to compile program: %s", err)
}
exprProcessor := &processor.ScriptExpr{Program: program}
got, err := exprProcessor.Process(t.Context(), test.payload)
if err == nil {
t.Fatalf("script.expr expected to fail but succeeded, got: %v", got)
}
if err.Error() != test.errorString {
t.Fatalf("script.expr got error '%s', expected '%s'", err.Error(), test.errorString)
}
})
}
}

View File

@@ -0,0 +1,179 @@
package processor_test
import (
"maps"
"testing"
"github.com/jwetzell/showbridge-go/internal/config"
"github.com/jwetzell/showbridge-go/internal/processor"
)
func TestScriptJSFromRegistry(t *testing.T) {
registration, ok := processor.ProcessorRegistry["script.js"]
if !ok {
t.Fatalf("script.js processor not registered")
}
processorInstance, err := registration.New(config.ProcessorConfig{
Type: "script.js",
Params: map[string]any{
"program": `
payload = payload + 1
`,
},
})
if err != nil {
t.Fatalf("failed to create script.js processor: %s", err)
}
if processorInstance.Type() != "script.js" {
t.Fatalf("script.js processor has wrong type: %s", processorInstance.Type())
}
payload := 1
expected := 2
got, err := processorInstance.Process(t.Context(), payload)
if err != nil {
t.Fatalf("script.js processing failed: %s", err)
}
if got != expected {
t.Fatalf("script.js got %+v, expected %+v", got, expected)
}
}
func TestScriptJSNoProgram(t *testing.T) {
registration, ok := processor.ProcessorRegistry["script.js"]
if !ok {
t.Fatalf("script.js processor not registered")
}
_, err := registration.New(config.ProcessorConfig{
Type: "script.js",
Params: map[string]any{},
})
if err == nil {
t.Fatalf("script.js processor should have thrown an error when creating")
}
}
func TestScriptJSBadConfigWrongProgramType(t *testing.T) {
registration, ok := processor.ProcessorRegistry["script.js"]
if !ok {
t.Fatalf("script.js processor not registered")
}
_, err := registration.New(config.ProcessorConfig{
Type: "script.js",
Params: map[string]any{
"program": 12345,
},
})
if err == nil {
t.Fatalf("script.js processor should have thrown an error when creating with non-string program")
}
}
func TestGoodScriptJS(t *testing.T) {
tests := []struct {
processor processor.Processor
name string
payload any
expected any
}{
{
name: "number",
processor: &processor.ScriptJS{Program: `
payload = payload + 1
`},
payload: 1,
expected: 2,
},
{
name: "string",
processor: &processor.ScriptJS{Program: `
payload = payload + "1"
`},
payload: "1",
expected: "11",
},
{
name: "object",
processor: &processor.ScriptJS{Program: `
payload = { key: payload }
`},
payload: "1",
expected: map[string]any{"key": "1"},
},
{
name: "nil",
processor: &processor.ScriptJS{Program: `
payload = undefined
`},
payload: "1",
expected: nil,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got, err := test.processor.Process(t.Context(), test.payload)
if err != nil {
t.Fatalf("script.js process failed: %s", err)
}
//TODO(jwetzell): work out better way to compare the any/any
gotMap, ok := got.(map[string]interface{})
if ok {
// got a map
expectedMap, ok := test.expected.(map[string]interface{})
if ok {
if !maps.Equal(gotMap, expectedMap) {
t.Fatalf("script.js got %+v, expected %+v", got, test.expected)
}
} else {
t.Fatalf("script.js got %+v, expected %+v", got, test.expected)
}
} else {
if got != test.expected {
t.Fatalf("script.js got %+v, expected %+v", got, test.expected)
}
}
})
}
}
func TestBadScriptJS(t *testing.T) {
tests := []struct {
name string
processor processor.Processor
payload any
errorString string
}{
{
name: "accessing not defined variable",
processor: &processor.ScriptJS{Program: `paylod = foo`},
payload: 0,
errorString: "ReferenceError: 'foo' is not defined",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got, err := test.processor.Process(t.Context(), test.payload)
if err == nil {
t.Fatalf("script.js expected to fail but succeeded, got: %v", got)
}
if err.Error() != test.errorString {
t.Fatalf("script.js got error '%s', expected '%s'", err.Error(), test.errorString)
}
})
}
}

View File

@@ -0,0 +1,187 @@
package processor_test
import (
"testing"
"text/template"
"github.com/jwetzell/showbridge-go/internal/config"
"github.com/jwetzell/showbridge-go/internal/processor"
)
type TestStruct struct {
Data string
}
func (t TestStruct) GetData() string {
return t.Data
}
func TestStringCreateFromRegistry(t *testing.T) {
registration, ok := processor.ProcessorRegistry["string.create"]
if !ok {
t.Fatalf("string.create processor not registered")
}
processorInstance, err := registration.New(config.ProcessorConfig{
Type: "string.create",
Params: map[string]any{
"template": "{{.}}",
},
})
if err != nil {
t.Fatalf("failed to create string.create processor: %s", err)
}
if processorInstance.Type() != "string.create" {
t.Fatalf("string.create processor has wrong type: %s", processorInstance.Type())
}
payload := "hello"
expected := "hello"
got, err := processorInstance.Process(t.Context(), payload)
if err != nil {
t.Fatalf("string.create processing failed: %s", err)
}
if got != expected {
t.Fatalf("string.create got %+v, expected %+v", got, expected)
}
}
func TestGoodStringCreate(t *testing.T) {
tests := []struct {
name string
template string
payload any
expected string
}{
{
name: "string payload",
template: "{{.}}",
payload: "hello",
expected: "hello",
},
{
name: "number payload",
template: "{{.}}",
payload: 4,
expected: "4",
},
{
name: "boolean payload",
template: "{{.}}",
payload: true,
expected: "true",
},
{
name: "struct payload - field",
template: "{{.Data}}",
payload: TestStruct{Data: "test"},
expected: "test",
},
{
name: "struct payload - method",
template: "{{.GetData}}",
payload: TestStruct{Data: "test"},
expected: "test",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
template, err := template.New("template").Parse(test.template)
if err != nil {
t.Fatalf("string.create template parsing failed: %s", err)
}
processor := &processor.StringCreate{Template: template}
got, err := processor.Process(t.Context(), test.payload)
gotStrings, ok := got.(string)
if !ok {
t.Fatalf("string.create returned a %T payload: %s", got, got)
}
if err != nil {
t.Fatalf("string.create failed: %s", err)
}
if gotStrings != test.expected {
t.Fatalf("string.create got %s, expected %s", got, test.expected)
}
})
}
}
func TestBadStringCreate(t *testing.T) {
tests := []struct {
name string
params map[string]any
payload any
errorString string
}{
{
name: "no template param",
payload: "hello",
params: map[string]any{},
errorString: "string.create requires a template parameter",
},
{
name: "non string template",
payload: "hello",
params: map[string]any{
"template": 1,
},
errorString: "string.create template must be a string",
},
{
name: "invalid template",
payload: "hello",
params: map[string]any{
"template": "{{.",
},
errorString: "template: template:1: illegal number syntax: \".\"",
},
{
name: "bad property in template",
payload: "hello",
params: map[string]any{
"template": "{{.Invalid}}",
},
errorString: "template: template:1:2: executing \"template\" at <.Invalid>: can't evaluate field Invalid in type string",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
registration, ok := processor.ProcessorRegistry["string.create"]
if !ok {
t.Fatalf("string.create processor not registered")
}
processorInstance, err := registration.New(config.ProcessorConfig{
Type: "string.create",
Params: test.params,
})
if err != nil {
if test.errorString != err.Error() {
t.Fatalf("string.create got error '%s', expected '%s'", err.Error(), test.errorString)
}
return
}
got, err := processorInstance.Process(t.Context(), test.payload)
if err == nil {
t.Fatalf("string.create expected to fail but got payload: %s", got)
}
if err.Error() != test.errorString {
t.Fatalf("string.create got error '%s', expected '%s'", err.Error(), test.errorString)
}
})
}
}

View File

@@ -0,0 +1,98 @@
package processor_test
import (
"testing"
"github.com/jwetzell/showbridge-go/internal/config"
"github.com/jwetzell/showbridge-go/internal/processor"
)
func TestStringDecodeFromRegistry(t *testing.T) {
registration, ok := processor.ProcessorRegistry["string.decode"]
if !ok {
t.Fatalf("string.decode processor not registered")
}
processorInstance, err := registration.New(config.ProcessorConfig{
Type: "string.decode",
})
if err != nil {
t.Fatalf("failed to create string.decode processor: %s", err)
}
if processorInstance.Type() != "string.decode" {
t.Fatalf("string.decode processor has wrong type: %s", processorInstance.Type())
}
payload := []byte{'h', 'e', 'l', 'l', 'o'}
expected := "hello"
got, err := processorInstance.Process(t.Context(), payload)
if err != nil {
t.Fatalf("string.decode processing failed: %s", err)
}
if got != expected {
t.Fatalf("string.decode got %+v, expected %+v", got, expected)
}
}
func TestGoodStringDecode(t *testing.T) {
stringDecoder := processor.StringDecode{}
tests := []struct {
name string
payload any
expected string
}{
{
name: "basic string",
payload: []byte{0x68, 0x65, 0x6c, 0x6c, 0x6f},
expected: "hello",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got, err := stringDecoder.Process(t.Context(), test.payload)
gotString, ok := got.(string)
if !ok {
t.Fatalf("string.decode returned a %T payload: %s", got, got)
}
if err != nil {
t.Fatalf("string.decode failed: %s", err)
}
if gotString != test.expected {
t.Fatalf("string.decode got %s, expected %s", got, test.expected)
}
})
}
}
func TestBadStringDecode(t *testing.T) {
stringDecoder := processor.StringDecode{}
tests := []struct {
name string
payload any
errorString string
}{
{
name: "non-[]byte input",
payload: "hello",
errorString: "string.decode processor only accepts a []byte",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got, err := stringDecoder.Process(t.Context(), test.payload)
if err == nil {
t.Fatalf("string.decode expected to fail but got payload: %s", got)
}
if err.Error() != test.errorString {
t.Fatalf("string.decode got error '%s', expected '%s'", err.Error(), test.errorString)
}
})
}
}

View File

@@ -0,0 +1,105 @@
package processor_test
import (
"slices"
"testing"
"github.com/jwetzell/showbridge-go/internal/config"
"github.com/jwetzell/showbridge-go/internal/processor"
)
func TestStringEncodeFromRegistry(t *testing.T) {
registration, ok := processor.ProcessorRegistry["string.encode"]
if !ok {
t.Fatalf("string.encode processor not registered")
}
processorInstance, err := registration.New(config.ProcessorConfig{
Type: "string.encode",
})
if err != nil {
t.Fatalf("failed to create string.encode processor: %s", err)
}
if processorInstance.Type() != "string.encode" {
t.Fatalf("string.encode processor has wrong type: %s", processorInstance.Type())
}
payload := "hello"
expected := []byte{'h', 'e', 'l', 'l', 'o'}
got, err := processorInstance.Process(t.Context(), payload)
if err != nil {
t.Fatalf("string.encode processing failed: %s", err)
}
gotBytes, ok := got.([]byte)
if !ok {
t.Fatalf("string.encode should return byte slice")
}
if !slices.Equal(gotBytes, expected) {
t.Fatalf("string.encode got %+v, expected %+v", got, expected)
}
}
func TestGoodStringEncode(t *testing.T) {
stringEncoder := processor.StringEncode{}
tests := []struct {
name string
payload any
expected []byte
}{
{
name: "basic string",
payload: "hello",
expected: []byte{0x68, 0x65, 0x6c, 0x6c, 0x6f},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got, err := stringEncoder.Process(t.Context(), test.payload)
gotBytes, ok := got.([]byte)
if !ok {
t.Fatalf("string.encode returned a %T payload: %s", got, got)
}
if err != nil {
t.Fatalf("string.encode failed: %s", err)
}
if !slices.Equal(gotBytes, test.expected) {
t.Fatalf("string.encode got %s, expected %s", got, test.expected)
}
})
}
}
func TestBadStringEncode(t *testing.T) {
stringEncoder := processor.StringEncode{}
tests := []struct {
name string
payload any
errorString string
}{
{
name: "non-string input",
payload: []byte{0x68, 0x65, 0x6c, 0x6c, 0x6f},
errorString: "string.encode processor only accepts a string",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got, err := stringEncoder.Process(t.Context(), test.payload)
if err == nil {
t.Fatalf("string.encode expected to fail but got payload: %s", got)
}
if err.Error() != test.errorString {
t.Fatalf("string.encode got error '%s', expected '%s'", err.Error(), test.errorString)
}
})
}
}

View File

@@ -0,0 +1,187 @@
package processor_test
import (
"reflect"
"testing"
"github.com/jwetzell/showbridge-go/internal/config"
"github.com/jwetzell/showbridge-go/internal/processor"
)
func TestStringFilterFromRegistry(t *testing.T) {
registration, ok := processor.ProcessorRegistry["string.filter"]
if !ok {
t.Fatalf("string.filter processor not registered")
}
processorInstance, err := registration.New(config.ProcessorConfig{
Type: "string.filter",
Params: map[string]any{
"pattern": "hello",
},
})
if err != nil {
t.Fatalf("failed to create string.filter processor: %s", err)
}
if processorInstance.Type() != "string.filter" {
t.Fatalf("string.filter processor has wrong type: %s", processorInstance.Type())
}
payload := "hello"
expected := "hello"
got, err := processorInstance.Process(t.Context(), payload)
if err != nil {
t.Fatalf("string.filter processing failed: %s", err)
}
gotString, ok := got.(string)
if !ok {
t.Fatalf("string.filter should return byte slice")
}
if gotString != expected {
t.Fatalf("string.filter got %+v, expected %+v", got, expected)
}
}
func TestGoodStringFilter(t *testing.T) {
tests := []struct {
name string
params map[string]any
payload string
expected any
}{
{
name: "matches pattern",
payload: "hello",
params: map[string]any{"pattern": "hello"},
expected: "hello",
},
{
name: "does not match pattern",
payload: "hello",
params: map[string]any{"pattern": "world"},
expected: nil,
},
{
name: "basic regex",
payload: "hello world",
params: map[string]any{"pattern": ".* world"},
expected: "hello world",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
registration, ok := processor.ProcessorRegistry["string.filter"]
if !ok {
t.Fatalf("string.filter processor not registered")
}
processorInstance, err := registration.New(config.ProcessorConfig{
Type: "string.filter",
Params: test.params,
})
if err != nil {
t.Fatalf("string.filter failed to create processor: %s", err)
}
got, err := processorInstance.Process(t.Context(), test.payload)
if err != nil {
t.Fatalf("string.filter failed: %s", err)
}
if test.expected == nil {
if got != nil {
t.Fatalf("string.filter got %+v, expected nil", got)
}
return
}
gotString, ok := got.(string)
if !ok {
t.Fatalf("string.filter returned a %T payload: %s", got, got)
}
if !reflect.DeepEqual(gotString, test.expected) {
t.Fatalf("string.filter got %+v, expected %+v", gotString, test.expected)
}
})
}
}
func TestBadStringFilter(t *testing.T) {
tests := []struct {
name string
payload any
params map[string]any
errorString string
}{
{
name: "no pattern param",
payload: "hello",
params: map[string]any{},
errorString: "string.filter requires a pattern parameter",
},
{
name: "non-string input",
payload: []byte{0x68, 0x65, 0x6c, 0x6c, 0x6f},
params: map[string]any{
"pattern": "hello",
},
errorString: "string.filter processor only accepts a string",
},
{
name: "non-string pattern param",
payload: "hello",
params: map[string]any{
"pattern": 123,
},
errorString: "string.filter pattern must be a string",
},
{
name: "invalid regex pattern",
payload: "hello",
params: map[string]any{
"pattern": "*invalid",
},
errorString: "error parsing regexp: missing argument to repetition operator: `*`",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
registration, ok := processor.ProcessorRegistry["string.filter"]
if !ok {
t.Fatalf("string.filter processor not registered")
}
processorInstance, err := registration.New(config.ProcessorConfig{
Type: "string.filter",
Params: test.params,
})
if err != nil {
if test.errorString != err.Error() {
t.Fatalf("string.filter got error '%s', expected '%s'", err.Error(), test.errorString)
}
return
}
got, err := processorInstance.Process(t.Context(), test.payload)
if err == nil {
t.Fatalf("string.filter expected to fail but got payload: %s", got)
}
if err.Error() != test.errorString {
t.Fatalf("string.filter got error '%s', expected '%s'", err.Error(), test.errorString)
}
})
}
}

View File

@@ -0,0 +1,139 @@
package processor_test
import (
"slices"
"testing"
"github.com/jwetzell/showbridge-go/internal/config"
"github.com/jwetzell/showbridge-go/internal/processor"
)
func TestStringSplitFromRegistry(t *testing.T) {
registration, ok := processor.ProcessorRegistry["string.split"]
if !ok {
t.Fatalf("string.split processor not registered")
}
processorInstance, err := registration.New(config.ProcessorConfig{
Type: "string.split",
Params: map[string]any{
"separator": ",",
},
})
if err != nil {
t.Fatalf("failed to create string.split processor: %s", err)
}
if processorInstance.Type() != "string.split" {
t.Fatalf("string.split processor has wrong type: %s", processorInstance.Type())
}
payload := "part1,part2,part3"
expected := []string{"part1", "part2", "part3"}
got, err := processorInstance.Process(t.Context(), payload)
if err != nil {
t.Fatalf("string.split processing failed: %s", err)
}
gotStrings, ok := got.([]string)
if !ok {
t.Fatalf("string.split should return a slice of strings")
}
if !slices.Equal(gotStrings, expected) {
t.Fatalf("string.split got %+v, expected %+v", got, expected)
}
}
func TestGoodStringSplit(t *testing.T) {
tests := []struct {
processor processor.Processor
name string
payload any
expected []string
}{
{
processor: &processor.StringSplit{Separator: ","},
name: "comma separated",
payload: "part1,part2,part3",
expected: []string{"part1", "part2", "part3"},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got, err := test.processor.Process(t.Context(), test.payload)
gotStrings, ok := got.([]string)
if !ok {
t.Fatalf("string.split returned a %T payload: %s", got, got)
}
if err != nil {
t.Fatalf("string.split failed: %s", err)
}
if !slices.Equal(gotStrings, test.expected) {
t.Fatalf("string.split got %s, expected %s", got, test.expected)
}
})
}
}
func TestBadStringSplit(t *testing.T) {
tests := []struct {
name string
payload any
params map[string]any
errorString string
}{
{
name: "non-string input",
payload: []byte{0x68, 0x65, 0x6c, 0x6c, 0x6f},
params: map[string]any{"separator": ","},
errorString: "string.split only accepts a string",
},
{
name: "missing separator param",
payload: "part1,part2,part3",
params: map[string]any{},
errorString: "string.split requires a separator",
},
{
name: "non-string separator param",
payload: "part1,part2,part3",
params: map[string]any{"separator": 123},
errorString: "string.split separator must be a string",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
registration, ok := processor.ProcessorRegistry["string.split"]
if !ok {
t.Fatalf("string.split processor not registered")
}
processorInstance, err := registration.New(config.ProcessorConfig{
Type: "string.split",
Params: test.params,
})
if err != nil {
if test.errorString != err.Error() {
t.Fatalf("string.split got error '%s', expected '%s'", err.Error(), test.errorString)
}
return
}
got, err := processorInstance.Process(t.Context(), test.payload)
if err == nil {
t.Fatalf("string.split expected error but got none, payload: %s", got)
}
if err.Error() != test.errorString {
t.Fatalf("string.split got error '%s', expected '%s'", err.Error(), test.errorString)
}
})
}
}

View File

@@ -0,0 +1,30 @@
package processor_test
import (
"testing"
"github.com/jwetzell/showbridge-go/internal/config"
"github.com/jwetzell/showbridge-go/internal/processor"
)
func TestTimeSleepFromRegistry(t *testing.T) {
registration, ok := processor.ProcessorRegistry["time.sleep"]
if !ok {
t.Fatalf("time.sleep processor not registered")
}
processorInstance, err := registration.New(config.ProcessorConfig{
Type: "time.sleep",
Params: map[string]any{
"duration": 1000.0,
},
})
if err != nil {
t.Fatalf("failed to create time.sleep processor: %s", err)
}
if processorInstance.Type() != "time.sleep" {
t.Fatalf("time.sleep processor has wrong type: %s", processorInstance.Type())
}
}

View File

@@ -0,0 +1,209 @@
package processor_test
import (
"testing"
"github.com/jwetzell/showbridge-go/internal/config"
"github.com/jwetzell/showbridge-go/internal/processor"
)
func TestUintParseFromRegistry(t *testing.T) {
registration, ok := processor.ProcessorRegistry["uint.parse"]
if !ok {
t.Fatalf("uint.parse processor not registered")
}
processorInstance, err := registration.New(config.ProcessorConfig{
Type: "uint.parse",
})
if err != nil {
t.Fatalf("failed to create uint.parse processor: %s", err)
}
if processorInstance.Type() != "uint.parse" {
t.Fatalf("uint.parse processor has wrong type: %s", processorInstance.Type())
}
}
func TestUintParseBadConfigBaseString(t *testing.T) {
registration, ok := processor.ProcessorRegistry["uint.parse"]
if !ok {
t.Fatalf("uint.parse processor not registered")
}
_, err := registration.New(config.ProcessorConfig{
Type: "uint.parse",
Params: map[string]any{
"base": "10",
},
})
if err == nil {
t.Fatalf("uint.parse should have returned an error for bad base config")
}
}
func TestUintParseBadConfigBitSizeString(t *testing.T) {
registration, ok := processor.ProcessorRegistry["uint.parse"]
if !ok {
t.Fatalf("uint.parse processor not registered")
}
_, err := registration.New(config.ProcessorConfig{
Type: "uint.parse",
Params: map[string]any{
"bitSize": "64",
},
})
if err == nil {
t.Fatalf("uint.parse should have returned an error for bad bitSize config")
}
}
func TestUintParseGoodConfig(t *testing.T) {
registration, ok := processor.ProcessorRegistry["uint.parse"]
if !ok {
t.Fatalf("uint.parse processor not registered")
}
processorInstance, err := registration.New(config.ProcessorConfig{
Type: "uint.parse",
Params: map[string]any{
"base": 10.0,
"bitSize": 64.0,
},
})
if err != nil {
t.Fatalf("uint.parse should have created processor but got error: %s", err)
}
payload := "12345"
expected := uint64(12345)
got, err := processorInstance.Process(t.Context(), payload)
if err != nil {
t.Fatalf("uint.parse processing failed: %s", err)
}
gotUint, ok := got.(uint64)
if !ok {
t.Fatalf("uint.parse returned a %T payload: %s", got, got)
}
if gotUint != expected {
t.Fatalf("uint.parse got %d, expected %d", gotUint, expected)
}
}
func TestGoodUintParse(t *testing.T) {
tests := []struct {
processor processor.Processor
name string
payload any
expected uint64
base int
bitSize int
}{
{
name: "positive number",
payload: "12345",
expected: 12345,
base: 10,
bitSize: 64,
},
{
name: "zero",
payload: "0",
expected: 0,
base: 10,
bitSize: 64,
},
{
name: "binary",
payload: "1010101",
expected: 85,
base: 2,
bitSize: 64,
},
{
name: "hex",
payload: "15F",
expected: 351,
base: 16,
bitSize: 64,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
uintParser := processor.UintParse{
Base: test.base,
BitSize: test.bitSize,
}
got, err := uintParser.Process(t.Context(), test.payload)
gotUint, ok := got.(uint64)
if !ok {
t.Fatalf("uint.parse returned a %T payload: %s", got, got)
}
if err != nil {
t.Fatalf("uint.parse failed: %s", err)
}
if gotUint != test.expected {
t.Fatalf("uint.parse got %d, expected %d", gotUint, test.expected)
}
})
}
}
func TestBadUintParse(t *testing.T) {
uintParser := processor.UintParse{}
tests := []struct {
processor processor.Processor
name string
payload any
base int
bitSize int
errorString string
}{
{
name: "non-string input",
payload: []byte{0x01},
base: 10,
bitSize: 64,
errorString: "uint.parse processor only accepts a string",
},
{
name: "not uint string",
payload: "-1234",
base: 10,
bitSize: 64,
errorString: "strconv.ParseUint: parsing \"-1234\": invalid syntax",
},
{
name: "bit overflow",
payload: "123456789012345678901234567",
base: 10,
bitSize: 32,
errorString: "strconv.ParseUint: parsing \"123456789012345678901234567\": value out of range",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got, err := uintParser.Process(t.Context(), test.payload)
if err == nil {
t.Fatalf("uint.parse expected to fail but succeeded, got: %v", got)
}
if err.Error() != test.errorString {
t.Fatalf("uint.parse got error '%s', expected '%s'", err.Error(), test.errorString)
}
})
}
}

View File

@@ -0,0 +1,177 @@
package processor_test
import (
"testing"
"github.com/jwetzell/showbridge-go/internal/config"
"github.com/jwetzell/showbridge-go/internal/processor"
)
func TestUintRandomFromRegistry(t *testing.T) {
registration, ok := processor.ProcessorRegistry["uint.random"]
if !ok {
t.Fatalf("uint.random processor not registered")
}
processorInstance, err := registration.New(config.ProcessorConfig{
Type: "uint.random",
Params: map[string]any{
"min": 1.0,
"max": 10.0,
},
})
if err != nil {
t.Fatalf("failed to create uint.random processor: %s", err)
}
if processorInstance.Type() != "uint.random" {
t.Fatalf("uint.random processor has wrong type: %s", processorInstance.Type())
}
}
func TestUintRandomGoodConfig(t *testing.T) {
registration, ok := processor.ProcessorRegistry["uint.random"]
if !ok {
t.Fatalf("uint.random processor not registered")
}
processorInstance, err := registration.New(config.ProcessorConfig{
Type: "uint.random",
Params: map[string]any{
"min": 1.0,
"max": 10.0,
},
})
if err != nil {
t.Fatalf("uint.random should have created processor but got error: %s", err)
}
payload := "12345"
got, err := processorInstance.Process(t.Context(), payload)
if err != nil {
t.Fatalf("uint.random processing failed: %s", err)
}
gotUint, ok := got.(uint)
if !ok {
t.Fatalf("uint.random returned a %T payload: %s", got, got)
}
if gotUint < 1 || gotUint > 10 {
t.Fatalf("uint.random got %d, expected between %d and %d", gotUint, 1, 10)
}
}
func TestGoodUintRandom(t *testing.T) {
tests := []struct {
processor processor.Processor
name string
payload any
min uint
max uint
}{
{
name: "1-10",
payload: "12345",
min: 1,
max: 10,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
uintRandom := processor.UintRandom{
Min: test.min,
Max: test.max,
}
got, err := uintRandom.Process(t.Context(), test.payload)
gotUint, ok := got.(uint)
if !ok {
t.Fatalf("uint.random returned a %T payload: %s", got, got)
}
if err != nil {
t.Fatalf("uint.random failed: %s", err)
}
if gotUint < test.min || gotUint > test.max {
t.Fatalf("uint.random got %d, expected between %d and %d", gotUint, test.min, test.max)
}
})
}
}
func TestBadUintRandom(t *testing.T) {
tests := []struct {
name string
params map[string]any
payload any
errorString string
}{
{
name: "no min param",
payload: "hello",
params: map[string]any{"max": 10.0},
errorString: "uint.random requires a min parameter",
},
{
name: "no max param",
payload: "hello",
params: map[string]any{"min": 1.0},
errorString: "uint.random requires a max parameter",
},
{
name: "min param not a number",
payload: "hello",
params: map[string]any{"min": "1", "max": 10.0},
errorString: "uint.random min must be a number",
},
{
name: "max param not a number",
payload: "hello",
params: map[string]any{"min": 1.0, "max": "10"},
errorString: "uint.random max must be a number",
},
{
name: "max less than min",
payload: "hello",
params: map[string]any{"min": 1.0, "max": 0.0},
errorString: "uint.random max must be greater than min",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
registration, ok := processor.ProcessorRegistry["uint.random"]
if !ok {
t.Fatalf("uint.random processor not registered")
}
processorInstance, err := registration.New(config.ProcessorConfig{
Type: "uint.random",
Params: test.params,
})
if err != nil {
if test.errorString != err.Error() {
t.Fatalf("uint.random got error '%s', expected '%s'", err.Error(), test.errorString)
}
return
}
got, err := processorInstance.Process(t.Context(), test.payload)
if err == nil {
t.Fatalf("uint.random expected to fail but got payload: %s", got)
}
if err.Error() != test.errorString {
t.Fatalf("uint.random got error '%s', expected '%s'", err.Error(), test.errorString)
}
})
}
}