Fix failing of numeric operations with invalid literals

This commit is contained in:
Marcel Otto 2020-05-20 00:32:20 +02:00
parent fa130bf14e
commit accf66d75f
3 changed files with 12 additions and 2 deletions

View file

@ -15,6 +15,7 @@ This project adheres to [Semantic Versioning](http://semver.org/) and
### Fixed
- numeric operations on invalid numeric literals no longer fail, but return `nil` instead
- BEAM error warnings when trying to use top-level modules as vocabulary terms

View file

@ -517,7 +517,8 @@ defmodule RDF.XSD.Numeric do
defp arithmetic_operation(op, %Literal{literal: literal1}, literal2, fun), do: arithmetic_operation(op, literal1, literal2, fun)
defp arithmetic_operation(op, literal1, %Literal{literal: literal2}, fun), do: arithmetic_operation(op, literal1, literal2, fun)
defp arithmetic_operation(op, %datatype1{} = literal1, %datatype2{} = literal2, fun) do
if datatype?(datatype1) and datatype?(datatype2) do
if datatype?(datatype1) and datatype?(datatype2) and
Literal.Datatype.valid?(literal1) and Literal.Datatype.valid?(literal2) do
result_type = result_type(op, datatype1, datatype2)
{arg1, arg2} = type_conversion(literal1, literal2, result_type)
result = fun.(arg1.value, arg2.value, result_type)

View file

@ -130,7 +130,7 @@ defmodule RDF.XSD.NumericTest do
assert Numeric.add(@negative_infinity, XSD.double(3.14)) == @negative_infinity
assert Numeric.add(XSD.double(0), @negative_infinity) == @negative_infinity
assert Numeric.add(XSD.double(3.14), @negative_infinity) == @negative_infinity
assert Numeric.add(@negative_infinity, Age.new(0)) == @negative_infinity
assert Numeric.add(@negative_infinity, Age.new(1)) == @negative_infinity
end
test "if both operands are INF, INF is returned" do
@ -157,6 +157,14 @@ defmodule RDF.XSD.NumericTest do
assert Numeric.add(:foo, 42) == nil
assert Numeric.add(:foo, :bar) == nil
end
test "with invalid numeric literals" do
refute Numeric.add(XSD.integer("foo"), XSD.integer("bar"))
refute Numeric.add(XSD.integer("foo"), XSD.integer(1))
refute Numeric.add(XSD.integer(1), XSD.integer("foo"))
refute Numeric.add(XSD.integer(1), XSD.byte(300))
refute Numeric.add(XSD.integer(1), Age.new(200))
end
end
describe "subtract/2" do