Skip to content

Commit

Permalink
Subgraph outputs DG (#25)
Browse files Browse the repository at this point in the history
* output of DG.subgraph should be of type DG

* minor spelling fix
  • Loading branch information
jdewar authored Sep 27, 2023
1 parent c15d444 commit 2679e6c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
10 changes: 4 additions & 6 deletions lib/dg.ex
Original file line number Diff line number Diff line change
Expand Up @@ -205,12 +205,10 @@ defmodule DG do
:digraph_utils.strong_components(dg)
end

def subgraph(%__MODULE__{dg: dg}, vertices) do
:digraph_utils.subgraph(dg, vertices)
end

def subgraph(%__MODULE__{dg: dg}, vertices, options) do
:digraph_utils.subgraph(dg, vertices, options)
def subgraph(%__MODULE__{dg: dg}, vertices, options \\ []) do
{digraph_opts, opts} = Keyword.pop(options, :digraph_opts, [])
subgraph = :digraph_utils.subgraph(dg, vertices, digraph_opts)
%__MODULE__{dg: subgraph, opts: opts}
end

def topsort(%__MODULE__{dg: dg}) do
Expand Down
23 changes: 22 additions & 1 deletion test/dg_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,31 @@ defmodule DG.Test do
end
end

describe "subgraph" do
test "returns a DG" do
dg = DG.new(test: true)
~w(a b c d e) |> Enum.map(&{:vertex, &1}) |> Enum.into(dg)

assert %DG{} = DG.subgraph(dg, ~w(a b c))
end

test "handles digraph options" do
dg = DG.new(test: true)
~w(a b c) |> Enum.map(&{:vertex, &1}) |> Enum.into(dg)

label = "a -> b"
Enum.into([{:edge, "a", "b", label}], dg)

subgraph = DG.subgraph(dg, ~w(a b), digraph_opts: [keep_labels: true])

assert {_, "a", "b", ^label} = DG.edge(subgraph, List.first(DG.edges(dg, "a")))
end
end

describe "sigil" do
import DG.Sigil

test "ingetration" do
test "integration" do
dg = ~g"""
graph LR
a[aaaaa]
Expand Down

0 comments on commit 2679e6c

Please sign in to comment.