Fix handling of vocabulary terms starting with an underscore

This commit is contained in:
Marcel Otto 2017-06-23 17:23:14 +02:00
parent 02202b49a7
commit 8ab2ab1e15
2 changed files with 47 additions and 10 deletions

View file

@ -304,14 +304,18 @@ defmodule RDF.Vocabulary.Namespace do
defp detect_case_violations(terms, data, base_uri) do
aliased_terms = aliased_terms(terms)
Enum.filter terms, fn
{term, true} ->
if not term in aliased_terms do
proper_case?(term, base_uri, Atom.to_string(term), data)
end
{term, original_term} ->
proper_case?(term, base_uri, original_term, data)
end
terms
|> Enum.filter(fn {term, _} ->
not(Atom.to_string(term) |> String.starts_with?("_"))
end)
|> Enum.filter(fn
{term, true} ->
if not term in aliased_terms do
proper_case?(term, base_uri, Atom.to_string(term), data)
end
{term, original_term} ->
proper_case?(term, base_uri, original_term, data)
end)
end
defp proper_case?(term, base_uri, uri_suffix, data) do
@ -502,7 +506,7 @@ defmodule RDF.Vocabulary.Namespace do
defp lowercase?(term) when is_atom(term),
do: Atom.to_string(term) |> lowercase?
defp lowercase?(term),
do: term =~ ~r/^\p{Ll}/u
do: term =~ ~r/^(_|\p{Ll})/u
defp strip_base_uri(uri, base_uri) do
if String.starts_with?(uri, base_uri) do

View file

@ -337,6 +337,20 @@ defmodule RDF.Vocabulary.NamespaceTest do
end
end
end
test "terms starting with an underscore are not checked" do
defmodule NSWithUnderscoreTerms do
use RDF.Vocabulary.Namespace
defvocab Example,
base_uri: "http://example.com/ex#",
case_violations: :fail,
data: RDF.Graph.new([
{~I<http://example.com/ex#_Foo>, ~I<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>, ~I<http://www.w3.org/1999/02/22-rdf-syntax-ns#Property>},
{~I<http://example.com/ex#_bar>, ~I<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>, ~I<http://www.w3.org/2000/01/rdf-schema#Resource>}
])
end
end
end
@ -589,11 +603,26 @@ defmodule RDF.Vocabulary.NamespaceTest do
assert RDF.uri(StrictExampleFromTerms.foo) == URI.parse("http://example.com/strict_from_terms#foo")
end
test "captitalized terms" do
test "capitalized terms" do
assert RDF.uri(ExampleFromGraph.Bar) == URI.parse("http://example.com/from_graph#Bar")
assert RDF.uri(ExampleFromNTriplesFile.Bar) == URI.parse("http://example.com/from_ntriples/Bar")
assert RDF.uri(StrictExampleFromTerms.Bar) == URI.parse("http://example.com/strict_from_terms#Bar")
end
test "terms starting with an underscore" do
defmodule NSwithUnderscoreTerms do
use RDF.Vocabulary.Namespace
defvocab Example,
base_uri: "http://example.com/ex#",
terms: ~w[_foo]
end
alias NSwithUnderscoreTerms.Example
alias TestNS.EX
assert Example._foo == ~I<http://example.com/ex#_foo>
assert Example._foo(EX.S, 1) == RDF.description(EX.S, Example._foo, 1)
end
end
@ -607,6 +636,10 @@ defmodule RDF.Vocabulary.NamespaceTest do
assert RDF.uri(NonStrictExampleFromTerms.Random) == URI.parse("http://example.com/non_strict_from_terms#Random")
end
test "undefined terms starting with an underscore" do
assert NonStrictExampleFromTerms._random == URI.parse("http://example.com/non_strict_from_terms#_random")
end
test "defined lowercase terms" do
assert NonStrictExampleFromTerms.foo == URI.parse("http://example.com/non_strict_from_terms#foo")
end