Skip to content

Improve test coverage of the Date module #14524

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 23, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions lib/elixir/test/elixir/calendar/date_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ defmodule DateTest do
assert Date.compare(date4, date5) == :gt
assert Date.compare(date5, date4) == :lt
assert Date.compare(date5, date5) == :eq

assert_raise ArgumentError,
~r/cannot compare .*\n\n.* their calendars have incompatible day rollover moments/,
fn -> Date.compare(date1, %{date2 | calendar: FakeCalendar}) end
end

test "before?/2 and after?/2" do
Expand Down Expand Up @@ -180,10 +184,14 @@ defmodule DateTest do
|> Date.convert!(Calendar.Holocene)
|> Date.convert!(Calendar.ISO) == ~D[2000-01-01]

assert Date.convert(~D[2016-02-03], FakeCalendar) == {:error, :incompatible_calendars}

assert Date.convert(~N[2000-01-01 00:00:00], Calendar.Holocene) ==
{:ok, Calendar.Holocene.date(12000, 01, 01)}

assert Date.convert(~D[2016-02-03], FakeCalendar) == {:error, :incompatible_calendars}

assert_raise ArgumentError,
"cannot convert ~D[2016-02-03] to target calendar FakeCalendar, reason: :incompatible_calendars",
fn -> Date.convert!(~D[2016-02-03], FakeCalendar) end
end

test "add/2" do
Expand Down Expand Up @@ -214,6 +222,10 @@ defmodule DateTest do
date2 = Calendar.Holocene.date(12000, 01, 14)
assert Date.diff(date1, date2) == -13
assert Date.diff(date2, date1) == 13

assert_raise ArgumentError,
~r/cannot calculate the difference between .* because their calendars are not compatible/,
fn -> Date.diff(date1, %{date2 | calendar: FakeCalendar}) end
end

test "shift/2" do
Expand Down Expand Up @@ -249,6 +261,10 @@ defmodule DateTest do
"unknown unit :months. Expected :year, :month, :week, :day",
fn -> Date.shift(~D[2012-01-01], months: 12) end

assert_raise ArgumentError,
"unsupported value nil for :day. Expected an integer",
fn -> Date.shift(~D[2012-02-29], year: 1, day: nil) end

assert_raise ArgumentError,
"cannot shift date by time scale unit. Expected :year, :month, :week, :day",
fn -> Date.shift(~D[2012-02-29], %Duration{second: 86400}) end
Expand All @@ -266,4 +282,18 @@ defmodule DateTest do
Date.shift(date, month: 1)
end
end

test "utc_today/1" do
date = Date.utc_today()
assert date.year > 2020
assert date.calendar == Calendar.ISO

date = Date.utc_today(Calendar.ISO)
assert date.year > 2020
assert date.calendar == Calendar.ISO

date = Date.utc_today(Calendar.Holocene)
assert date.year > 12020
assert date.calendar == Calendar.Holocene
end
end