diff --git a/lib/rdf/datatype.ex b/lib/rdf/datatype.ex index 11b7d0c..dd90806 100644 --- a/lib/rdf/datatype.ex +++ b/lib/rdf/datatype.ex @@ -204,6 +204,11 @@ defmodule RDF.Datatype do def valid?(_), do: false + def equal_value?(%Literal{uncanonical_lexical: lexical1, datatype: @id, value: nil}, + %Literal{uncanonical_lexical: lexical2, datatype: @id}) do + lexical1 == lexical2 + end + def equal_value?(%Literal{datatype: @id} = literal1, %Literal{datatype: @id} = literal2) do canonical(literal1).value == canonical(literal2).value end diff --git a/lib/rdf/datatypes/numeric.ex b/lib/rdf/datatypes/numeric.ex index 91e1375..b1df159 100644 --- a/lib/rdf/datatypes/numeric.ex +++ b/lib/rdf/datatypes/numeric.ex @@ -47,12 +47,6 @@ defmodule RDF.Numeric do def literal?(%Literal{datatype: datatype}), do: type?(datatype) def literal?(_), do: false -# @doc """ -# Returns if a given datatype is a numeric datatype for integers. -# """ -# def integer_type?(type), do: MapSet.member?(@integer_types, type) - - @doc """ Tests for numeric value equality of two numeric literals. @@ -66,6 +60,11 @@ defmodule RDF.Numeric do """ def equal_value?(left, right) + def equal_value?(%Literal{uncanonical_lexical: lexical1, datatype: dt, value: nil}, + %Literal{uncanonical_lexical: lexical2, datatype: dt}) do + lexical1 == lexical2 + end + def equal_value?(%Literal{datatype: left_datatype, value: left}, %Literal{datatype: right_datatype, value: right}) when is_xsd_decimal(left_datatype) or is_xsd_decimal(right_datatype), diff --git a/test/unit/equality_test.exs b/test/unit/equality_test.exs index 12eb962..161d5fe 100644 --- a/test/unit/equality_test.exs +++ b/test/unit/equality_test.exs @@ -87,20 +87,28 @@ defmodule RDF.EqualityTest do describe "RDF.Boolean" do @term_equal_booleans [ - {RDF.true, RDF.true}, - {RDF.false, RDF.false}, + {RDF.true, RDF.true}, + {RDF.false, RDF.false}, + # invalid literals + {RDF.boolean("foo"), RDF.boolean("foo")}, ] @term_unequal_booleans [ - {RDF.true, RDF.false}, - {RDF.false, RDF.true}, + {RDF.true, RDF.false}, + {RDF.false, RDF.true}, + # invalid literals + {RDF.boolean("foo"), RDF.boolean("bar")}, ] @value_equal_booleans [ {RDF.true, RDF.boolean("TRUE")}, {RDF.boolean(1), RDF.true}, + # invalid literals + {RDF.boolean("foo"), RDF.boolean("foo")}, ] @value_unequal_booleans [ {RDF.true, RDF.boolean("FALSE")}, {RDF.boolean(0), RDF.true}, + # invalid literals + {RDF.boolean("foo"), RDF.boolean("bar")}, ] @incomparable_booleans [ {RDF.true, nil}, @@ -120,9 +128,17 @@ defmodule RDF.EqualityTest do @term_equal_numerics [ {RDF.integer(42), RDF.integer(42)}, {RDF.integer("042"), RDF.integer("042")}, + # invalid literals + {RDF.integer("foo"), RDF.integer("foo")}, + {RDF.decimal("foo"), RDF.decimal("foo")}, + {RDF.double("foo"), RDF.double("foo")}, ] @term_unequal_numerics [ {RDF.integer(1), RDF.integer(2)}, + # invalid literals + {RDF.integer("foo"), RDF.integer("bar")}, + {RDF.decimal("foo"), RDF.decimal("bar")}, + {RDF.double("foo"), RDF.double("bar")}, ] @value_equal_numerics [ {RDF.integer("42"), RDF.integer("042")}, @@ -133,10 +149,18 @@ defmodule RDF.EqualityTest do {RDF.double(3.14), RDF.decimal(3.14)}, {RDF.double("+0"), RDF.double("-0")}, {RDF.decimal("+0"), RDF.decimal("-0")}, + # invalid literals + {RDF.integer("foo"), RDF.integer("foo")}, + {RDF.decimal("foo"), RDF.decimal("foo")}, + {RDF.double("foo"), RDF.double("foo")}, ] @value_unequal_numerics [ {RDF.integer("1"), RDF.double("1.1")}, {RDF.integer("1"), RDF.decimal("1.1")}, + # invalid literals + {RDF.integer("foo"), RDF.integer("bar")}, + {RDF.decimal("foo"), RDF.decimal("bar")}, + {RDF.double("foo"), RDF.double("bar")}, ] @incomparable_numerics [ {RDF.string("42"), RDF.integer(42)}, @@ -154,9 +178,13 @@ defmodule RDF.EqualityTest do @term_equal_datetimes [ {RDF.date_time("2002-04-02T12:00:00-01:00"), RDF.date_time("2002-04-02T12:00:00-01:00")}, {RDF.date_time("2002-04-02T12:00:00"), RDF.date_time("2002-04-02T12:00:00")}, + # invalid literals + {RDF.date_time("foo"), RDF.date_time("foo")}, ] @term_unequal_datetimes [ {RDF.date_time("2002-04-02T12:00:00"), RDF.date_time("2002-04-02T17:00:00")}, + # invalid literals + {RDF.date_time("foo"), RDF.date_time("bar")}, ] @value_equal_datetimes [ {RDF.date_time("2002-04-02T12:00:00-01:00"), RDF.date_time("2002-04-02T17:00:00+04:00")}, @@ -164,9 +192,13 @@ defmodule RDF.EqualityTest do {RDF.date_time("1999-12-31T24:00:00"), RDF.date_time("2000-01-01T00:00:00")}, # TODO: Assume that the dynamic context provides an implicit timezone value of -05:00 # {RDF.date_time("2002-04-02T12:00:00"), RDF.date_time("2002-04-02T23:00:00+06:00")}, + # invalid literals + {RDF.date_time("foo"), RDF.date_time("foo")}, ] @value_unequal_datetimes [ {RDF.date_time("2005-04-04T24:00:00"), RDF.date_time("2005-04-04T00:00:00")}, + # invalid literals + {RDF.date_time("foo"), RDF.date_time("bar")}, ] @incomparable_datetimes [ {RDF.string("2002-04-02T12:00:00-01:00"), RDF.date_time("2002-04-02T12:00:00-01:00")},