Skip to content

Commit

Permalink
Add tests for Interval.DateTime.to_string/2 with :date_format and :ti…
Browse files Browse the repository at this point in the history
…me_format
  • Loading branch information
kipcole9 committed Oct 30, 2023
1 parent aabd837 commit 0baf411
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 12 deletions.
23 changes: 11 additions & 12 deletions lib/cldr/interval/date_time.ex
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ defmodule Cldr.DateTime.Interval do
* `:format` is one of `:short`, `:medium` or `:long` or a
specific format type or a string representation of an interval
format. The default is `:medium`.
* `:date_format` is any one of `:short`, `:medium`, `:long`, `:full`. If defined,
this option is used to format the date part of the date time. This option is
only acceptable if the `:format` option is not specified, or is specified as either
Expand Down Expand Up @@ -422,7 +422,7 @@ defmodule Cldr.DateTime.Interval do
* `:format` is one of `:short`, `:medium` or `:long` or a
specific format type or a string representing of an interval
format. The default is `:medium`.
* `:date_format` is any one of `:short`, `:medium`, `:long`, `:full`. If defined,
this option is used to format the date part of the date time. This option is
only acceptable if the `:format` option is not specified, or is specified as either
Expand Down Expand Up @@ -506,7 +506,7 @@ defmodule Cldr.DateTime.Interval do
* `:format` is one of `:short`, `:medium` or `:long` or a
specific format type or a string representation of an interval
format. The default is `:medium`.
* `:date_format` is any one of `:short`, `:medium`, `:long`, `:full`. If defined,
this option is used to format the date part of the date time. This option is
only acceptable if the `:format` option is not specified, or is specified as either
Expand Down Expand Up @@ -713,27 +713,27 @@ defmodule Cldr.DateTime.Interval do
# If the format is binary then neither date or time format can be
# specified.
defp validate_format(format, date_format, time_format) when is_binary(format) do
format_error(format, date_format, time_format)
{:error, format_error(format, date_format, time_format)}
end

defp validate_format(format, date_format, time_format) do
format_error(format, date_format, time_format)
{:error, format_error(format, date_format, time_format)}
end

@doc false
def format_error(format, date_format \\ nil, time_format \\ nil)

def format_error(format, nil, nil) do
{
Cldr.DateTime.UnresolvedFormat,
Cldr.DateTime.InvalidFormat,
"The interval format #{inspect(format)} is invalid. " <>
"Valid formats are #{inspect(@formats)} or an interval format string.}"
}
end

def format_error(format, date_format, time_format) when format in @formats do
{
Cldr.DateTime.UnresolvedFormat,
Cldr.DateTime.InvalidFormat,
":date_format and :time_format must be one of " <> inspect(@formats) <>
" if :format is also one of #{inspect @formats}. Found #{inspect date_format} and #{inspect time_format}."
}
Expand All @@ -742,25 +742,24 @@ defmodule Cldr.DateTime.Interval do
def format_error(format, date_format, time_format)
when is_binary(format) when not is_nil(date_format) and not is_nil(time_format) do
{
Cldr.DateTime.UnresolvedFormat, ":date_format and :time_format " <> error_string(format) <> "."
Cldr.DateTime.InvalidFormat, ":date_format and :time_format " <> error_string(format) <> "."
}
end

def format_error(format, date_format, nil) when is_binary(format) and not is_nil(date_format) do
{
Cldr.DateTime.UnresolvedFormat, ":date_format " <> error_string(format) <> "."
Cldr.DateTime.InvalidFormat, ":date_format " <> error_string(format) <> "."
}
end

def format_error(format, nil, time_format) when is_binary(format) and not is_nil(time_format) do
{
Cldr.DateTime.UnresolvedFormat, ":time_format " <> error_string(format) <> "."
Cldr.DateTime.InvalidFormat, ":time_format " <> error_string(format) <> "."
}
end

defp error_string(format) do
"cannot be specified when the interval format is a binary, Found: #{inspect(format)}"
"cannot be specified when the interval format is a binary or atom other than one of #{inspect @formats}. Found: #{inspect(format)}"
end


end
30 changes: 30 additions & 0 deletions test/interval_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,34 @@ defmodule Cldr.DateTime.Interval.Test do
assert {:ok, "12/31 – 1/2"} =
MyApp.Cldr.Date.Interval.to_string(~D[2023-12-31], ~D[2024-01-02], format: :short, style: :month_and_day)
end

test "Interval formats with different date and time formats" do
assert {:ok, "January 1, 2020, 12:00 AM – December 31, 2020, 10:00 AM"} =
MyApp.Cldr.DateTime.Interval.to_string(~U[2020-01-01 00:00:00.0Z], ~U[2020-12-31 10:00:00.0Z], format: :medium, date_format: :long, time_format: :short)

assert {:ok, "January 1, 2020, 12:00 AM – 10:00 AM"} =
MyApp.Cldr.DateTime.Interval.to_string(~U[2020-01-01 00:00:00.0Z], ~U[2020-01-01 10:00:00.0Z], format: :medium, date_format: :long, time_format: :short)

assert {:ok, "1/1/20, 12:00 AM – 12/31/20, 10:00 AM"} =
MyApp.Cldr.DateTime.Interval.to_string(~U[2020-01-01 00:00:00.0Z], ~U[2020-12-31 10:00:00.0Z], format: :medium, date_format: :short, time_format: :short)

assert {:ok, "1/1/20, 12:00 AM – 10:00 AM"} =
MyApp.Cldr.DateTime.Interval.to_string(~U[2020-01-01 00:00:00.0Z], ~U[2020-01-01 10:00:00.0Z], format: :medium, date_format: :short, time_format: :short)
end

test "Invalid :date_format and :time_format for intervals" do
assert {:error, {Cldr.DateTime.InvalidFormat, ":date_format and :time_format must be one of [:short, :medium, :long] if :format is also one of [:short, :medium, :long]. Found :short and \"invalid\"."}} =
MyApp.Cldr.DateTime.Interval.to_string(~U[2020-01-01 00:00:00.0Z], ~U[2020-01-01 10:00:00.0Z], format: :medium, date_format: :short, time_format: "invalid")

assert {:error, {Cldr.DateTime.InvalidFormat, ":date_format and :time_format must be one of [:short, :medium, :long] if :format is also one of [:short, :medium, :long]. Found \"invalid\" and :short."}} =
MyApp.Cldr.DateTime.Interval.to_string(~U[2020-01-01 00:00:00.0Z], ~U[2020-01-01 10:00:00.0Z], format: :medium, date_format: "invalid", time_format: :short)
end

test "If :format is a string or atom (other than standard formats) then :date_format and :time_format are not permitted" do
assert {:error, {Cldr.DateTime.InvalidFormat, ":date_format and :time_format cannot be specified when the interval format is a binary or atom other than one of [:short, :medium, :long]. Found: \"y-M-d - d\"."}} =
MyApp.Cldr.DateTime.Interval.to_string(~U[2020-01-01 00:00:00.0Z], ~U[2020-01-01 10:00:00.0Z], format: "y-M-d - d", date_format: :short, time_format: :long)

assert {:error, {Cldr.DateTime.InvalidFormat, ":date_format and :time_format cannot be specified when the interval format is a binary or atom other than one of [:short, :medium, :long]. Found: :gMY."}} =
MyApp.Cldr.DateTime.Interval.to_string(~U[2020-01-01 00:00:00.0Z], ~U[2020-01-01 10:00:00.0Z], format: :gMY, date_format: :short, time_format: :short)
end
end

0 comments on commit 0baf411

Please sign in to comment.