Add missing validity check to cast/1 functions
This commit is contained in:
parent
44a3ecb57a
commit
9e3fbde380
4 changed files with 13 additions and 8 deletions
|
@ -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
|
||||
|
|
|
@ -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}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue