mirror of
https://github.com/gonum/gonum.git
synced 2025-10-14 03:13:46 +08:00
graph/encoding/dot: store graph DOT ID if present (#237)
Add round trip test case for graph DOT ID.
This commit is contained in:
@@ -54,6 +54,9 @@ func copyGraph(dst encoding.Builder, src *ast.Graph) (err error) {
|
|||||||
directed: src.Directed,
|
directed: src.Directed,
|
||||||
ids: make(map[string]graph.Node),
|
ids: make(map[string]graph.Node),
|
||||||
}
|
}
|
||||||
|
if dst, ok := dst.(DOTIDSetter); ok {
|
||||||
|
dst.SetDOTID(src.ID)
|
||||||
|
}
|
||||||
if a, ok := dst.(AttributeSetters); ok {
|
if a, ok := dst.(AttributeSetters); ok {
|
||||||
gen.graphAttr, gen.nodeAttr, gen.edgeAttr = a.DOTAttributeSetters()
|
gen.graphAttr, gen.nodeAttr, gen.edgeAttr = a.DOTAttributeSetters()
|
||||||
}
|
}
|
||||||
|
@@ -26,6 +26,14 @@ func TestRoundTrip(t *testing.T) {
|
|||||||
want: undirected,
|
want: undirected,
|
||||||
directed: false,
|
directed: false,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
want: directedID,
|
||||||
|
directed: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
want: undirectedID,
|
||||||
|
directed: false,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
for i, g := range golden {
|
for i, g := range golden {
|
||||||
var dst encoding.Builder
|
var dst encoding.Builder
|
||||||
@@ -94,6 +102,24 @@ const undirected = `graph {
|
|||||||
A -- B [label="baz 2"];
|
A -- B [label="baz 2"];
|
||||||
}`
|
}`
|
||||||
|
|
||||||
|
const directedID = `digraph G {
|
||||||
|
// Node definitions.
|
||||||
|
A;
|
||||||
|
B;
|
||||||
|
|
||||||
|
// Edge definitions.
|
||||||
|
A -> B;
|
||||||
|
}`
|
||||||
|
|
||||||
|
const undirectedID = `graph H {
|
||||||
|
// Node definitions.
|
||||||
|
A;
|
||||||
|
B;
|
||||||
|
|
||||||
|
// Edge definitions.
|
||||||
|
A -- B;
|
||||||
|
}`
|
||||||
|
|
||||||
// Below follows a minimal implementation of a graph capable of validating the
|
// Below follows a minimal implementation of a graph capable of validating the
|
||||||
// round-trip encoding and decoding of DOT graphs with nodes and edges
|
// round-trip encoding and decoding of DOT graphs with nodes and edges
|
||||||
// containing DOT attributes.
|
// containing DOT attributes.
|
||||||
@@ -101,9 +127,11 @@ const undirected = `graph {
|
|||||||
// dotDirectedGraph extends simple.DirectedGraph to add NewNode and NewEdge
|
// dotDirectedGraph extends simple.DirectedGraph to add NewNode and NewEdge
|
||||||
// methods for creating user-defined nodes and edges.
|
// methods for creating user-defined nodes and edges.
|
||||||
//
|
//
|
||||||
// dotDirectedGraph implements the dot.Builder interface.
|
// dotDirectedGraph implements the encoding.Builder and the dot.Graph
|
||||||
|
// interfaces.
|
||||||
type dotDirectedGraph struct {
|
type dotDirectedGraph struct {
|
||||||
*simple.DirectedGraph
|
*simple.DirectedGraph
|
||||||
|
id string
|
||||||
graph, node, edge attributes
|
graph, node, edge attributes
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -133,12 +161,24 @@ func (g *dotDirectedGraph) DOTAttributeSetters() (graph, node, edge encoding.Att
|
|||||||
return &g.graph, &g.node, &g.edge
|
return &g.graph, &g.node, &g.edge
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetDOTID sets the DOT ID of the graph.
|
||||||
|
func (g *dotDirectedGraph) SetDOTID(id string) {
|
||||||
|
g.id = id
|
||||||
|
}
|
||||||
|
|
||||||
|
// DOTID returns the DOT ID of the graph.
|
||||||
|
func (g *dotDirectedGraph) DOTID() string {
|
||||||
|
return g.id
|
||||||
|
}
|
||||||
|
|
||||||
// dotUndirectedGraph extends simple.UndirectedGraph to add NewNode and NewEdge
|
// dotUndirectedGraph extends simple.UndirectedGraph to add NewNode and NewEdge
|
||||||
// methods for creating user-defined nodes and edges.
|
// methods for creating user-defined nodes and edges.
|
||||||
//
|
//
|
||||||
// dotUndirectedGraph implements the dot.Builder interface.
|
// dotUndirectedGraph implements the encoding.Builder and the dot.Graph
|
||||||
|
// interfaces.
|
||||||
type dotUndirectedGraph struct {
|
type dotUndirectedGraph struct {
|
||||||
*simple.UndirectedGraph
|
*simple.UndirectedGraph
|
||||||
|
id string
|
||||||
graph, node, edge attributes
|
graph, node, edge attributes
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -168,6 +208,16 @@ func (g *dotUndirectedGraph) DOTAttributeSetters() (graph, node, edge encoding.A
|
|||||||
return &g.graph, &g.node, &g.edge
|
return &g.graph, &g.node, &g.edge
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetDOTID sets the DOT ID of the graph.
|
||||||
|
func (g *dotUndirectedGraph) SetDOTID(id string) {
|
||||||
|
g.id = id
|
||||||
|
}
|
||||||
|
|
||||||
|
// DOTID returns the DOT ID of the graph.
|
||||||
|
func (g *dotUndirectedGraph) DOTID() string {
|
||||||
|
return g.id
|
||||||
|
}
|
||||||
|
|
||||||
// dotNode extends simple.Node with a label field to test round-trip encoding
|
// dotNode extends simple.Node with a label field to test round-trip encoding
|
||||||
// and decoding of node DOT label attributes.
|
// and decoding of node DOT label attributes.
|
||||||
type dotNode struct {
|
type dotNode struct {
|
||||||
|
Reference in New Issue
Block a user