From 45ab384677d05459849412c4582fdb288d4878a3 Mon Sep 17 00:00:00 2001 From: ParkerS00 Date: Thu, 3 Oct 2024 18:45:43 -0600 Subject: [PATCH] Handling the special case in ToQuantity where they pass in a negative 1 and other negative numbers, added tests to make sure changes worked. --- src/Humanizer.Tests/ToQuantityTests.cs | 59 +++++++++++++++++++++++++- src/Humanizer/ToQuantityExtensions.cs | 6 +-- 2 files changed, 61 insertions(+), 4 deletions(-) diff --git a/src/Humanizer.Tests/ToQuantityTests.cs b/src/Humanizer.Tests/ToQuantityTests.cs index c3172f0da..f420c8eba 100644 --- a/src/Humanizer.Tests/ToQuantityTests.cs +++ b/src/Humanizer.Tests/ToQuantityTests.cs @@ -1,56 +1,90 @@ -[UseCulture("en-US")] +[UseCulture("en-US")] public class ToQuantityTests { [Theory] [InlineData("case", 0, "0 cases")] [InlineData("case", 1, "1 case")] + [InlineData("case", -1, "-1 case")] [InlineData("case", 5, "5 cases")] + [InlineData("case", -5, "-5 cases")] [InlineData("man", 0, "0 men")] [InlineData("man", 1, "1 man")] + [InlineData("man", -1, "-1 man")] [InlineData("man", 2, "2 men")] + [InlineData("man", -2, "-2 men")] [InlineData("men", 2, "2 men")] + [InlineData("men", -2, "-2 men")] [InlineData("process", 2, "2 processes")] + [InlineData("process", -2, "-2 processes")] [InlineData("process", 1, "1 process")] + [InlineData("process", -1, "-1 process")] [InlineData("processes", 2, "2 processes")] + [InlineData("processes", -2, "-2 processes")] [InlineData("processes", 1, "1 process")] + [InlineData("processes", -1, "-1 process")] [InlineData("slice", 1, "1 slice")] + [InlineData("slice", -1, "-1 slice")] [InlineData("slice", 2, "2 slices")] + [InlineData("slice", -2, "-2 slices")] [InlineData("slices", 1, "1 slice")] + [InlineData("slices", -1, "-1 slice")] [InlineData("slices", 2, "2 slices")] + [InlineData("slices", -2, "-2 slices")] public void ToQuantity(string word, int quantity, string expected) => Assert.Equal(expected, word.ToQuantity(quantity)); [Theory] [InlineData("case", 0, "cases")] [InlineData("case", 1, "case")] + [InlineData("case", -1, "case")] [InlineData("case", 5, "cases")] + [InlineData("case", -5, "cases")] [InlineData("man", 0, "men")] [InlineData("man", 1, "man")] + [InlineData("man", -1, "man")] [InlineData("man", 2, "men")] + [InlineData("man", -2, "men")] [InlineData("men", 2, "men")] + [InlineData("men", -2, "men")] [InlineData("process", 2, "processes")] + [InlineData("process", -2, "processes")] [InlineData("process", 1, "process")] + [InlineData("process", -1, "process")] [InlineData("processes", 2, "processes")] + [InlineData("processes", -2, "processes")] [InlineData("processes", 1, "process")] + [InlineData("processes", -1, "process")] public void ToQuantityWithNoQuantity(string word, int quantity, string expected) => Assert.Equal(expected, word.ToQuantity(quantity, ShowQuantityAs.None)); [Theory] [InlineData("case", 0, "0 cases")] [InlineData("case", 1, "1 case")] + [InlineData("case", -1, "-1 case")] [InlineData("case", 5, "5 cases")] + [InlineData("case", -5, "-5 cases")] [InlineData("man", 0, "0 men")] [InlineData("man", 1, "1 man")] + [InlineData("man", -1, "-1 man")] [InlineData("man", 2, "2 men")] + [InlineData("man", -2, "-2 men")] [InlineData("men", 2, "2 men")] + [InlineData("men", -2, "-2 men")] [InlineData("process", 2, "2 processes")] + [InlineData("process", -2, "-2 processes")] [InlineData("process", 1, "1 process")] + [InlineData("process", -1, "-1 process")] [InlineData("processes", 2, "2 processes")] + [InlineData("processes", -2, "-2 processes")] [InlineData("processes", 1, "1 process")] + [InlineData("processes", -1, "-1 process")] public void ToQuantityNumeric(string word, int quantity, string expected) => Assert.Equal(expected, word.ToQuantity(quantity)); [Theory] + [InlineData("hour", -1, "-1 hour")] + [InlineData("hour", -0.5, "-0.5 hours")] + [InlineData("hour", -22.4, "-22.4 hours")] [InlineData("hour", 1, "1 hour")] [InlineData("hour", 0.5, "0.5 hours")] [InlineData("hour", 22.4, "22.4 hours")] @@ -61,27 +95,43 @@ public void ToDoubleQuantityNumeric(string word, double quantity, string expecte [Theory] [InlineData("case", 0, "zero cases")] [InlineData("case", 1, "one case")] + [InlineData("case", -1, "minus one case")] [InlineData("case", 5, "five cases")] + [InlineData("case", -5, "minus five cases")] [InlineData("man", 0, "zero men")] [InlineData("man", 1, "one man")] + [InlineData("man", -1, "minus one man")] [InlineData("man", 2, "two men")] + [InlineData("man", -2, "minus two men")] [InlineData("men", 2, "two men")] + [InlineData("men", -2, "minus two men")] [InlineData("process", 2, "two processes")] + [InlineData("process", -2, "minus two processes")] [InlineData("process", 1, "one process")] + [InlineData("process", -1, "minus one process")] [InlineData("processes", 2, "two processes")] + [InlineData("processes", -2, "minus two processes")] [InlineData("processes", 1200, "one thousand two hundred processes")] + [InlineData("processes", -1200, "minus one thousand two hundred processes")] [InlineData("processes", 1, "one process")] + [InlineData("processes", -1, "minus one process")] public void ToQuantityWords(string word, int quantity, string expected) => Assert.Equal(expected, word.ToQuantity(quantity, ShowQuantityAs.Words)); [Theory] [InlineData("case", 0, null, "0 cases")] [InlineData("case", 1, null, "1 case")] + [InlineData("case", -1, null, "-1 case")] [InlineData("case", 2, null, "2 cases")] + [InlineData("case", -2, null, "-2 cases")] [InlineData("case", 1, "N0", "1 case")] + [InlineData("case", -1, "N0", "-1 case")] [InlineData("case", 2, "N0", "2 cases")] + [InlineData("case", -2, "N0", "-2 cases")] [InlineData("case", 123456, "N0", "123,456 cases")] + [InlineData("case", -123456, "N0", "-123,456 cases")] [InlineData("case", 123456, "N2", "123,456.00 cases")] + [InlineData("case", -123456, "N2", "-123,456.00 cases")] [InlineData("dollar", 0, "C0", "$0 dollars")] [InlineData("dollar", 1, "C0", "$1 dollar")] [InlineData("dollar", 2, "C0", "$2 dollars")] @@ -92,13 +142,20 @@ public void ToQuantityWordsWithCurrentCultureFormatting(string word, int quantit [Theory] [InlineData("case", 0, "N0", "it-IT", "0 cases")] [InlineData("case", 1, "N0", "it-IT", "1 case")] + [InlineData("case", -1, "N0", "it-IT", "-1 case")] [InlineData("case", 2, "N0", "it-IT", "2 cases")] + [InlineData("case", -2, "N0", "it-IT", "-2 cases")] [InlineData("case", 1234567, "N0", "it-IT", "1.234.567 cases")] + [InlineData("case", -1234567, "N0", "it-IT", "-1.234.567 cases")] [InlineData("case", 1234567, "N2", "it-IT", "1.234.567,00 cases")] + [InlineData("case", -1234567, "N2", "it-IT", "-1.234.567,00 cases")] [InlineData("euro", 0, "C0", "es-ES", "0 € euros")] [InlineData("euro", 1, "C0", "es-ES", "1 € euro")] + [InlineData("euro", -1, "C0", "es-ES", "-1 € euro")] [InlineData("euro", 2, "C0", "es-ES", "2 € euros")] + [InlineData("euro", -2, "C0", "es-ES", "-2 € euros")] [InlineData("euro", 2, "C2", "es-ES", "2,00 € euros")] + [InlineData("euro", -2, "C2", "es-ES", "-2,00 € euros")] public void ToQuantityWordsWithCustomCultureFormatting(string word, int quantity, string format, string cultureCode, string expected) { var culture = new CultureInfo(cultureCode); diff --git a/src/Humanizer/ToQuantityExtensions.cs b/src/Humanizer/ToQuantityExtensions.cs index 52b086556..a69813b0f 100644 --- a/src/Humanizer/ToQuantityExtensions.cs +++ b/src/Humanizer/ToQuantityExtensions.cs @@ -1,4 +1,4 @@ -namespace Humanizer; +namespace Humanizer; /// /// Enumerates the ways of displaying a quantity value when converting @@ -62,7 +62,7 @@ public static string ToQuantity(this string input, long quantity, string? format static string ToQuantity(this string input, long quantity, ShowQuantityAs showQuantityAs = ShowQuantityAs.Numeric, string? format = null, IFormatProvider? formatProvider = null) { - var transformedInput = quantity == 1 + var transformedInput = quantity == 1 || quantity == -1 ? input.Singularize(inputIsKnownToBePlural: false) : input.Pluralize(inputIsKnownToBeSingular: false); @@ -93,7 +93,7 @@ static string ToQuantity(this string input, long quantity, ShowQuantityAs showQu /// public static string ToQuantity(this string input, double quantity, string? format = null, IFormatProvider? formatProvider = null) { - var transformedInput = quantity == 1 + var transformedInput = quantity == 1 || quantity == -1 ? input.Singularize(inputIsKnownToBePlural: false) : input.Pluralize(inputIsKnownToBeSingular: false);