Add Enumerable protocol implementation for RDF.PropertyMap

This commit is contained in:
Marcel Otto 2020-10-11 01:07:53 +02:00
parent c1495a5f39
commit 95a69d9007
2 changed files with 32 additions and 3 deletions

View file

@ -143,6 +143,15 @@ defmodule RDF.PropertyMap do
end
end
defimpl Enumerable do
alias RDF.PropertyMap
def reduce(%PropertyMap{iris: iris}, acc, fun), do: Enumerable.reduce(iris, acc, fun)
def member?(%PropertyMap{iris: iris}, mapping), do: Enumerable.member?(iris, mapping)
def count(%PropertyMap{iris: iris}), do: Enumerable.count(iris)
def slice(_property_map), do: {:error, __MODULE__}
end
defimpl Inspect do
import Inspect.Algebra

View file

@ -171,9 +171,6 @@ defmodule RDF.PropertyMapTest do
PropertyMap.new(Baz: EX.Baz)
end
describe "expand_description/2" do
end
describe "Access behaviour" do
test "fetch/2" do
assert @example_property_map[:foo] == ~I<http://example.com/test/foo>
@ -197,4 +194,27 @@ defmodule RDF.PropertyMapTest do
{~I<http://example.com/test/foo>, PropertyMap.delete(@example_property_map, :foo)}
end
end
describe "Enumerable protocol" do
test "Enum.count" do
assert Enum.count(PropertyMap.new()) == 0
assert Enum.count(@example_property_map) == 3
end
test "Enum.member?" do
assert Enum.member?(@example_property_map, {:foo, ~I<http://example.com/test/foo>})
assert Enum.member?(@example_property_map, {:bar, ~I<http://example.com/test/bar>})
assert Enum.member?(@example_property_map, {:Baz, RDF.iri(EX.Baz)})
refute Enum.member?(@example_property_map, {:bar, ~I<http://example.com/test/foo>})
end
test "Enum.reduce" do
assert Enum.reduce(@example_property_map, [], fn mapping, acc -> [mapping | acc] end) ==
[
{:foo, ~I<http://example.com/test/foo>},
{:bar, ~I<http://example.com/test/bar>},
{:Baz, RDF.iri(EX.Baz)}
]
end
end
end