Add version-dependent tests for negative years in Dates and DateTimes

This commit is contained in:
Marcel Otto 2018-08-25 00:53:37 +02:00
parent 6bafb41cf9
commit fbbd52d1b0
3 changed files with 52 additions and 4 deletions

View file

@ -725,12 +725,19 @@ config :rdf,
## Caveats
The `Date` and `DateTime` modules of Elixir versions < 1.7.2 don't handle negative years properly. In case you're data contains negative years in `xsd:date` or `xsd:dateTime` literals, you'll have to upgrade to a newer Elixir version.
## Getting help
- [Documentation](http://hexdocs.pm/rdf)
- [Google Group](https://groups.google.com/d/forum/rdfex)
## TODO
There's still much to do for a complete RDF ecosystem for Elixir, which means there are plenty of opportunities for you to contribute. Here are some suggestions:
@ -744,11 +751,13 @@ There's still much to do for a complete RDF ecosystem for Elixir, which means th
- improve documentation
## Contributing
see [CONTRIBUTING](CONTRIBUTING.md) for details.
## License and Copyright
(c) 2017-2018 Marcel Otto. MIT Licensed, see [LICENSE](LICENSE.md) for details.

View file

@ -10,8 +10,8 @@ defmodule RDF.DateTest do
"2010-01-01+01:00" => { {~D[2010-01-01], "+01:00"} , nil , "2010-01-01+01:00" },
"2009-12-31-01:00" => { {~D[2009-12-31], "-01:00"} , nil , "2009-12-31-01:00" },
"2014-09-01-08:00" => { {~D[2014-09-01], "-08:00"} , nil , "2014-09-01-08:00" },
# TODO: DateTime doesn't support negative years (at least with the iso8601 conversion functions)
# "-2010-01-01Z" => { ~D[-2010-01-01] , nil , "-2010-01-01Z" },
# TODO: Dates on Elixir versions < 1.7.2 don't handle negative years correctly, so we test this conditionally below
# "-2010-01-01Z" => { {~D[-2010-01-01], "Z"} , nil , "-2010-01-01Z" },
},
invalid: ~w(
foo
@ -25,6 +25,28 @@ defmodule RDF.DateTest do
) ++ [true, false, 2010, 3.14, "2010-01-01Z foo", "foo 2010-01-01Z"]
unless Version.compare(System.version(), "1.7.2") == :lt do
test "negative years" do
assert Date.new("-2010-01-01Z") ==
%Literal{value: {~D[-2010-01-01], "Z"}, uncanonical_lexical: nil, datatype: RDF.NS.XSD.date, language: nil}
assert (Date.new("-2010-01-01Z") |> Literal.lexical) == "-2010-01-01Z"
assert (Date.new("-2010-01-01Z") |> Literal.canonical) == Date.new("-2010-01-01Z")
assert Literal.valid? Date.new("-2010-01-01Z")
assert Date.new("-2010-01-01") ==
%Literal{value: ~D[-2010-01-01], uncanonical_lexical: nil, datatype: RDF.NS.XSD.date, language: nil}
assert (Date.new("-2010-01-01") |> Literal.lexical) == "-2010-01-01"
assert (Date.new("-2010-01-01") |> Literal.canonical) == Date.new("-2010-01-01")
assert Literal.valid? Date.new("-2010-01-01")
assert Date.new("-2010-01-01+00:00") ==
%Literal{value: {~D[-2010-01-01], "Z"}, uncanonical_lexical: "-2010-01-01+00:00", datatype: RDF.NS.XSD.date, language: nil}
assert (Date.new("-2010-01-01+00:00") |> Literal.lexical) == "-2010-01-01+00:00"
assert (Date.new("-2010-01-01+00:00") |> Literal.canonical) == Date.new("-2010-01-01Z")
assert Literal.valid? Date.new("-2010-01-01+00:00")
end
end
describe "equality" do
test "two literals are equal when they have the same datatype and lexical form" do
[

View file

@ -22,8 +22,8 @@ defmodule RDF.DateTimeTest do
"2009-12-31T23:00:00-01:00" => { dt( "2010-01-01T00:00:00Z") , "2009-12-31T23:00:00-01:00", "2010-01-01T00:00:00Z" },
"2009-12-31T24:00:00" => { dt( "2010-01-01T00:00:00") , "2009-12-31T24:00:00" , "2010-01-01T00:00:00" },
"2009-12-31T24:00:00+00:00" => { dt( "2010-01-01T00:00:00Z") , "2009-12-31T24:00:00+00:00", "2010-01-01T00:00:00Z" },
# TODO: DateTime doesn't support negative years (at least with the iso8601 conversion functions)
# "-2010-01-01T00:00:00Z" => { dt("-2010-01-01T00:00:00Z") , nil, "-2010-01-01T00:00:00Z" },
# TODO: DateTimes on Elixir versions < 1.7.2 don't handle negative years correctly, so we test this conditionally below
# "-2010-01-01T00:00:00Z" => { dt("-2010-01-01T00:00:00Z") , nil, "-2010-01-01T00:00:00Z" },
},
invalid: ~w(
foo
@ -37,6 +37,23 @@ defmodule RDF.DateTimeTest do
2010_
) ++ [true, false, 2010, 3.14, "2010-01-01T00:00:00Z foo", "foo 2010-01-01T00:00:00Z"]
unless Version.compare(System.version(), "1.7.2") == :lt do
test "negative years" do
assert DateTime.new("-2010-01-01T00:00:00Z") ==
%Literal{value: dt("-2010-01-01T00:00:00Z"), uncanonical_lexical: nil, datatype: RDF.NS.XSD.dateTime, language: nil}
assert (DateTime.new("-2010-01-01T00:00:00Z") |> Literal.lexical) == "-2010-01-01T00:00:00Z"
assert (DateTime.new("-2010-01-01T00:00:00Z") |> Literal.canonical) ==
DateTime.new("-2010-01-01T00:00:00Z")
assert Literal.valid? DateTime.new("-2010-01-01T00:00:00Z")
assert DateTime.new("-2010-01-01T00:00:00+00:00") ==
%Literal{value: dt("-2010-01-01T00:00:00Z"), uncanonical_lexical: "-2010-01-01T00:00:00+00:00", datatype: RDF.NS.XSD.dateTime, language: nil}
assert (DateTime.new("-2010-01-01T00:00:00+00:00") |> Literal.lexical) == "-2010-01-01T00:00:00+00:00"
assert (DateTime.new("-2010-01-01T00:00:00+00:00") |> Literal.canonical) ==
DateTime.new("-2010-01-01T00:00:00Z")
assert Literal.valid? DateTime.new("-2010-01-01T00:00:00+00:00")
end
end
describe "equality" do
test "two literals are equal when they have the same datatype and lexical form" do