From 0baf411b6c621a1a435443af1c111f90199b583b Mon Sep 17 00:00:00 2001 From: Kip Cole Date: Mon, 30 Oct 2023 12:36:16 +0800 Subject: [PATCH] Add tests for Interval.DateTime.to_string/2 with :date_format and :time_format --- lib/cldr/interval/date_time.ex | 23 +++++++++++------------ test/interval_test.exs | 30 ++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 12 deletions(-) diff --git a/lib/cldr/interval/date_time.ex b/lib/cldr/interval/date_time.ex index 1640ac6..74bb96b 100644 --- a/lib/cldr/interval/date_time.ex +++ b/lib/cldr/interval/date_time.ex @@ -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 @@ -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 @@ -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 @@ -713,11 +713,11 @@ 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 @@ -725,7 +725,7 @@ defmodule Cldr.DateTime.Interval do 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.}" } @@ -733,7 +733,7 @@ defmodule Cldr.DateTime.Interval do 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}." } @@ -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 diff --git a/test/interval_test.exs b/test/interval_test.exs index b7a9ea4..dcc25cc 100644 --- a/test/interval_test.exs +++ b/test/interval_test.exs @@ -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