cmd/digraph: use graphfmt.Dot Change-Id: Icddd469995974dbf9681b126330db284a95adc4f Reviewed-on: https://go-review.googlesource.com/c/tools/+/747369 Reviewed-by: Alan Donovan <adonovan@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
diff --git a/cmd/digraph/digraph.go b/cmd/digraph/digraph.go index a31e13e..9e527c6 100644 --- a/cmd/digraph/digraph.go +++ b/cmd/digraph/digraph.go
@@ -29,6 +29,7 @@ "unicode/utf8" "golang.org/x/tools/internal/graph" + "golang.org/x/tools/internal/graph/graphfmt" ) func usage() { @@ -171,20 +172,6 @@ return nil } -func (g digraph) toDot(w *bytes.Buffer) { - fmt.Fprintln(w, "digraph {") - for _, src := range g.nodelist() { - for _, dst := range g[src].sort() { - // Dot's quoting rules appear to align with Go's for escString, - // which is the syntax of node IDs. Labels require significantly - // more quoting, but that appears not to be necessary if the node ID - // is implicitly used as the label. - fmt.Fprintf(w, "\t%q -> %q;\n", src, dst) - } - } - fmt.Fprintln(w, "}") -} - func parse(rd io.Reader) (digraph, error) { g := make(digraph) @@ -398,9 +385,7 @@ if len(args) != 1 || args[0] != "dot" { return fmt.Errorf("usage: digraph to dot") } - var b bytes.Buffer - g.toDot(&b) - stdout.Write(b.Bytes()) + stdout.Write([]byte(graphfmt.Dot[string]{}.Sprint(g))) default: return fmt.Errorf("no such command %q", cmd)
diff --git a/cmd/digraph/digraph_test.go b/cmd/digraph/digraph_test.go index 74e61ee..dcef4c9 100644 --- a/cmd/digraph/digraph_test.go +++ b/cmd/digraph/digraph_test.go
@@ -387,11 +387,15 @@ in := `a b c b "d\"\\d" c "d\"\\d"` - want := `digraph { - "a" -> "b"; - "a" -> "c"; - "b" -> "d\"\\d"; - "c" -> "d\"\\d"; + want := `digraph "" { +n0 [label="a"]; +n1 [label="b"]; +n2 [label="c"]; +n3 [label="d\"\\d"]; +n0 -> n1; +n0 -> n2; +n1 -> n3; +n2 -> n3; } ` defer func(in io.Reader, out io.Writer) { stdin, stdout = in, out }(stdin, stdout) @@ -402,7 +406,6 @@ } got := stdout.(fmt.Stringer).String() if got != want { - t.Errorf("digraph(to, dot) = got %q, want %q", got, want) + t.Errorf("digraph(to, dot) = got\n %q\nwant\n %q", got, want) } - }