From e679bf2b46f09eaefcf8fcce1bf2092b24f1a7c0 Mon Sep 17 00:00:00 2001 From: Joel Wetzell Date: Wed, 24 Dec 2025 18:18:08 -0600 Subject: [PATCH] add test for string.create --- internal/processor/string-create_test.go | 81 ++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 internal/processor/string-create_test.go diff --git a/internal/processor/string-create_test.go b/internal/processor/string-create_test.go new file mode 100644 index 0000000..1ee3c1c --- /dev/null +++ b/internal/processor/string-create_test.go @@ -0,0 +1,81 @@ +package processor_test + +import ( + "testing" + "text/template" + + "github.com/jwetzell/showbridge-go/internal/processor" +) + +type TestStruct struct { + Data string +} + +func (t TestStruct) GetData() string { + return t.Data +} + +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.Errorf("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.Errorf("string.create returned a %T payload: %s", got, got) + } + if err != nil { + t.Errorf("string.create failed: %s", err) + } + if gotStrings != test.expected { + t.Errorf("string.create got %s, expected %s", got, test.expected) + } + }) + } +}