Skip to content

Commit

Permalink
Merge pull request #42 from qmuntal/graph-tests
Browse files Browse the repository at this point in the history
graphviz: quote guard functions appropriately
  • Loading branch information
qmuntal committed Apr 19, 2022
2 parents 130113d + 03b6436 commit ee0c014
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
2 changes: 1 addition & 1 deletion graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ func (g *graph) formatOneTransition(sm *StateMachine, source, destination State,
if sb.Len() > 0 {
sb.WriteString(" ")
}
sb.WriteString(fmt.Sprintf("[%s]", esc(info.Description.String(), true)))
sb.WriteString(fmt.Sprintf("[%s]", esc(info.Description.String(), false)))
}
return g.formatOneLine(str(source, true), str(destination, true), sb.String(), lhead, ltail)
}
Expand Down
18 changes: 18 additions & 0 deletions graph_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package stateless_test

import (
"bytes"
"context"
"flag"
"os"
"reflect"
Expand Down Expand Up @@ -40,11 +41,28 @@ func withInitialState() *stateless.StateMachine {
return sm
}

func withGuards() *stateless.StateMachine {
sm := stateless.NewStateMachine("B")
sm.SetTriggerParameters("X", reflect.TypeOf(0))
sm.Configure("A").
Permit("X", "D", func(_ context.Context, args ...interface{}) bool {
return args[0].(int) == 3
})

sm.Configure("B").
SubstateOf("A").
Permit("X", "C", func(_ context.Context, args ...interface{}) bool {
return args[0].(int) == 2
})
return sm
}

func TestStateMachine_ToGraph(t *testing.T) {
tests := []func() *stateless.StateMachine{
emptyWithInitial,
withSubstate,
withInitialState,
withGuards,
}
for _, fn := range tests {
name := runtime.FuncForPC(reflect.ValueOf(fn).Pointer()).Name()
Expand Down
14 changes: 14 additions & 0 deletions testdata/golden/withGuards.dot
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
digraph {
compound=true;
node [shape=Mrecord];
rankdir="LR";

subgraph cluster_A {
label="A";
B [label="B"];
}
B -> D [label="X [func1]", ltail="cluster_A"];
B -> C [label="X [func2]"];
init [label="", shape=point];
init -> B
}

0 comments on commit ee0c014

Please sign in to comment.