Skip to content

Commit

Permalink
graphviz: quote transition labels appropriately
Browse files Browse the repository at this point in the history
  • Loading branch information
qmuntal committed Apr 20, 2022
1 parent 03b6436 commit 9bd9cbe
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
2 changes: 1 addition & 1 deletion graph.go
Expand Up @@ -195,7 +195,7 @@ func (g *graph) formatAllStateTransitions(sm *StateMachine, sr *stateRepresentat

func (g *graph) formatOneTransition(sm *StateMachine, source, destination State, trigger Trigger, ltail, lhead string, actions []string, guards transitionGuard) string {
var sb strings.Builder
sb.WriteString(str(trigger, true))
sb.WriteString(str(trigger, false))
if len(actions) > 0 {
sb.WriteString(" / ")
sb.WriteString(strings.Join(actions, ", "))
Expand Down
23 changes: 23 additions & 0 deletions graph_test.go
Expand Up @@ -57,12 +57,35 @@ func withGuards() *stateless.StateMachine {
return sm
}

func œ(_ context.Context, args ...interface{}) bool {
return args[0].(int) == 2
}

func withUnicodeNames() *stateless.StateMachine {
sm := stateless.NewStateMachine("Ĕ")
sm.Configure("Ĕ").
Permit("◵", "ų", œ)
sm.Configure("ų").
InitialTransition("ㇴ")
sm.Configure("ㇴ").
InitialTransition("ꬠ").
SubstateOf("ų")
sm.Configure("ꬠ").
SubstateOf("𒀄")
sm.Configure("1").
SubstateOf("𒀄")
sm.Configure("2").
SubstateOf("1")
return sm
}

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

Ĕ [label="Ĕ"];
subgraph cluster_ų {
label="ų";
"cluster_ų-init" [label="", shape=point];
ㇴ [label=""];
}
subgraph cluster_𒀄 {
label="𒀄";
ꬠ [label=""];
subgraph "cluster_1" {
label="1";
2 [label="2"];
}
}
"cluster_ų-init" -> ꬠ [label="", lhead="cluster_ㇴ"];
"cluster_ㇴ-init" -> ꬠ [label=""];
Ĕ -> ꬠ [label="◵ [œ]", lhead="cluster_ų"];
init [label="", shape=point];
init -> Ĕ
}

0 comments on commit 9bd9cbe

Please sign in to comment.