forked from looplab/fsm
-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils_test.go
68 lines (62 loc) · 1.75 KB
/
utils_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
package fsm
import (
"fmt"
"strings"
"testing"
)
func TestGraphvizOutput(t *testing.T) {
fsmUnderTest := NewFSM(
"closed",
Events{
{Name: "open", Src: []string{"closed"}, Dst: "open"},
{Name: "close", Src: []string{"open"}, Dst: "closed"},
{Name: "part-close", Src: []string{"intermediate"}, Dst: "closed"},
},
Callbacks{},
)
got := Visualize(fsmUnderTest)
wanted := `
digraph fsm {
"closed" -> "open" [ label = "open" ];
"intermediate" -> "closed" [ label = "part-close" ];
"open" -> "closed" [ label = "close" ];
"closed";
"intermediate";
"open";
}`
normalizedGot := strings.Replace(got, "\n", "", -1)
normalizedWanted := strings.ReplaceAll(wanted, "\n", "")
if normalizedGot != normalizedWanted {
t.Errorf("build graphivz graph failed. \nwanted \n%s\nand got \n%s\n", wanted, got)
fmt.Println([]byte(normalizedGot))
fmt.Println([]byte(normalizedWanted))
}
}
func TestMermaidOutput(t *testing.T) {
fsmUnderTest := NewFSM(
"closed",
Events{
{Name: "open", Src: []string{"closed"}, Dst: "open"},
{Name: "close", Src: []string{"open"}, Dst: "closed"},
{Name: "part-close", Src: []string{"intermediate"}, Dst: "closed"},
},
Callbacks{},
)
got, err := VisualizeWithType(fsmUnderTest, MERMAID)
if err != nil {
t.Errorf("got error for visualizing with type MERMAID: %s", err)
}
wanted := `
graph fsm
closed -->|open| open
intermediate -->|part-close| closed
open -->|close| closed
`
normalizedGot := strings.Replace(got, "\n", "", -1)
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))
}
}