From 9bd9cbe2227c6a30c241f2df570169ebc262698a Mon Sep 17 00:00:00 2001 From: qmuntal Date: Wed, 20 Apr 2022 16:53:52 +0200 Subject: [PATCH] graphviz: quote transition labels appropriately --- graph.go | 2 +- graph_test.go | 23 +++++++++++++++++++++++ testdata/golden/withUnicodeNames.dot | 25 +++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 testdata/golden/withUnicodeNames.dot diff --git a/graph.go b/graph.go index d57ffde..bc3112c 100644 --- a/graph.go +++ b/graph.go @@ -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, ", ")) diff --git a/graph_test.go b/graph_test.go index 5904a45..4f8602d 100644 --- a/graph_test.go +++ b/graph_test.go @@ -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() diff --git a/testdata/golden/withUnicodeNames.dot b/testdata/golden/withUnicodeNames.dot new file mode 100644 index 0000000..d2bfc60 --- /dev/null +++ b/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 -> Ĕ +}