From e383cf88ba749f39735aebf41993bed0c72262f7 Mon Sep 17 00:00:00 2001 From: Kip Cole Date: Thu, 6 Jan 2022 08:42:12 +1100 Subject: [PATCH] Sort units in errors to ensure deterministic ordering --- lib/cldr/unit.ex | 3 +++ test/unit_parse_test.exs | 6 +++--- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/cldr/unit.ex b/lib/cldr/unit.ex index 97529bf..54fda1a 100644 --- a/lib/cldr/unit.ex +++ b/lib/cldr/unit.ex @@ -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 @@ -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}" diff --git a/test/unit_parse_test.exs b/test/unit_parse_test.exs index f8844c5..b0ec768 100644 --- a/test/unit_parse_test.exs +++ b/test/unit_parse_test.exs @@ -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 @@ -41,7 +41,7 @@ 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, @@ -49,7 +49,7 @@ defmodule Cldr.Unit.Parse.Test do 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 \ No newline at end of file