Fix deprecation warning with Decimal 1.6

warning: passing float to Decimal.new/1 is deprecated, use Decimal.from_float/1 instead
This commit is contained in:
Marcel Otto 2019-02-15 00:59:56 +01:00
parent 183ee3ed0d
commit 3086374a0d
5 changed files with 48 additions and 46 deletions

View file

@ -87,8 +87,8 @@ defmodule RDF.Numeric do
def equal_value?(_, _), do: nil
defp equal_decimal_value?(%D{} = left, %D{} = right), do: D.equal?(left, right)
defp equal_decimal_value?(%D{} = left, right), do: equal_decimal_value?(left, D.new(right))
defp equal_decimal_value?(left, %D{} = right), do: equal_decimal_value?(D.new(left), right)
defp equal_decimal_value?(%D{} = left, right), do: equal_decimal_value?(left, new_decimal(right))
defp equal_decimal_value?(left, %D{} = right), do: equal_decimal_value?(new_decimal(left), right)
defp equal_decimal_value?(_, _), do: nil
@ -137,8 +137,8 @@ defmodule RDF.Numeric do
def compare(_, _), do: nil
defp compare_decimal_value(%D{} = left, %D{} = right), do: D.cmp(left, right)
defp compare_decimal_value(%D{} = left, right), do: compare_decimal_value(left, D.new(right))
defp compare_decimal_value(left, %D{} = right), do: compare_decimal_value(D.new(left), right)
defp compare_decimal_value(%D{} = left, right), do: compare_decimal_value(left, new_decimal(right))
defp compare_decimal_value(left, %D{} = right), do: compare_decimal_value(new_decimal(left), right)
defp compare_decimal_value(_, _), do: nil
@ -359,7 +359,7 @@ defmodule RDF.Numeric do
def round(%Literal{datatype: datatype} = literal, precision) when is_xsd_double(datatype) do
if RDF.Double.valid?(literal) do
literal.value
|> D.new()
|> new_decimal()
|> xpath_round(precision)
|> D.to_float()
|> RDF.Double.new()
@ -370,7 +370,7 @@ defmodule RDF.Numeric do
if type?(datatype) and Literal.valid?(literal) do
if precision < 0 do
literal.value
|> D.new()
|> new_decimal()
|> xpath_round(precision)
|> D.to_integer()
|> RDF.Integer.new()
@ -559,4 +559,7 @@ defmodule RDF.Numeric do
end
end
defp new_decimal(value) when is_float(value), do: D.from_float(value)
defp new_decimal(value), do: D.new(value)
end

View file

