diff --git a/CHANGELOG.md b/CHANGELOG.md index b56d222..3f75647 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,8 +9,8 @@ This project adheres to [Semantic Versioning](http://semver.org/) and ### Changed -- `RDF.Data.merge/3` is now commutative, i.e. structs which implement the - `RDF.Data` protocol can be given also as the second argument +- `RDF.Data.merge/2` and `RDF.Data.equal?/2` are now commutative, i.e. structs + which implement the `RDF.Data` protocol can be given also as the second argument (previously custom structs with `RDF.Data` protocol implementations always had to be given as the first argument) - several performance improvements diff --git a/lib/rdf/data.ex b/lib/rdf/data.ex index 7dbbf45..1be238c 100644 --- a/lib/rdf/data.ex +++ b/lib/rdf/data.ex @@ -237,6 +237,14 @@ defimpl RDF.Data, for: RDF.Description do RDF.Data.equal?(dataset, description) end + def equal?(description, %_{} = other) do + if RDF.Data.impl_for(other) do + RDF.Data.equal?(other, description) + else + raise ArgumentError, "no RDF.Data implementation found for #{inspect(other)}" + end + end + def equal?(_, _), do: false end @@ -331,6 +339,14 @@ defimpl RDF.Data, for: RDF.Graph do def equal?(graph, %Dataset{} = dataset), do: RDF.Data.equal?(dataset, graph) + def equal?(graph, %_{} = other) do + if RDF.Data.impl_for(other) do + RDF.Data.equal?(other, graph) + else + raise ArgumentError, "no RDF.Data implementation found for #{inspect(other)}" + end + end + def equal?(_, _), do: false end @@ -433,5 +449,13 @@ defimpl RDF.Data, for: RDF.Dataset do ) end + def equal?(dataset, %_{} = other) do + if RDF.Data.impl_for(other) do + RDF.Data.equal?(other, dataset) + else + raise ArgumentError, "no RDF.Data implementation found for #{inspect(other)}" + end + end + def equal?(_, _), do: false end diff --git a/test/support/external_data_impl.ex b/test/support/external_data_impl.ex index 2ba8ac3..aca3cd2 100644 --- a/test/support/external_data_impl.ex +++ b/test/support/external_data_impl.ex @@ -27,6 +27,8 @@ defmodule External do def values(_external, _opts \\ []), do: nil def map(_external, _fun), do: nil - def equal?(_, _), do: false + def equal?(%External{}, data) do + RDF.Data.equal?(data, RDF.graph(External.data())) + end end end diff --git a/test/unit/data_test.exs b/test/unit/data_test.exs index be99e91..ff5d64b 100644 --- a/test/unit/data_test.exs +++ b/test/unit/data_test.exs @@ -626,20 +626,28 @@ defmodule RDF.DataTest do end end - test "external RDF.Data protocol implementations" do - description = EX.S |> EX.p(EX.O2) + describe "external RDF.Data protocol implementations" do + test "merge/2" do + description = EX.S |> EX.p(EX.O2) - assert RDF.Data.merge(description, %External{}) == - Description.add(description, External.data()) + assert RDF.Data.merge(description, %External{}) == + Description.add(description, External.data()) - graph = RDF.graph({EX.S, EX.p(), EX.O2}) + graph = RDF.graph({EX.S, EX.p(), EX.O2}) - assert RDF.Data.merge(graph, %External{}) == - Graph.add(graph, External.data()) + assert RDF.Data.merge(graph, %External{}) == + Graph.add(graph, External.data()) - dataset = RDF.dataset({EX.S, EX.p(), EX.O2}) + dataset = RDF.dataset({EX.S, EX.p(), EX.O2}) - assert RDF.Data.merge(dataset, %External{}) == - Dataset.add(dataset, External.data()) + assert RDF.Data.merge(dataset, %External{}) == + Dataset.add(dataset, External.data()) + end + + test "equal/2" do + assert EX.S |> EX.p(42) |> RDF.Data.equal?(%External{}) == true + assert {EX.S, EX.p(), 42} |> RDF.graph() |> RDF.Data.equal?(%External{}) == true + assert {EX.S, EX.p(), 42} |> RDF.dataset() |> RDF.Data.equal?(%External{}) == true + end end end