From 15961b8fef57a568489bbcb355ebfc25d3dd6682 Mon Sep 17 00:00:00 2001 From: Marcel Otto Date: Sat, 30 Mar 2019 02:01:30 +0100 Subject: [PATCH] Fix detection of vocabulary namespaces --- lib/rdf/prefix_map.ex | 2 +- lib/rdf/vocabulary_namespace.ex | 5 +++++ test/unit/prefix_map_test.exs | 10 ++++++++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/rdf/prefix_map.ex b/lib/rdf/prefix_map.ex index 2992504..f4c1433 100644 --- a/lib/rdf/prefix_map.ex +++ b/lib/rdf/prefix_map.ex @@ -41,7 +41,7 @@ defmodule RDF.PrefixMap do do: normalize({prefix, IRI.new(namespace)}) defp normalize({prefix, namespace}) when is_atom(namespace) do - if function_exported?(namespace, :__base_iri__, 0) do + if RDF.Vocabulary.Namespace.vocabulary_namespace?(namespace) do normalize({prefix, apply(namespace, :__base_iri__, [])}) else raise ArgumentError, diff --git a/lib/rdf/vocabulary_namespace.ex b/lib/rdf/vocabulary_namespace.ex index 7c92a24..9809c5c 100644 --- a/lib/rdf/vocabulary_namespace.ex +++ b/lib/rdf/vocabulary_namespace.ex @@ -572,4 +572,9 @@ defmodule RDF.Vocabulary.Namespace do def term_to_iri(base_iri, term), do: RDF.iri(base_iri <> term) + @doc false + def vocabulary_namespace?(name) do + Code.ensure_loaded?(name) && function_exported?(name, :__base_iri__, 0) + end + end diff --git a/test/unit/prefix_map_test.exs b/test/unit/prefix_map_test.exs index cbe3816..7d73a08 100644 --- a/test/unit/prefix_map_test.exs +++ b/test/unit/prefix_map_test.exs @@ -70,12 +70,22 @@ defmodule RDF.PrefixMapTest do assert PrefixMap.add(@example1, :ex, EX) == {:ok, @example4} end + test "when the IRI namespace is given as a RDF.Vocabulary.Namespace which is not loaded yet" do + assert {:ok, prefix_map} = PrefixMap.new |> PrefixMap.add(:rdfs, RDF.NS.RDFS) + assert PrefixMap.has_prefix?(prefix_map, :rdfs) + end + test "when the IRI namespace is given as an atom" do assert_raise ArgumentError, "Invalid prefix mapping for :ex, :foo is not a vocabulary namespace", fn -> PrefixMap.add(@example1, :ex, :foo) end end + @tag skip: "TODO: " + test "when a default namespace is given" do + assert PrefixMap.add(@example1, :_, @ex_ns2) == {:ok, @example5} + end + test "when a mapping of the given prefix to the same namespace already exists" do assert PrefixMap.add(@example2, :ex2, "http://example.com/bar#") == {:ok, @example2} end