From 76b6f696133ec6a45269ca3d8f4639a6402e18b2 Mon Sep 17 00:00:00 2001 From: Marcel Otto Date: Sun, 17 May 2020 00:01:18 +0200 Subject: [PATCH] Fix raised BEAM error warning when namespace resolving top-level modules --- CHANGELOG.md | 5 +++++ lib/rdf/namespace.ex | 8 ++++++++ test/unit/vocabulary_namespace_test.exs | 6 ++++++ 3 files changed, 19 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f49cd14..50a524f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,11 @@ This project adheres to [Semantic Versioning](http://semver.org/) and - `RDF.Namespace.resolve_term/1` now returns ok or error tuples, but a new function `RDF.Namespace.resolve_term!/1` with the old behaviour was added +### Fixed + +- BEAM error warnings when trying to use top-level modules as vocabulary terms + + [Compare v0.7.1...HEAD](https://github.com/marcelotto/rdf-ex/compare/v0.7.1...HEAD) diff --git a/lib/rdf/namespace.ex b/lib/rdf/namespace.ex index 8b70ad1..24084fe 100644 --- a/lib/rdf/namespace.ex +++ b/lib/rdf/namespace.ex @@ -73,6 +73,14 @@ defmodule RDF.Namespace do defp do_resolve_term(RDF, term), do: do_resolve_term(RDF.NS.RDF, term) + defp do_resolve_term(Elixir, term) do + {:error, + %RDF.Namespace.UndefinedTermError{message: + "#{term} is not a RDF.Namespace; top-level modules can't be RDF.Namespaces" + } + } + end + defp do_resolve_term(namespace, term) do is_module = case Code.ensure_compiled(namespace) do diff --git a/test/unit/vocabulary_namespace_test.exs b/test/unit/vocabulary_namespace_test.exs index 7506529..f95df60 100644 --- a/test/unit/vocabulary_namespace_test.exs +++ b/test/unit/vocabulary_namespace_test.exs @@ -752,6 +752,12 @@ defmodule RDF.Vocabulary.NamespaceTest do assert_raise RDF.Namespace.UndefinedTermError, fn -> RDF.iri(ExUnit.Test) end end + test "resolving an top-level module" do + assert_raise RDF.Namespace.UndefinedTermError, + "ExUnit is not a RDF.Namespace; top-level modules can't be RDF.Namespaces", + fn -> RDF.iri(ExUnit) end + end + test "resolving an non-existing RDF.Namespace module" do assert_raise RDF.Namespace.UndefinedTermError, fn -> RDF.iri(NonExisting.Test) end end