Various fixes of RDF.Datatype.cast/1 implementations

This commit is contained in:
Marcel Otto 2018-09-13 00:57:57 +02:00
parent e0635b42ad
commit 97b1cec4aa
5 changed files with 17 additions and 16 deletions

View file

@ -44,12 +44,8 @@ defmodule RDF.Boolean do
!Decimal.equal?(literal.value, 0)
|> new()
is_xsd_double(datatype) or is_xsd_float(datatype) ->
literal.value not in [0.0, :nan]
|> new()
RDF.Numeric.type?(datatype) ->
literal.value
literal.value not in [0, 0.0, :nan]
|> new()
true ->

View file

@ -36,9 +36,10 @@ defmodule RDF.Integer do
new(1)
is_xsd_string(datatype) ->
literal.value
|> new()
|> canonical()
case Integer.parse(literal.value) do
{value, _} -> new(value)
_ -> nil
end
is_xsd_decimal(datatype) ->
literal.value

View file

@ -64,6 +64,7 @@ defmodule RDF.BooleanTest do
test "casting an integer" do
assert RDF.integer(0) |> RDF.Boolean.cast() == RDF.false
assert RDF.integer(1) |> RDF.Boolean.cast() == RDF.true
assert RDF.integer(42) |> RDF.Boolean.cast() == RDF.true
end
test "casting a decimal" do

View file

@ -74,6 +74,7 @@ defmodule RDF.DoubleTest do
test "casting a string" do
assert RDF.string("1.0") |> RDF.Double.cast() == RDF.double("1.0E0")
assert RDF.string("3.14") |> RDF.Double.cast() == RDF.double("3.14E0")
assert RDF.string("3.14E0") |> RDF.Double.cast() == RDF.double("3.14E0")
end
test "casting an integer" do

View file

@ -29,6 +29,8 @@ defmodule RDF.IntegerTest do
test "casting a string" do
assert RDF.string("0") |> RDF.Integer.cast() == RDF.integer(0)
assert RDF.string("042") |> RDF.Integer.cast() == RDF.integer(42)
assert RDF.string("3.14") |> RDF.Integer.cast() == RDF.integer(3)
end
test "casting an decimal" do