Skip to content

Commit

Permalink
Sort units in errors to ensure deterministic ordering
Browse files Browse the repository at this point in the history
  • Loading branch information
kipcole9 committed Jan 5, 2022
1 parent 4cd87c7 commit e383cf8
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
3 changes: 3 additions & 0 deletions lib/cldr/unit.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2434,6 +2434,7 @@ defmodule Cldr.Unit do
end

def unit_error(units) when is_list(units) do
units = Enum.sort(units)
{Cldr.UnknownUnitError, "The units #{inspect(units)} are not known."}
end

Expand Down Expand Up @@ -2571,6 +2572,8 @@ defmodule Cldr.Unit do

@doc false
def ambiguous_unit_error(unit, units) do
units = Enum.sort(units)

{
Cldr.Unit.AmbiguousUnitError,
"The string #{inspect String.trim(unit)} ambiguously resolves to #{inspect units}"
Expand Down
6 changes: 3 additions & 3 deletions test/unit_parse_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ defmodule Cldr.Unit.Parse.Test do
assert MyApp.Cldr.Unit.parse("2 m", only: :minute) == Cldr.Unit.new(2, :minute)
assert MyApp.Cldr.Unit.parse("2 m", only: [:year, :month, :day]) == Cldr.Unit.new(2, :month)
assert MyApp.Cldr.Unit.parse("2 m", only: :duration) ==
{:error, {Cldr.Unit.AmbiguousUnitError, "The string \"m\" ambiguously resolves to [:month, :minute]"}}
{:error, {Cldr.Unit.AmbiguousUnitError, "The string \"m\" ambiguously resolves to [:minute, :month]"}}
end

test "Parse an ambiguous unit with :except filter" do
Expand All @@ -41,15 +41,15 @@ defmodule Cldr.Unit.Parse.Test do

assert MyApp.Cldr.Unit.parse("2w", only: [:invalid, :also_invalid]) ==
{:error,
{Cldr.UnknownUnitError, "The units [:invalid, :also_invalid] are not known."}}
{Cldr.UnknownUnitError, "The units [:also_invalid, :invalid] are not known."}}

assert MyApp.Cldr.Unit.parse("2w", except: :invalid) ==
{:error,
{Cldr.UnknownUnitError, "The unit :invalid is not known."}}

assert MyApp.Cldr.Unit.parse("2w", except: [:invalid, :also_invalid]) ==
{:error,
{Cldr.UnknownUnitError, "The units [:invalid, :also_invalid] are not known."}}
{Cldr.UnknownUnitError, "The units [:also_invalid, :invalid] are not known."}}
end

end

0 comments on commit e383cf8

Please sign in to comment.