diff --git a/internal/processor/string-create_test.go b/internal/processor/string-create_test.go index 54e0527..6082229 100644 --- a/internal/processor/string-create_test.go +++ b/internal/processor/string-create_test.go @@ -4,6 +4,7 @@ import ( "testing" "text/template" + "github.com/jwetzell/showbridge-go/internal/config" "github.com/jwetzell/showbridge-go/internal/processor" ) @@ -15,6 +16,39 @@ 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 { @@ -79,3 +113,75 @@ func TestGoodStringCreate(t *testing.T) { }) } } + +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.encode got error '%s', expected '%s'", err.Error(), test.errorString) + } + return + } + + got, err := processorInstance.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) + } + }) + } +} diff --git a/internal/processor/string-decode_test.go b/internal/processor/string-decode_test.go index ac80465..b90251e 100644 --- a/internal/processor/string-decode_test.go +++ b/internal/processor/string-decode_test.go @@ -3,9 +3,40 @@ 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 { diff --git a/internal/processor/string-encode_test.go b/internal/processor/string-encode_test.go index b86ad2d..95c1c9a 100644 --- a/internal/processor/string-encode_test.go +++ b/internal/processor/string-encode_test.go @@ -4,9 +4,46 @@ 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 {