@ -4,35 +4,34 @@ defmodule RDF.DecimalTest do
use RDF.Datatype.Test.Case, datatype: RDF.Decimal, id: RDF.NS.XSD.decimal,
valid: %{
# input => {value lexical canonicalized}
0 => {Elixir.Decimal.new(0.0), nil, "0.0"},
1 => {Elixir.Decimal.new(1.0), nil, "1.0"},
-1 => {Elixir.Decimal.new(-1.0), nil, "-1.0"},
1.0 => {Elixir.Decimal.new(1.0), nil, "1.0"},
-3.14 => {Elixir.Decimal.new(-3.14), nil, "-3.14"},
0.0E2 => {Elixir.Decimal.new(0.0), nil, "0.0"},
0 => {Elixir.Decimal.from_float(0.0), nil, "0.0"},
1 => {Elixir.Decimal.from_float(1.0), nil, "1.0"},
-1 => {Elixir.Decimal.from_float(-1.0), nil, "-1.0"},
1.0 => {Elixir.Decimal.from_float(1.0), nil, "1.0"},
-3.14 => {Elixir.Decimal.from_float(-3.14), nil, "-3.14"},
0.0E2 => {Elixir.Decimal.from_float(0.0), nil, "0.0"},
1.2E3 => {Elixir.Decimal.new("1200.0"), nil, "1200.0"},
Elixir.Decimal.new(1.0) => {Elixir.Decimal.new(1.0), nil, "1.0"},
Elixir.Decimal.new(1) => {Elixir.Decimal.new(1.0), nil, "1.0"},
Elixir.Decimal.new(1.2E3) => {Elixir.Decimal.new("1200.0"), nil, "1200.0"},
"1" => {Elixir.Decimal.new(1.0), "1", "1.0" },
"01" => {Elixir.Decimal.new(1.0), "01", "1.0" },
"0123" => {Elixir.Decimal.new(123.0), "0123", "123.0" },
"-1" => {Elixir.Decimal.new(-1.0), "-1", "-1.0" },
"1." => {Elixir.Decimal.new(1.0), "1.", "1.0" },
"1.0" => {Elixir.Decimal.new(1.0), nil, "1.0" },
"1.000000000" => {Elixir.Decimal.new(1.0), "1.000000000", "1.0" },
"+001.00" => {Elixir.Decimal.new(1.0), "+001.00", "1.0" },
"123.456" => {Elixir.Decimal.new(123.456), nil, "123.456" },
"0123.456" => {Elixir.Decimal.new(123.456), "0123.456", "123.456" },
"010.020" => {Elixir.Decimal.new(10.02), "010.020", "10.02" },
"2.3" => {Elixir.Decimal.new(2.3), nil, "2.3" },
"2.345" => {Elixir.Decimal.new(2.345), nil, "2.345" },
"2.234000005" => {Elixir.Decimal.new(2.234000005), nil, "2.234000005" },
"1.234567890123456789012345789"
=> {Elixir.Decimal.new("1.234567890123456789012345789"),
Elixir.Decimal.from_float(1.0) => {Elixir.Decimal.from_float(1.0), nil, "1.0"},
Elixir.Decimal.new(1) => {Elixir.Decimal.from_float(1.0), nil, "1.0"},
Elixir.Decimal.from_float(1.2E3) => {Elixir.Decimal.new("1200.0"), nil, "1200.0"},
"1" => {Elixir.Decimal.from_float(1.0), "1", "1.0" },
"01" => {Elixir.Decimal.from_float(1.0), "01", "1.0" },
"0123" => {Elixir.Decimal.from_float(123.0), "0123", "123.0" },
"-1" => {Elixir.Decimal.from_float(-1.0), "-1", "-1.0" },
"1." => {Elixir.Decimal.from_float(1.0), "1.", "1.0" },
"1.0" => {Elixir.Decimal.from_float(1.0), nil, "1.0" },
"1.000000000" => {Elixir.Decimal.from_float(1.0), "1.000000000", "1.0" },
"+001.00" => {Elixir.Decimal.from_float(1.0), "+001.00", "1.0" },
"123.456" => {Elixir.Decimal.from_float(123.456), nil, "123.456" },
"0123.456" => {Elixir.Decimal.from_float(123.456), "0123.456", "123.456" },
"010.020" => {Elixir.Decimal.from_float(10.02), "010.020", "10.02" },
"2.3" => {Elixir.Decimal.from_float(2.3), nil, "2.3" },
"2.345" => {Elixir.Decimal.from_float(2.345), nil, "2.345" },
"2.234000005" => {Elixir.Decimal.from_float(2.234000005), nil, "2.234000005" },
"1.234567890123456789012345789" => {Elixir.Decimal.new("1.234567890123456789012345789"),
nil, "1.234567890123456789012345789" },
".3" => {Elixir.Decimal.new(0.3), ".3", "0.3" },
"-.3" => {Elixir.Decimal.new(-0.3), "-.3", "-0.3" },
".3" => {Elixir.Decimal.from_float(0.3), ".3", "0.3" },
"-.3" => {Elixir.Decimal.from_float(-0.3), "-.3", "-0.3" },
},
invalid: ~w(foo 10.1e1 12.xyz 3,5 NaN Inf) ++ [true, false, "1.0 foo", "foo 1.0",
Elixir.Decimal.new("NaN"), Elixir.Decimal.new("Inf")]

View file

@ -388,7 +388,7 @@ defmodule RDF.NumericTest do
assert Numeric.abs(42) == RDF.integer(42)
assert Numeric.abs(-42) == RDF.integer(42)
assert Numeric.abs(-3.14) == RDF.double(3.14)
assert Numeric.abs(D.new(-3.14)) == RDF.decimal(3.14)
assert Numeric.abs(D.from_float(-3.14)) == RDF.decimal(3.14)
assert Numeric.abs("foo") == nil
assert Numeric.abs(:foo) == nil
end
@ -425,7 +425,7 @@ defmodule RDF.NumericTest do
test "coercion" do
assert Numeric.round(-42) == RDF.integer(-42)
assert Numeric.round(-3.14) == RDF.double(-3.0)
assert Numeric.round(D.new(3.14)) == RDF.decimal("3")
assert Numeric.round(D.from_float(3.14)) == RDF.decimal("3")
assert Numeric.round("foo") == nil
assert Numeric.round(:foo) == nil
end
@ -466,7 +466,7 @@ defmodule RDF.NumericTest do
test "coercion" do
assert Numeric.round(-42, 1) == RDF.integer(-42)
assert Numeric.round(-3.14, 1) == RDF.double(-3.1)
assert Numeric.round(D.new(3.14), 1) == RDF.decimal("3.1")
assert Numeric.round(D.from_float(3.14), 1) == RDF.decimal("3.1")
assert Numeric.round("foo", 1) == nil
assert Numeric.round(:foo, 1) == nil
end
@ -501,7 +501,7 @@ defmodule RDF.NumericTest do
test "coercion" do
assert Numeric.ceil(-42) == RDF.integer(-42)
assert Numeric.ceil(-3.14) == RDF.double("-3")
assert Numeric.ceil(D.new(3.14)) == RDF.decimal("4")
assert Numeric.ceil(D.from_float(3.14)) == RDF.decimal("4")
assert Numeric.ceil("foo") == nil
assert Numeric.ceil(:foo) == nil
end
@ -536,7 +536,7 @@ defmodule RDF.NumericTest do
test "coercion" do
assert Numeric.floor(-42) == RDF.integer(-42)
assert Numeric.floor(-3.14) == RDF.double("-4")
assert Numeric.floor(D.new(3.14)) == RDF.decimal("3")
assert Numeric.floor(D.from_float(3.14)) == RDF.decimal("3")
assert Numeric.floor("foo") == nil
assert Numeric.floor(:foo) == nil
end

View file

@ -188,14 +188,14 @@ defmodule RDF.EqualityTest do
{RDF.integer(42), D.new(42)},
{RDF.decimal(42), 42},
{RDF.decimal(3.14), 3.14},
{RDF.decimal(3.14), D.new(3.14)},
{RDF.decimal(3.14), D.from_float(3.14)},
{RDF.double(42), 42},
{RDF.double(3.14), 3.14},
{RDF.double(3.14), D.new(3.14)},
{RDF.double(3.14), D.from_float(3.14)},
]
@value_unequal_numerics_by_coercion [
{RDF.integer(3), 3.14},
{RDF.integer(3), D.new(3.14)},
{RDF.integer(3), D.from_float(3.14)},
{RDF.double(3.14), 3},
{RDF.decimal(3.14), 3},
]

View file

@ -36,7 +36,7 @@ defmodule RDF.TermTest do
end
test "with decimal" do
assert D.new(3.14) |> RDF.Term.coerce() == RDF.decimal(3.14)
assert D.from_float(3.14) |> RDF.Term.coerce() == RDF.decimal(3.14)
end
test "with datetime" do
@ -101,7 +101,7 @@ defmodule RDF.TermTest do
end
test "with decimal" do
assert D.new(3.14) |> RDF.Term.value() == D.new(3.14)
assert D.from_float(3.14) |> RDF.Term.value() == D.from_float(3.14)
end
test "with datetime" do