From 3549a67a1274aad70622610e54526c3cfc6c50ff Mon Sep 17 00:00:00 2001 From: Marcel Otto Date: Sun, 26 Aug 2018 05:46:18 +0200 Subject: [PATCH] Fix new and new! on RDF.Literal and RDF.String when language nil given --- lib/rdf/datatypes/string.ex | 18 ++++++++++++------ lib/rdf/literal.ex | 5 ++++- test/unit/datatypes/string_test.exs | 16 ++++++++++++++++ test/unit/literal_test.exs | 12 ++++++++++++ 4 files changed, 44 insertions(+), 7 deletions(-) diff --git a/lib/rdf/datatypes/string.ex b/lib/rdf/datatypes/string.ex index 7ff53b1..168ab72 100644 --- a/lib/rdf/datatypes/string.ex +++ b/lib/rdf/datatypes/string.ex @@ -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 diff --git a/lib/rdf/literal.ex b/lib/rdf/literal.ex index 3d3dfbc..b07c8e7 100644 --- a/lib/rdf/literal.ex +++ b/lib/rdf/literal.ex @@ -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) diff --git a/test/unit/datatypes/string_test.exs b/test/unit/datatypes/string_test.exs index dedd030..c5b00de 100644 --- a/test/unit/datatypes/string_test.exs +++ b/test/unit/datatypes/string_test.exs @@ -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 diff --git a/test/unit/literal_test.exs b/test/unit/literal_test.exs index 32b9f19..e0ac470 100644 --- a/test/unit/literal_test.exs +++ b/test/unit/literal_test.exs @@ -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)