mirror of
https://github.com/jwetzell/showbridge-go.git
synced 2026-04-27 13:25:40 +00:00
Merge pull request #18 from jwetzell/test/script-processors
add basic tests for script processors
This commit is contained in:
58
internal/processor/script-expr_test.go
Normal file
58
internal/processor/script-expr_test.go
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
package processor_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/expr-lang/expr"
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/processor"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestGoodScriptExpr(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
program string
|
||||||
|
name string
|
||||||
|
payload map[string]any
|
||||||
|
expected any
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
program: "foo + bar",
|
||||||
|
name: "number",
|
||||||
|
payload: map[string]any{
|
||||||
|
"foo": 1,
|
||||||
|
"bar": 1,
|
||||||
|
},
|
||||||
|
expected: 2,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
program: "foo + bar",
|
||||||
|
name: "string",
|
||||||
|
payload: map[string]any{
|
||||||
|
"foo": "1",
|
||||||
|
"bar": "1",
|
||||||
|
},
|
||||||
|
expected: "11",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
t.Run(test.name, func(t *testing.T) {
|
||||||
|
program, err := expr.Compile(test.program)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("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.Errorf("script.expr failed: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
//TODO(jwetzell): work out better way to compare the any/any
|
||||||
|
if got != test.expected {
|
||||||
|
t.Errorf("script.expr got %+v (%T), expected %+v (%T)", got, got, test.expected, test.expected)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
48
internal/processor/script-js_test.go
Normal file
48
internal/processor/script-js_test.go
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
package processor_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/processor"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestGoodScriptJS(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
processor processor.Processor
|
||||||
|
name string
|
||||||
|
payload any
|
||||||
|
expected any
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
processor: &processor.ScriptJS{Program: `
|
||||||
|
payload = payload + 1
|
||||||
|
`},
|
||||||
|
name: "number",
|
||||||
|
payload: 1,
|
||||||
|
expected: 2,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
processor: &processor.ScriptJS{Program: `
|
||||||
|
payload = payload + "1"
|
||||||
|
`},
|
||||||
|
name: "string",
|
||||||
|
payload: "1",
|
||||||
|
expected: "11",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
t.Run(test.name, func(t *testing.T) {
|
||||||
|
got, err := test.processor.Process(t.Context(), test.payload)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("script.js failed: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
//TODO(jwetzell): work out better way to compare the any/any
|
||||||
|
if got != test.expected {
|
||||||
|
t.Errorf("script.js got %+v, expected %+v", got, test.expected)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user