controllogic: add action.array.* nodes (push/set/remove/pop/clear)
This commit is contained in:
@@ -1,10 +1,65 @@
|
||||
package controllogic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// runFlowOnce compiles g, wires a minimal engine/context onto the compiled graph
|
||||
// (enough for the run/follow path and the lock-free emitDebug guard), then drives
|
||||
// the flow from triggerID synchronously and returns the compiled graph so callers
|
||||
// can inspect locals.
|
||||
func runFlowOnce(t *testing.T, g Graph, triggerID string) *compiledGraph {
|
||||
t.Helper()
|
||||
cg := compile(g)
|
||||
cg.engine = &Engine{}
|
||||
cg.genCtx = context.Background()
|
||||
R := func(ds, name string) Value {
|
||||
switch ds {
|
||||
case "local":
|
||||
return cg.getLocal(name)
|
||||
default:
|
||||
return 0.0
|
||||
}
|
||||
}
|
||||
ctx := &runCtx{fired: triggerID, resolve: R}
|
||||
cg.follow(triggerID, "out", ctx)
|
||||
return cg
|
||||
}
|
||||
|
||||
func TestArrayPushNode(t *testing.T) {
|
||||
g := Graph{
|
||||
ID: "g", Name: "n",
|
||||
StateVars: []StateVar{{Name: "buf", Type: "array", Initial: "[1]", Sizing: "dynamic"}},
|
||||
Nodes: []Node{
|
||||
{ID: "t", Kind: "trigger.timer", Params: map[string]string{"interval": "1000"}},
|
||||
{ID: "p", Kind: "action.array.push", Params: map[string]string{"array": "buf", "expr": "5"}},
|
||||
},
|
||||
Wires: []Wire{{From: "t", To: "p"}},
|
||||
}
|
||||
cg := runFlowOnce(t, g, "t")
|
||||
if got := cg.getLocal("buf"); !reflect.DeepEqual(got, []Value{1.0, 5.0}) {
|
||||
t.Fatalf("after push buf = %#v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestArrayClearNode(t *testing.T) {
|
||||
g := Graph{
|
||||
ID: "g", Name: "n",
|
||||
StateVars: []StateVar{{Name: "buf", Type: "array", Initial: "[1,2,3]", Sizing: "fixed", Capacity: 3}},
|
||||
Nodes: []Node{
|
||||
{ID: "t", Kind: "trigger.timer", Params: map[string]string{"interval": "1000"}},
|
||||
{ID: "c", Kind: "action.array.clear", Params: map[string]string{"array": "buf"}},
|
||||
},
|
||||
Wires: []Wire{{From: "t", To: "c"}},
|
||||
}
|
||||
cg := runFlowOnce(t, g, "t")
|
||||
if got := cg.getLocal("buf"); !reflect.DeepEqual(got, []Value{0.0, 0.0, 0.0}) {
|
||||
t.Fatalf("after clear buf = %#v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCompileInitsLocalsFromDecls(t *testing.T) {
|
||||
g := Graph{
|
||||
ID: "g", Name: "n",
|
||||
|
||||
Reference in New Issue
Block a user