Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handling the special case in ToQuantity where they pass in a negative… #1548

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 58 additions & 1 deletion src/Humanizer.Tests/ToQuantityTests.cs
Original file line number Diff line number Diff line change
@@ -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")]
Expand All @@ -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")]
Expand All @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions src/Humanizer/ToQuantityExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Humanizer;
namespace Humanizer;

/// <summary>
/// Enumerates the ways of displaying a quantity value when converting
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -93,7 +93,7 @@ static string ToQuantity(this string input, long quantity, ShowQuantityAs showQu
/// </example>
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);

Expand Down