add test for string.create

This commit is contained in:
Joel Wetzell
2025-12-24 18:18:08 -06:00
parent 460d2f051d
commit e679bf2b46

View File

@@ -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)
}
})
}
}