Skip to content

Commit

Permalink
digraph: more idiomatic writedot api
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchellh committed Jul 14, 2014
1 parent bd2fe4d commit a63125b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 37 deletions.
10 changes: 8 additions & 2 deletions digraph/graphviz.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,22 @@ import (

// GenerateDot is used to emit a GraphViz compatible definition

This comment has been minimized.

Copy link
@armon

armon Jul 14, 2014

Member

Didn't update the name in the docs 😦

This comment has been minimized.

Copy link
@mitchellh

mitchellh Jul 14, 2014

Author Contributor

Ah yeah, doing it now.

// for a directed graph. It can be used to dump a .dot file.
func GenerateDot(nodes []Node, w io.Writer) {
func WriteDot(w io.Writer, nodes []Node) error {
w.Write([]byte("digraph {\n"))
defer w.Write([]byte("}\n"))

for _, n := range nodes {
w.Write([]byte(fmt.Sprintf("\t\"%s\";\n", n)))
nodeLine := fmt.Sprintf("\t\"%s\";\n", n)

w.Write([]byte(nodeLine))

for _, edge := range n.Edges() {
target := edge.Tail()
line := fmt.Sprintf("\t\"%s\" -> \"%s\" [label=\"%s\"];\n",
n, target, edge)
w.Write([]byte(line))
}
}

return nil
}
57 changes: 22 additions & 35 deletions digraph/graphviz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"testing"
)

func Test_GenerateDot(t *testing.T) {
func TestWriteDot(t *testing.T) {
nodes := ParseBasic(`a -> b ; foo
a -> c
b -> d
Expand All @@ -18,40 +18,27 @@ b -> e
}

buf := bytes.NewBuffer(nil)
GenerateDot(nlist, buf)

out := string(buf.Bytes())
if !strings.HasPrefix(out, "digraph {\n") {
t.Fatalf("bad: %v", out)
}
if !strings.HasSuffix(out, "\n}\n") {
t.Fatalf("bad: %v", out)
}
if !strings.Contains(out, "\n\t\"a\";\n") {
t.Fatalf("bad: %v", out)
}
if !strings.Contains(out, "\n\t\"b\";\n") {
t.Fatalf("bad: %v", out)
}
if !strings.Contains(out, "\n\t\"c\";\n") {
t.Fatalf("bad: %v", out)
}
if !strings.Contains(out, "\n\t\"d\";\n") {
t.Fatalf("bad: %v", out)
}
if !strings.Contains(out, "\n\t\"e\";\n") {
t.Fatalf("bad: %v", out)
if err := WriteDot(buf, nlist); err != nil {
t.Fatalf("err: %s", err)
}
if !strings.Contains(out, "\n\t\"a\" -> \"b\" [label=\"foo\"];\n") {
t.Fatalf("bad: %v", out)
}
if !strings.Contains(out, "\n\t\"a\" -> \"c\" [label=\"Edge\"];\n") {
t.Fatalf("bad: %v", out)
}
if !strings.Contains(out, "\n\t\"b\" -> \"d\" [label=\"Edge\"];\n") {
t.Fatalf("bad: %v", out)
}
if !strings.Contains(out, "\n\t\"b\" -> \"e\" [label=\"Edge\"];\n") {
t.Fatalf("bad: %v", out)

actual := strings.TrimSpace(string(buf.Bytes()))
expected := strings.TrimSpace(writeDotStr)
if actual != expected {
t.Fatalf("bad: %s", actual)
}
}

const writeDotStr = `
digraph {
"a";
"a" -> "b" [label="foo"];
"a" -> "c" [label="Edge"];
"b";
"b" -> "d" [label="Edge"];
"b" -> "e" [label="Edge"];
"c";
"d";
"e";
}
`

0 comments on commit a63125b

Please sign in to comment.