diff --git a/CHANGELOG.md b/CHANGELOG.md index 50a524f..9fc8e10 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/rdf/xsd/datatypes/numeric.ex b/lib/rdf/xsd/datatypes/numeric.ex index c1b27aa..dbfd2fe 100644 --- a/lib/rdf/xsd/datatypes/numeric.ex +++ b/lib/rdf/xsd/datatypes/numeric.ex @@ -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) diff --git a/test/unit/xsd/datatypes/numeric_test.exs b/test/unit/xsd/datatypes/numeric_test.exs index 2d0918b..98ee426 100644 --- a/test/unit/xsd/datatypes/numeric_test.exs +++ b/test/unit/xsd/datatypes/numeric_test.exs @@ -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