Skip to content

Commit

Permalink
Support non string labels (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
martosaur authored Sep 28, 2024
1 parent 7bc4c54 commit 4332117
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/inspect.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
12 changes: 12 additions & 0 deletions test/dg_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4332117

Please sign in to comment.