Skip to content

Commit

Permalink
Add variant tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kipcole9 committed Jul 25, 2024
1 parent d0f00cf commit 58ca64f
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/cldr/backend/date_time.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion lib/cldr/date.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"}
Expand Down Expand Up @@ -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"
Expand Down
38 changes: 38 additions & 0 deletions test/variant_test.exs
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 58ca64f

Please sign in to comment.