From 58ca64f35225c2ebd0d09d8656aff48822b0df3b Mon Sep 17 00:00:00 2001 From: Kip Cole Date: Fri, 26 Jul 2024 09:49:03 +1000 Subject: [PATCH] Add variant tests --- lib/cldr/backend/date_time.ex | 4 ++-- lib/cldr/date.ex | 8 +++++++- test/variant_test.exs | 38 +++++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 test/variant_test.exs diff --git a/lib/cldr/backend/date_time.ex b/lib/cldr/backend/date_time.ex index 5ba4251..cc39310 100644 --- a/lib/cldr/backend/date_time.ex +++ b/lib/cldr/backend/date_time.ex @@ -197,7 +197,7 @@ defmodule Cldr.DateAndTime.Backend do `Cldr.Date.available_formats/3`. See [here](README.md#date-time-and-datetime-localization-formats) for more information about specifying formats. - * `:locale:` any locale returned by `Cldr.known_locale_names/1`. + * `:locale` any locale returned by `Cldr.known_locale_names/1`. The default is `Cldr.get_locale/0`. * `:number_system` a number system into which the formatted date digits @@ -282,7 +282,7 @@ defmodule Cldr.DateAndTime.Backend do `Cldr.Date.available_formats/3`. See [here](README.md#date-time-and-datetime-localization-formats) for more information about specifying formats. - * `:locale:` any locale returned by `Cldr.known_locale_names/1`. + * `:locale` any locale returned by `Cldr.known_locale_names/1`. The default is `Cldr.get_locale/0`. * `:number_system` a number system into which the formatted date digits diff --git a/lib/cldr/date.ex b/lib/cldr/date.ex index bf3f206..7bcabf1 100644 --- a/lib/cldr/date.ex +++ b/lib/cldr/date.ex @@ -68,7 +68,7 @@ defmodule Cldr.Date do `Cldr.Date.available_formats/3`. See [here](README.md#date-time-and-datetime-localization-formats) for more information about specifying formats. - * `:locale:` any locale returned by `Cldr.known_locale_names/1`. + * `:locale` any locale returned by `Cldr.known_locale_names/1`. The default is `Cldr.get_locale/0`. * `:number_system` a number system into which the formatted date digits @@ -109,6 +109,9 @@ defmodule Cldr.Date do iex> Cldr.Date.to_string(~D[2017-07-10], MyApp.Cldr, format: :short, locale: "fr") {:ok, "10/07/2017"} + iex> Cldr.Date.to_string(~D[2024-03-01], format: :yMd, prefer: :variant, locale: "en-CA") + {:ok, "1/3/2024"} + # A partial date with a derived "best match" format iex> Cldr.Date.to_string(%{year: 2024, month: 6}, MyApp.Cldr, locale: "fr") {:ok, "06/2024"} @@ -237,6 +240,9 @@ defmodule Cldr.Date do iex> Cldr.Date.to_string!(~D[2017-07-10], MyApp.Cldr, format: :short, locale: "fr") "10/07/2017" + iex> Cldr.Date.to_string!(~D[2024-03-01], format: :yMd, prefer: :variant, locale: "en-CA") + "1/3/2024" + # A partial date with a derived "best match" format iex> Cldr.Date.to_string!(%{year: 2024, month: 6}, MyApp.Cldr, locale: "fr") "06/2024" diff --git a/test/variant_test.exs b/test/variant_test.exs new file mode 100644 index 0000000..716b24e --- /dev/null +++ b/test/variant_test.exs @@ -0,0 +1,38 @@ +defmodule Cldr.DateTime.VariantTest do + use ExUnit.Case, async: true + + test "Date interval variant" do + assert {:ok, "1/1/2024 – 1/2/2024"} = + Cldr.Date.Interval.to_string ~D[2024-01-01], ~D[2024-02-01], format: :yMd, prefer: :variant, locale: "en-CA" + + assert {:ok, "1/1/2024–2/1/2024"} = + Cldr.Date.Interval.to_string ~D[2024-01-01], ~D[2024-02-01], format: :yMd, prefer: :default, locale: "en-CA" + + assert {:ok, "1/1/2024 – 1/2/2024"} = + Cldr.Date.Interval.to_string ~D[2024-01-01], ~D[2024-02-01], format: :yMd, prefer: [:variant], locale: "en-CA" + + assert {:ok, "1/1/2024–2/1/2024"} = + Cldr.Date.Interval.to_string ~D[2024-01-01], ~D[2024-02-01], format: :yMd, prefer: [:default], locale: "en-CA" + + assert {:ok, "1/1/2024–2/1/2024"} = + Cldr.Date.Interval.to_string ~D[2024-01-01], ~D[2024-02-01], format: :yMd, locale: "en-CA" + end + + test "Date variant" do + assert {:ok, "1/3/2024"} = + Cldr.Date.to_string ~D[2024-03-01], format: :yMd, prefer: :variant, locale: "en-CA" + + assert {:ok, "2024-03-01"} = + Cldr.Date.to_string ~D[2024-03-01], format: :yMd, prefer: :default, locale: "en-CA" + + assert {:ok, "1/3/2024"} = + Cldr.Date.to_string ~D[2024-03-01], format: :yMd, prefer: [:variant], locale: "en-CA" + + assert {:ok, "2024-03-01"} = + Cldr.Date.to_string ~D[2024-03-01], format: :yMd, prefer: [:default], locale: "en-CA" + + assert {:ok, "2024-03-01"} = + Cldr.Date.to_string ~D[2024-03-01], format: :yMd, locale: "en-CA" + end + +end \ No newline at end of file