diff --git a/lib/inspect.ex b/lib/inspect.ex index ec96c6a..3a9c49f 100644 --- a/lib/inspect.ex +++ b/lib/inspect.ex @@ -21,7 +21,7 @@ defimpl Inspect, for: DG do [inspect_node(dg, v), "-->", inspect_node(dg, n)] {_e, ^v, n, label} -> - [inspect_node(dg, v), "--", label, "-->", inspect_node(dg, n)] + [inspect_node(dg, v), "--", inspect_term(label), "-->", inspect_node(dg, n)] end) |> Enum.intersperse([line()]) end @@ -49,8 +49,11 @@ defimpl Inspect, for: DG do defp label(dg, v, prefix) do case :digraph.vertex(dg, v) do {^v, []} -> [prefix] - {^v, l} -> [prefix, "[", l, "]"] + {^v, l} -> [prefix, "[", inspect_term(l), "]"] end |> concat end + + defp inspect_term(term) when is_binary(term), do: term + defp inspect_term(term), do: inspect(term) end diff --git a/test/dg_test.exs b/test/dg_test.exs index c68e405..c5a6d1b 100644 --- a/test/dg_test.exs +++ b/test/dg_test.exs @@ -74,6 +74,18 @@ defmodule DG.Test do 1--one to two-->2[two] """) end + + test "fancy labels", %{dg: dg} do + DG.add_vertex(dg, 1) + DG.add_vertex(dg, 2, :c.pid(0, 42, 42)) + DG.add_edge(dg, 1, 2, :"one to two") + + assert inspect(dg) == + String.trim(""" + graph LR + 1--:\"one to two\"-->2[#PID<0.42.42>] + """) + end end describe "collectable" do