core: creating a new graph from another graph

This commit is contained in:
Marcel Otto 2017-03-31 16:04:39 +02:00
parent f49828b76b
commit fd3a20116b
2 changed files with 32 additions and 0 deletions

View file

@ -38,6 +38,12 @@ defmodule RDF.Graph do
def new(%RDF.Description{} = description), def new(%RDF.Description{} = description),
do: new() |> add(description) do: new() |> add(description)
@doc """
Creates an unnamed `RDF.Graph` from another `RDF.Graph`.
"""
def new(%RDF.Graph{descriptions: descriptions}),
do: %RDF.Graph{descriptions: descriptions}
@doc """ @doc """
Creates an empty unnamed `RDF.Graph`. Creates an empty unnamed `RDF.Graph`.
""" """
@ -68,6 +74,12 @@ defmodule RDF.Graph do
def new(name, %RDF.Description{} = description), def new(name, %RDF.Description{} = description),
do: new(name) |> add(description) do: new(name) |> add(description)
@doc """
Creates a named `RDF.Graph` from another `RDF.Graph`.
"""
def new(name, %RDF.Graph{descriptions: descriptions}),
do: %RDF.Graph{new(name) | descriptions: descriptions}
@doc """ @doc """
Creates an unnamed `RDF.Graph` with initial triples. Creates an unnamed `RDF.Graph` with initial triples.
""" """

View file

@ -77,6 +77,26 @@ defmodule RDF.GraphTest do
assert unnamed_graph?(g) assert unnamed_graph?(g)
assert graph_includes_statement?(g, {EX.Subject, EX.predicate, EX.Object}) assert graph_includes_statement?(g, {EX.Subject, EX.predicate, EX.Object})
end end
test "creating a named graph from another graph" do
g = Graph.new(EX.GraphName, Graph.new({EX.Subject, EX.predicate, EX.Object}))
assert named_graph?(g, uri(EX.GraphName))
assert graph_includes_statement?(g, {EX.Subject, EX.predicate, EX.Object})
g = Graph.new(EX.GraphName, Graph.new(EX.OtherGraphName, {EX.Subject, EX.predicate, EX.Object}))
assert named_graph?(g, uri(EX.GraphName))
assert graph_includes_statement?(g, {EX.Subject, EX.predicate, EX.Object})
end
test "creating an unnamed graph from another graph" do
g = Graph.new(Graph.new({EX.Subject, EX.predicate, EX.Object}))
assert unnamed_graph?(g)
assert graph_includes_statement?(g, {EX.Subject, EX.predicate, EX.Object})
g = Graph.new(Graph.new(EX.OtherGraphName, {EX.Subject, EX.predicate, EX.Object}))
assert unnamed_graph?(g)
assert graph_includes_statement?(g, {EX.Subject, EX.predicate, EX.Object})
end
end end
describe "adding triples" do describe "adding triples" do