mirror of
https://github.com/jwetzell/showbridge-go.git
synced 2026-04-26 21:05:30 +00:00
add tests for registration and error cases to script.expr
This commit is contained in:
@@ -4,9 +4,83 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/expr-lang/expr"
|
"github.com/expr-lang/expr"
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/config"
|
||||||
"github.com/jwetzell/showbridge-go/internal/processor"
|
"github.com/jwetzell/showbridge-go/internal/processor"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestScriptExprFromRegistry(t *testing.T) {
|
||||||
|
registration, ok := processor.ProcessorRegistry["script.expr"]
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("script.expr processor not registered")
|
||||||
|
}
|
||||||
|
|
||||||
|
processorInstance, err := registration.New(config.ProcessorConfig{
|
||||||
|
Type: "script.expr",
|
||||||
|
Params: map[string]any{
|
||||||
|
"expression": "foo + bar",
|
||||||
|
},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to create script.expr processor: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if processorInstance.Type() != "script.expr" {
|
||||||
|
t.Fatalf("script.expr processor has wrong type: %s", processorInstance.Type())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestScriptExprNoProgram(t *testing.T) {
|
||||||
|
registration, ok := processor.ProcessorRegistry["script.expr"]
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("script.expr processor not registered")
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := registration.New(config.ProcessorConfig{
|
||||||
|
Type: "script.expr",
|
||||||
|
Params: map[string]any{},
|
||||||
|
})
|
||||||
|
|
||||||
|
if err == nil {
|
||||||
|
t.Fatalf("script.expr processor should have thrown an error when creating")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestScriptExprBadConfigWrongExpressionType(t *testing.T) {
|
||||||
|
registration, ok := processor.ProcessorRegistry["script.expr"]
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("script.expr processor not registered")
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := registration.New(config.ProcessorConfig{
|
||||||
|
Type: "script.expr",
|
||||||
|
Params: map[string]any{
|
||||||
|
"expression": 12345,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
if err == nil {
|
||||||
|
t.Fatalf("script.expr processor should have thrown an error when creating with non-string expression")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestScriptExprBadConfigNonCompilingExpression(t *testing.T) {
|
||||||
|
registration, ok := processor.ProcessorRegistry["script.expr"]
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("script.expr processor not registered")
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := registration.New(config.ProcessorConfig{
|
||||||
|
Type: "script.expr",
|
||||||
|
Params: map[string]any{
|
||||||
|
"expression": "foo + ",
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
if err == nil {
|
||||||
|
t.Fatalf("script.expr processor should have thrown an error when creating with non-compiling expression")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestGoodScriptExpr(t *testing.T) {
|
func TestGoodScriptExpr(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
program string
|
program string
|
||||||
@@ -56,3 +130,42 @@ func TestGoodScriptExpr(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestBadScriptExpr(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
program string
|
||||||
|
name string
|
||||||
|
payload map[string]any
|
||||||
|
errorString string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "accessing missing field",
|
||||||
|
program: "foo + bar",
|
||||||
|
payload: map[string]any{
|
||||||
|
"foo": 1,
|
||||||
|
},
|
||||||
|
errorString: "invalid operation: int + <nil> (1:5)\n | foo + bar\n | ....^",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
t.Run(test.name, func(t *testing.T) {
|
||||||
|
program, err := expr.Compile(test.program)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("script.expr failed to compile program: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
exprProcessor := &processor.ScriptExpr{Program: program}
|
||||||
|
|
||||||
|
got, err := exprProcessor.Process(t.Context(), test.payload)
|
||||||
|
|
||||||
|
if err == nil {
|
||||||
|
t.Fatalf("script.expr expected to fail but succeeded, got: %v", got)
|
||||||
|
|
||||||
|
}
|
||||||
|
if err.Error() != test.errorString {
|
||||||
|
t.Fatalf("script.expr got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user