From 9e3fbde38080ef6d40e0da39b3ca2007edd84788 Mon Sep 17 00:00:00 2001 From: Marcel Otto Date: Fri, 17 Apr 2020 00:50:06 +0200 Subject: [PATCH] Add missing validity check to cast/1 functions --- lib/rdf/literal/datatypes/lang_string.ex | 2 +- lib/rdf/literal/datatypes/xsd.ex | 4 +++- test/unit/datatypes/lang_string_test.exs | 8 +++++--- test/unit/datatypes/xsd_test.exs | 7 ++++--- 4 files changed, 13 insertions(+), 8 deletions(-) diff --git a/lib/rdf/literal/datatypes/lang_string.ex b/lib/rdf/literal/datatypes/lang_string.ex index 062c30e..64a6ffb 100644 --- a/lib/rdf/literal/datatypes/lang_string.ex +++ b/lib/rdf/literal/datatypes/lang_string.ex @@ -86,7 +86,7 @@ defmodule RDF.LangString do def valid?(_), do: false @impl Datatype - def cast(%Literal{literal: %__MODULE__{}} = literal), do: literal + def cast(%Literal{literal: %__MODULE__{}} = literal), do: if valid?(literal), do: literal def cast(_), do: nil @impl Datatype diff --git a/lib/rdf/literal/datatypes/xsd.ex b/lib/rdf/literal/datatypes/xsd.ex index 03846ec..ffd1875 100644 --- a/lib/rdf/literal/datatypes/xsd.ex +++ b/lib/rdf/literal/datatypes/xsd.ex @@ -73,7 +73,9 @@ defmodule RDF.Literal.XSD do def valid?(_), do: false @impl RDF.Literal.Datatype - def cast(%Literal{literal: %unquote(xsd_datatype){}} = literal), do: literal + def cast(%Literal{literal: %unquote(xsd_datatype){}} = literal) do + if valid?(literal), do: literal + end def cast(%Literal{literal: literal}) do if casted_literal = unquote(xsd_datatype).cast(literal) do %Literal{literal: casted_literal} diff --git a/test/unit/datatypes/lang_string_test.exs b/test/unit/datatypes/lang_string_test.exs index 469eae7..6f67b70 100644 --- a/test/unit/datatypes/lang_string_test.exs +++ b/test/unit/datatypes/lang_string_test.exs @@ -156,13 +156,17 @@ defmodule RDF.LangStringTest do end describe "cast/1" do - test "when given a RDF.LangString literal" do + test "when given a valid RDF.LangString literal" do Enum.each @valid, fn {input, {_, language}} -> assert LangString.new(input, language: language) |> LangString.cast() == LangString.new(input, language: language) end end + test "when given an valid RDF.LangString literal" do + assert LangString.new("foo", language: nil) |> LangString.cast() == nil + end + test "when given a literal with a datatype which is not castable" do assert RDF.XSD.String.new("foo") |> LangString.cast() == nil assert RDF.XSD.Integer.new(12345) |> LangString.cast() == nil @@ -170,8 +174,6 @@ defmodule RDF.LangStringTest do test "with invalid literals" do assert RDF.XSD.Integer.new(3.14) |> LangString.cast() == nil - assert RDF.XSD.Decimal.new("NAN") |> LangString.cast() == nil - assert RDF.XSD.Double.new(true) |> LangString.cast() == nil end test "with non-coercible value" do diff --git a/test/unit/datatypes/xsd_test.exs b/test/unit/datatypes/xsd_test.exs index c12aec8..6b6816e 100644 --- a/test/unit/datatypes/xsd_test.exs +++ b/test/unit/datatypes/xsd_test.exs @@ -165,9 +165,10 @@ defmodule RDF.Literal.XSDTest do end test "with invalid literals" do - assert RDF.XSD.Integer.new(3.14) |> RDF.XSD.String.cast() == nil - assert RDF.XSD.Decimal.new("NAN") |> RDF.XSD.String.cast() == nil - assert RDF.XSD.Double.new(true) |> RDF.XSD.String.cast() == nil + assert RDF.XSD.Integer.new(3.14) |> RDF.XSD.Integer.cast() == nil + assert RDF.XSD.Decimal.new("NAN") |> RDF.XSD.Decimal.cast() == nil + assert RDF.XSD.Double.new(true) |> RDF.XSD.Double.cast() == nil + assert RDF.XSD.Boolean.new("42") |> RDF.XSD.Boolean.cast() == nil end test "with non-coercible value" do