Add Decimal implementation of RDF.Term protocol
This commit is contained in:
parent
edfdb186d0
commit
d72c5ebd1f
3 changed files with 17 additions and 1 deletions
|
@ -100,6 +100,12 @@ defimpl RDF.Term, for: Float do
|
|||
def coerce(term), do: RDF.Double.new(term)
|
||||
end
|
||||
|
||||
defimpl RDF.Term, for: Decimal do
|
||||
def equal?(term1, term2), do: term1 == term2
|
||||
def equal_value?(term1, term2), do: RDF.Term.equal_value?(coerce(term1), term2)
|
||||
def coerce(term), do: RDF.Decimal.new(term)
|
||||
end
|
||||
|
||||
defimpl RDF.Term, for: DateTime do
|
||||
def equal?(term1, term2), do: term1 == term2
|
||||
def equal_value?(term1, term2), do: RDF.Term.equal_value?(coerce(term1), term2)
|
||||
|
|
|
@ -2,7 +2,7 @@ defmodule RDF.EqualityTest do
|
|||
use RDF.Test.Case
|
||||
|
||||
alias RDF.NS.XSD
|
||||
|
||||
alias Decimal, as: D
|
||||
|
||||
describe "RDF.IRI" do
|
||||
@term_equal_iris [
|
||||
|
@ -185,13 +185,17 @@ defmodule RDF.EqualityTest do
|
|||
@value_equal_numerics_by_coercion [
|
||||
{RDF.integer(42), 42},
|
||||
{RDF.integer(42), 42.0},
|
||||
{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.double(42), 42},
|
||||
{RDF.double(3.14), 3.14},
|
||||
{RDF.double(3.14), D.new(3.14)},
|
||||
]
|
||||
@value_unequal_numerics_by_coercion [
|
||||
{RDF.integer(3), 3.14},
|
||||
{RDF.integer(3), D.new(3.14)},
|
||||
{RDF.double(3.14), 3},
|
||||
{RDF.decimal(3.14), 3},
|
||||
]
|
||||
|
|
|
@ -3,6 +3,8 @@ defmodule RDF.TermTest do
|
|||
|
||||
doctest RDF.Term
|
||||
|
||||
alias Decimal, as: D
|
||||
|
||||
describe "coerce/1" do
|
||||
test "with RDF.IRI" do
|
||||
assert RDF.Term.coerce(~I<http://example.com/>) == ~I<http://example.com/>
|
||||
|
@ -33,6 +35,10 @@ defmodule RDF.TermTest do
|
|||
assert RDF.Term.coerce(3.14) == RDF.double(3.14)
|
||||
end
|
||||
|
||||
test "with decimal" do
|
||||
assert D.new(3.14) |> RDF.Term.coerce() == RDF.decimal(3.14)
|
||||
end
|
||||
|
||||
test "with datetime" do
|
||||
assert DateTime.from_iso8601("2002-04-02T12:00:00+00:00") |> elem(1) |> RDF.Term.coerce() ==
|
||||
DateTime.from_iso8601("2002-04-02T12:00:00+00:00") |> elem(1) |> RDF.datetime()
|
||||
|
|
Loading…
Reference in a new issue