-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvisualizer_mermaid_test.go
151 lines (142 loc) · 5.03 KB
/
visualizer_mermaid_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package fsm
import (
"fmt"
"strings"
"testing"
)
func Test_MermaidStateDiagram(t *testing.T) {
fsmUnderTest := NewSafeFsm[LampEvent, LampStatus](
LampStatus_Closed,
NewTransition([]Transform[LampEvent, LampStatus]{
{Event: LampEvent_Open, Src: []LampStatus{LampStatus_Closed}, Dst: LampStatus_Opened},
{Event: LampEvent_Close, Src: []LampStatus{LampStatus_Opened}, Dst: LampStatus_Closed},
{Event: LampEvent_PartialClose, Src: []LampStatus{LampStatus_Intermediate}, Dst: LampStatus_Closed},
}),
)
got, err := VisualizeMermaid[LampEvent, LampStatus](StateDiagram, fsmUnderTest)
if err != nil {
t.Errorf("got error for visualizing with type MERMAID: %s", err)
}
wanted := `
stateDiagram-v2
[*] --> closed
closed --> opened: open
intermediate --> closed: partial-close
opened --> closed: close
`
normalizedGot := strings.ReplaceAll(got, "\n", "")
normalizedWanted := strings.ReplaceAll(wanted, "\n", "")
if normalizedGot != normalizedWanted {
t.Errorf("build mermaid graph failed. \nwanted \n%s\nand got \n%s\n", wanted, got)
fmt.Println(normalizedGot)
fmt.Println(normalizedWanted)
}
}
func Test_MermaidStateDiagram_CustomName(t *testing.T) {
fsmUnderTest := NewSafeFsm[LampEvent, LampStatus](
LampStatus_Closed,
NewTransitionBuilder([]Transform[LampEvent, LampStatus]{
{Event: LampEvent_Open, Src: []LampStatus{LampStatus_Closed}, Dst: LampStatus_Opened},
{Event: LampEvent_Close, Src: []LampStatus{LampStatus_Opened}, Dst: LampStatus_Closed},
{Event: LampEvent_PartialClose, Src: []LampStatus{LampStatus_Intermediate}, Dst: LampStatus_Closed},
}).
Name("Lamp FSM").
Build(),
)
got, err := VisualizeMermaid[LampEvent, LampStatus](StateDiagram, fsmUnderTest)
if err != nil {
t.Errorf("got error for visualizing with type MERMAID: %s", err)
}
wanted := `
---
title: Lamp FSM
---
stateDiagram-v2
[*] --> closed
closed --> opened: open
intermediate --> closed: partial-close
opened --> closed: close
`
normalizedGot := strings.ReplaceAll(got, "\n", "")
normalizedWanted := strings.ReplaceAll(wanted, "\n", "")
if normalizedGot != normalizedWanted {
t.Errorf("build mermaid graph failed. \nwanted \n%s\nand got \n%s\n", wanted, got)
fmt.Println(normalizedGot)
fmt.Println(normalizedWanted)
}
}
func Test_MermaidFlowChart(t *testing.T) {
fsmUnderTest := NewSafeFsm[LampEvent, LampStatus](
LampStatus_Closed,
NewTransition([]Transform[LampEvent, LampStatus]{
{Event: LampEvent_Open, Src: []LampStatus{LampStatus_Closed}, Dst: LampStatus_Opened},
{Event: LampEvent_PartialOpen, Src: []LampStatus{LampStatus_Closed}, Dst: LampStatus_Intermediate},
{Event: LampEvent_PartialOpen, Src: []LampStatus{LampStatus_Intermediate}, Dst: LampStatus_Opened},
{Event: LampEvent_Close, Src: []LampStatus{LampStatus_Opened}, Dst: LampStatus_Closed},
{Event: LampEvent_PartialClose, Src: []LampStatus{LampStatus_Intermediate}, Dst: LampStatus_Closed},
}),
)
got, err := VisualizeMermaid[LampEvent, LampStatus](FlowChart, fsmUnderTest)
if err != nil {
t.Errorf("got error for visualizing with type MERMAID: %s", err)
}
wanted := `
graph LR
id0[closed]
id1[intermediate]
id2[opened]
id0 --> |open| id2
id0 --> |partial-open| id1
id1 --> |partial-close| id0
id1 --> |partial-open| id2
id2 --> |close| id0
style id0 fill:#00AA00
`
normalizedGot := strings.ReplaceAll(got, "\n", "")
normalizedWanted := strings.ReplaceAll(wanted, "\n", "")
if normalizedGot != normalizedWanted {
t.Errorf("build mermaid graph failed. \nwanted \n%s\nand got \n%s\n", wanted, got)
fmt.Println(normalizedGot)
fmt.Println(normalizedWanted)
}
}
func Test_MermaidFlowChart_CustomName(t *testing.T) {
fsmUnderTest := NewSafeFsm[LampEvent, LampStatus](
LampStatus_Closed,
NewTransitionBuilder([]Transform[LampEvent, LampStatus]{
{Event: LampEvent_Open, Src: []LampStatus{LampStatus_Closed}, Dst: LampStatus_Opened},
{Event: LampEvent_PartialOpen, Src: []LampStatus{LampStatus_Closed}, Dst: LampStatus_Intermediate},
{Event: LampEvent_PartialOpen, Src: []LampStatus{LampStatus_Intermediate}, Dst: LampStatus_Opened},
{Event: LampEvent_Close, Src: []LampStatus{LampStatus_Opened}, Dst: LampStatus_Closed},
{Event: LampEvent_PartialClose, Src: []LampStatus{LampStatus_Intermediate}, Dst: LampStatus_Closed},
}).
Name("Lamp FSM").
Build(),
)
got, err := VisualizeMermaid[LampEvent, LampStatus](FlowChart, fsmUnderTest)
if err != nil {
t.Errorf("got error for visualizing with type MERMAID: %s", err)
}
wanted := `
---
title: Lamp FSM
---
graph LR
id0[closed]
id1[intermediate]
id2[opened]
id0 --> |open| id2
id0 --> |partial-open| id1
id1 --> |partial-close| id0
id1 --> |partial-open| id2
id2 --> |close| id0
style id0 fill:#00AA00
`
normalizedGot := strings.ReplaceAll(got, "\n", "")
normalizedWanted := strings.ReplaceAll(wanted, "\n", "")
if normalizedGot != normalizedWanted {
t.Errorf("build mermaid graph failed. \nwanted \n%s\nand got \n%s\n", wanted, got)
fmt.Println([]byte(normalizedGot))
fmt.Println([]byte(normalizedWanted))
}
}