mirror of
https://github.com/jwetzell/showbridge-go.git
synced 2026-04-26 21:05:30 +00:00
219 lines
5.4 KiB
Go
219 lines
5.4 KiB
Go
package processor_test
|
|
|
|
import (
|
|
"reflect"
|
|
"testing"
|
|
|
|
"github.com/jwetzell/showbridge-go/internal/common"
|
|
"github.com/jwetzell/showbridge-go/internal/config"
|
|
"github.com/jwetzell/showbridge-go/internal/processor"
|
|
"github.com/jwetzell/showbridge-go/internal/test"
|
|
)
|
|
|
|
func TestStructMethodGetFromRegistry(t *testing.T) {
|
|
registration, ok := processor.ProcessorRegistry["struct.method.get"]
|
|
if !ok {
|
|
t.Fatalf("struct.method.get processor not registered")
|
|
}
|
|
|
|
processorInstance, err := registration.New(config.ProcessorConfig{
|
|
Type: "struct.method.get",
|
|
Params: map[string]any{
|
|
"name": "GetData",
|
|
},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("failed to create struct.method.get processor: %s", err)
|
|
}
|
|
|
|
if processorInstance.Type() != "struct.method.get" {
|
|
t.Fatalf("struct.method.get processor has wrong type: %s", processorInstance.Type())
|
|
}
|
|
|
|
payload := test.TestStruct{Data: "hello"}
|
|
expected := "hello"
|
|
|
|
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), payload))
|
|
if err != nil {
|
|
t.Fatalf("struct.method.get processing failed: %s", err)
|
|
}
|
|
|
|
if got.Payload != expected {
|
|
t.Fatalf("struct.method.get got %+v, expected %+v", got, expected)
|
|
}
|
|
}
|
|
|
|
func TestGoodStructMethodGet(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
name string
|
|
params map[string]any
|
|
payload any
|
|
expected any
|
|
}{
|
|
{
|
|
name: "string field",
|
|
params: map[string]any{"name": "GetString"},
|
|
payload: test.TestStruct{String: "hello"},
|
|
expected: "hello",
|
|
},
|
|
{
|
|
name: "int field",
|
|
params: map[string]any{"name": "GetInt"},
|
|
payload: test.TestStruct{Int: 42},
|
|
expected: 42,
|
|
},
|
|
{
|
|
name: "float field",
|
|
params: map[string]any{"name": "GetFloat"},
|
|
payload: test.TestStruct{Float: 3.14},
|
|
expected: 3.14,
|
|
},
|
|
{
|
|
name: "bool field",
|
|
params: map[string]any{"name": "GetBool"},
|
|
payload: test.TestStruct{Bool: true},
|
|
expected: true,
|
|
},
|
|
{
|
|
name: "array data",
|
|
params: map[string]any{
|
|
"name": "GetData",
|
|
},
|
|
payload: test.TestStruct{Data: []string{"hello"}},
|
|
expected: []string{"hello"},
|
|
},
|
|
{
|
|
name: "void",
|
|
params: map[string]any{
|
|
"name": "Void",
|
|
},
|
|
payload: test.TestStruct{},
|
|
expected: nil,
|
|
},
|
|
{
|
|
name: "pointer to struct payload",
|
|
params: map[string]any{
|
|
"name": "GetData",
|
|
},
|
|
payload: &test.TestStruct{Data: "hello"},
|
|
expected: "hello",
|
|
},
|
|
{
|
|
name: "int slice",
|
|
params: map[string]any{
|
|
"name": "GetIntSlice",
|
|
},
|
|
payload: test.TestStruct{IntSlice: []int{1, 2, 3}},
|
|
expected: []int{1, 2, 3},
|
|
},
|
|
{
|
|
name: "multiple return values",
|
|
params: map[string]any{
|
|
"name": "MultipleReturnValues",
|
|
},
|
|
payload: test.TestStruct{String: "hello", Int: 42},
|
|
expected: []any{"hello", 42},
|
|
},
|
|
}
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
registration, ok := processor.ProcessorRegistry["struct.method.get"]
|
|
if !ok {
|
|
t.Fatalf("struct.method.get processor not registered")
|
|
}
|
|
|
|
processorInstance, err := registration.New(config.ProcessorConfig{
|
|
Type: "struct.method.get",
|
|
Params: test.params,
|
|
})
|
|
|
|
if err != nil {
|
|
t.Fatalf("struct.method.get failed to create processor: %s", err)
|
|
}
|
|
|
|
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload))
|
|
|
|
if err != nil {
|
|
t.Fatalf("struct.method.get processing failed: %s", err)
|
|
}
|
|
|
|
if !reflect.DeepEqual(got.Payload, test.expected) {
|
|
t.Fatalf("struct.method.get got payload: %+v, expected %+v", got.Payload, test.expected)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestBadStructMethodGet(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
params map[string]any
|
|
payload any
|
|
errorString string
|
|
}{
|
|
{
|
|
name: "no name param",
|
|
payload: test.TestStruct{Data: "hello"},
|
|
params: map[string]any{},
|
|
errorString: "struct.method.get name error: not found",
|
|
},
|
|
{
|
|
name: "non string name",
|
|
payload: test.TestStruct{Data: "hello"},
|
|
params: map[string]any{
|
|
"name": 1,
|
|
},
|
|
errorString: "struct.method.get name error: not a string",
|
|
},
|
|
{
|
|
name: "missing method",
|
|
payload: test.TestStruct{String: "hello"},
|
|
params: map[string]any{
|
|
"name": "NonExistentMethod",
|
|
},
|
|
errorString: "struct.method.get method 'NonExistentMethod' does not exist",
|
|
},
|
|
{
|
|
name: "not a struct payload",
|
|
payload: "not a struct",
|
|
params: map[string]any{
|
|
"name": "NonExistentMethod",
|
|
},
|
|
errorString: "struct.method.get processor only accepts a struct payload",
|
|
},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
registration, ok := processor.ProcessorRegistry["struct.method.get"]
|
|
if !ok {
|
|
t.Fatalf("struct.method.get processor not registered")
|
|
}
|
|
|
|
processorInstance, err := registration.New(config.ProcessorConfig{
|
|
Type: "struct.method.get",
|
|
Params: test.params,
|
|
})
|
|
|
|
if err != nil {
|
|
if test.errorString != err.Error() {
|
|
t.Fatalf("struct.method.get got error '%s', expected '%s'", err.Error(), test.errorString)
|
|
}
|
|
return
|
|
}
|
|
|
|
got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload))
|
|
|
|
if err == nil {
|
|
t.Fatalf("struct.method.get expected to fail but got payload: %+v", got)
|
|
}
|
|
|
|
if err.Error() != test.errorString {
|
|
t.Fatalf("struct.method.get got error '%s', expected '%s'", err.Error(), test.errorString)
|
|
}
|
|
})
|
|
}
|
|
}
|