quote dot node names

providers can contain quotes, so we need to ensure proper quoting in the
dot format.
This commit is contained in:
James Bardin 2020-06-15 13:23:12 -04:00
parent bc386234d5
commit e36d300efd
2 changed files with 29 additions and 1 deletions

View File

@ -108,9 +108,14 @@ func newMarshalVertex(v Vertex) *marshalVertex {
dn = nil
}
// the name will be quoted again later, so we need to ensure it's properly
// escaped without quotes.
name := strconv.Quote(VertexName(v))
name = name[1 : len(name)-1]
return &marshalVertex{
ID: marshalVertexID(v),
Name: VertexName(v),
Name: name,
Attrs: make(map[string]string),
graphNodeDotter: dn,
}

View File

@ -32,6 +32,21 @@ func TestGraphDot_basic(t *testing.T) {
}
}
func TestGraphDot_quoted(t *testing.T) {
var g Graph
quoted := `name["with-quotes"]`
other := `other`
g.Add(quoted)
g.Add(other)
g.Connect(BasicEdge(quoted, other))
actual := strings.TrimSpace(string(g.Dot(nil)))
expected := strings.TrimSpace(testGraphDotQuotedStr)
if actual != expected {
t.Fatalf("\ngot: %q\nwanted %q\n", actual, expected)
}
}
func TestGraphDot_attrs(t *testing.T) {
var g Graph
g.Add(&testGraphNodeDotter{
@ -53,6 +68,14 @@ type testGraphNodeDotter struct{ Result *DotNode }
func (n *testGraphNodeDotter) Name() string { return n.Result.Name }
func (n *testGraphNodeDotter) DotNode(string, *DotOpts) *DotNode { return n.Result }
const testGraphDotQuotedStr = `digraph {
compound = "true"
newrank = "true"
subgraph "root" {
"[root] name[\"with-quotes\"]" -> "[root] other"
}
}`
const testGraphDotBasicStr = `digraph {
compound = "true"
newrank = "true"