add more tests for string.split

This commit is contained in:
Joel Wetzell
2026-02-08 21:50:31 -06:00
parent b53494fddc
commit 7ff20e0136

View File

@@ -4,9 +4,49 @@ 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
@@ -42,22 +82,51 @@ func TestGoodStringSplit(t *testing.T) {
func TestBadStringSplit(t *testing.T) {
tests := []struct {
processor processor.Processor
name string
payload any
params map[string]any
errorString string
}{
{
processor: &processor.StringSplit{Separator: ","},
name: "hello",
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) {
got, err := test.processor.Process(t.Context(), test.payload)
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)