From 95a69d9007860c722632931120c47a2467f51cb5 Mon Sep 17 00:00:00 2001 From: Marcel Otto Date: Sun, 11 Oct 2020 01:07:53 +0200 Subject: [PATCH] Add Enumerable protocol implementation for RDF.PropertyMap --- lib/rdf/property_map.ex | 9 +++++++++ test/unit/property_map_test.exs | 26 +++++++++++++++++++++++--- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/lib/rdf/property_map.ex b/lib/rdf/property_map.ex index 27cfcd4..3cbd6e4 100644 --- a/lib/rdf/property_map.ex +++ b/lib/rdf/property_map.ex @@ -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 diff --git a/test/unit/property_map_test.exs b/test/unit/property_map_test.exs index ae28b31..ae6dc35 100644 --- a/test/unit/property_map_test.exs +++ b/test/unit/property_map_test.exs @@ -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 @@ -197,4 +194,27 @@ defmodule RDF.PropertyMapTest do {~I, 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}) + assert Enum.member?(@example_property_map, {:bar, ~I}) + assert Enum.member?(@example_property_map, {:Baz, RDF.iri(EX.Baz)}) + refute Enum.member?(@example_property_map, {:bar, ~I}) + end + + test "Enum.reduce" do + assert Enum.reduce(@example_property_map, [], fn mapping, acc -> [mapping | acc] end) == + [ + {:foo, ~I}, + {:bar, ~I}, + {:Baz, RDF.iri(EX.Baz)} + ] + end + end end