Fix new and new! on RDF.Literal and RDF.String when language nil given

This commit is contained in:
Marcel Otto 2018-08-26 05:46:18 +02:00
parent fbbd52d1b0
commit 3549a67a12
4 changed files with 44 additions and 7 deletions

View file

@ -5,13 +5,19 @@ defmodule RDF.String do
use RDF.Datatype, id: RDF.Datatype.NS.XSD.string
def new(value, opts) when is_list(opts), do: new(value, Map.new(opts))
def new(value, %{language: _} = opts), do: RDF.LangString.new!(value, opts)
def new(value, opts), do: super(value, opts)
def new(value, opts) when is_list(opts),
do: new(value, Map.new(opts))
def new(value, %{language: language} = opts) when not is_nil(language),
do: RDF.LangString.new!(value, opts)
def new(value, opts),
do: super(value, opts)
def new!(value, opts) when is_list(opts), do: new!(value, Map.new(opts))
def new!(value, %{language: _} = opts), do: RDF.LangString.new!(value, opts)
def new!(value, opts), do: super(value, opts)
def new!(value, opts) when is_list(opts),
do: new!(value, Map.new(opts))
def new!(value, %{language: language} = opts) when not is_nil(language),
do: RDF.LangString.new!(value, opts)
def new!(value, opts),
do: super(value, opts)
def build_literal_by_lexical(lexical, opts) do

View file

@ -67,7 +67,10 @@ defmodule RDF.Literal do
def new(value, opts) when is_list(opts),
do: new(value, Map.new(opts))
def new(value, %{language: language} = opts) when not is_nil(language) do
def new(value, %{language: nil} = opts),
do: new(value, Map.delete(opts, :language))
def new(value, %{language: language} = opts) do
if is_binary(value) do
if opts[:datatype] in [nil, @lang_string] do
RDF.LangString.new(value, opts)

View file

@ -17,6 +17,14 @@ defmodule RDF.StringTest do
assert RDF.String.new("foo", language: "en") ==
RDF.LangString.new("foo", language: "en")
end
test "nil as language is ignored" do
assert RDF.String.new("Eule", datatype: XSD.string, language: nil) ==
RDF.String.new("Eule", datatype: XSD.string)
assert RDF.String.new("Eule", language: nil) ==
RDF.String.new("Eule")
end
end
describe "new!" do
@ -24,6 +32,14 @@ defmodule RDF.StringTest do
assert RDF.String.new!("foo", language: "en") ==
RDF.LangString.new!("foo", language: "en")
end
test "nil as language is ignored" do
assert RDF.String.new!("Eule", datatype: XSD.string, language: nil) ==
RDF.String.new!("Eule", datatype: XSD.string)
assert RDF.String.new!("Eule", language: nil) ==
RDF.String.new!("Eule")
end
end
end

View file

@ -91,6 +91,18 @@ defmodule RDF.LiteralTest do
end
end
test "nil as language is ignored" do
assert Literal.new("Eule", datatype: XSD.string, language: nil) ==
Literal.new("Eule", datatype: XSD.string)
assert Literal.new("Eule", language: nil) ==
Literal.new("Eule")
assert Literal.new!("Eule", datatype: XSD.string, language: nil) ==
Literal.new!("Eule", datatype: XSD.string)
assert Literal.new!("Eule", language: nil) ==
Literal.new!("Eule")
end
test "construction of a rdf:langString works, but results in an invalid literal" do
assert %Literal{value: "Eule"} = literal = Literal.new("Eule", datatype: RDF.langString)
refute Literal.valid?(literal)