Repository navigation
Expand file tree
/
Copy pathvalidate_test.go
More file actions
57 lines (53 loc) · 2.37 KB
/
Copy pathvalidate_test.go
File metadata and controls
57 lines (53 loc) · 2.37 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
package layout_test
import (
"math"
"testing"
"github.com/loov/layout"
)
// TestValidateNumbers checks that layouts reject numbers that would turn
// into non-finite coordinates or runaway ranks.
func TestValidateNumbers(t *testing.T) {
nan, inf := layout.Length(math.NaN()), layout.Length(math.Inf(1))
for name, spoil := range map[string]func(*layout.Graph){
"line height": func(g *layout.Graph) { g.LineHeight = nan },
"font size": func(g *layout.Graph) { g.FontSize = inf },
"node padding": func(g *layout.Graph) { g.NodePadding = nan },
"row padding": func(g *layout.Graph) { g.RowPadding = inf },
"edge padding": func(g *layout.Graph) { g.EdgePadding = nan },
"node size": func(g *layout.Graph) { g.Nodes[0].MinSize.X = inf },
"node font": func(g *layout.Graph) { g.Nodes[0].FontSize = nan },
"node line": func(g *layout.Graph) { g.Nodes[0].LineWidth = inf },
"pinned center": func(g *layout.Graph) { g.Nodes[0].Pos = &layout.Vector{X: 0, Y: nan} },
"pinned path": func(g *layout.Graph) { g.Edges[0].Pos = []layout.Vector{{0, 0}, {inf, 0}} },
"pinned label": func(g *layout.Graph) { g.Edges[0].LabelPos = &layout.Vector{X: nan} },
"edge weight": func(g *layout.Graph) { g.Edges[0].Weight = math.NaN() },
"edge font": func(g *layout.Graph) { g.Edges[0].FontSize = inf },
"edge line": func(g *layout.Graph) { g.Edges[0].LineWidth = nan },
"edge minlen": func(g *layout.Graph) { g.Edges[0].MinLen = 100000000 },
} {
for _, lay := range []func(*layout.Graph) error{
func(g *layout.Graph) error { _, err := layout.Hierarchical(g, layout.Options{}); return err },
func(g *layout.Graph) error { _, err := layout.Force(g, layout.ForceOptions{}); return err },
} {
graph := layout.NewDigraph()
graph.Edge("a", "b")
spoil(graph)
if err := lay(graph); err == nil {
t.Errorf("%s: no error", name)
}
}
}
}
func TestHierarchicalErrors(t *testing.T) {
graph := layout.NewDigraph()
stray := layout.NewNode("stray")
graph.AddEdge(layout.NewEdge(graph.Node("A"), stray))
if _, err := layout.Hierarchical(graph, layout.Options{}); err == nil {
t.Error("expected an error for an edge to a node outside the graph")
}
graph = layout.NewDigraph()
graph.AddEdge(&layout.Edge{From: graph.Node("A")})
if _, err := layout.Hierarchical(graph, layout.Options{}); err == nil {
t.Error("expected an error for a nil endpoint")
}
}