Repository navigation
Expand file tree
/
Copy pathloop_test.go
More file actions
82 lines (76 loc) · 2.57 KB
/
Copy pathloop_test.go
File metadata and controls
82 lines (76 loc) · 2.57 KB
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
package layout_test
import (
"slices"
"testing"
"github.com/loov/layout"
)
// TestLoopLeavesHorizontally checks that a self-loop leaves and returns
// horizontally from the outline, so that drawings on a grid don't get a
// step at its ends.
func TestLoopLeavesHorizontally(t *testing.T) {
for _, shape := range []layout.Shape{layout.Ellipse, layout.Circle, layout.Box} {
graph := layout.NewDigraph()
graph.Node("a").Shape = shape
loop := graph.Edge("a", "a")
l, err := layout.Hierarchical(graph, layout.Options{})
if err != nil {
t.Fatal(err)
}
p := l.Edge(loop).Path
if len(p) != 4 || p[0].Y != p[1].Y || p[2].Y != p[3].Y {
t.Errorf("%s: loop path %v, want horizontal ends", shape, p)
}
}
}
// TestLoopNodeLinesUp checks that a node with a self-loop lines up with
// its neighbors: the room reserved for the loop doesn't shift it.
func TestLoopNodeLinesUp(t *testing.T) {
for _, align := range []layout.Align{layout.AlignBalanced, layout.AlignLeft, layout.AlignRight} {
graph := layout.NewDigraph()
graph.Edge("a", "b")
graph.Edge("b", "b")
graph.Edge("b", "c")
l, err := layout.Hierarchical(graph, layout.Options{Align: align})
if err != nil {
t.Fatal(err)
}
a := l.Node(graph.Node("a")).Center.X
b := l.Node(graph.Node("b")).Center.X
c := l.Node(graph.Node("c")).Center.X
if a != b || b != c {
t.Errorf("align %v: a, b, c at x %v, %v, %v, want one line", align, a, b, c)
}
}
}
// TestLoopsApart checks that several self-loops of a node take routes of
// their own, with labels apart, in hierarchical and force layouts.
func TestLoopsApart(t *testing.T) {
for name, run := range algorithms {
graph := layout.NewDigraph()
for _, label := range []string{"first", "second"} {
edge := layout.NewEdge(graph.Node("a"), graph.Node("a"))
edge.Label = label
graph.AddEdge(edge)
}
l, err := run(graph)
if err != nil {
t.Fatal(err)
}
a, b := l.Edges[0], l.Edges[1]
if slices.Equal(a.Path, b.Path) {
t.Errorf("%s: loops share the route %v", name, a.Path)
}
if gap := (a.LabelCenter.Y - b.LabelCenter.Y).Abs(); gap < (a.LabelSize.Y+b.LabelSize.Y)/2 {
t.Errorf("%s: labels at %v and %v overlap", name, a.LabelCenter, b.LabelCenter)
}
}
}
// algorithms lays out a graph with each algorithm and default options
var algorithms = map[string]func(*layout.Graph) (*layout.Layout, error){
"hierarchical": func(graph *layout.Graph) (*layout.Layout, error) {
return layout.Hierarchical(graph, layout.Options{})
},
"force": func(graph *layout.Graph) (*layout.Layout, error) {
return layout.Force(graph, layout.ForceOptions{})
},
}