Skip to content

Commit

Permalink
Added an extra test to show that some cultures (en) ignore Grammatica…
Browse files Browse the repository at this point in the history
…l Gender for Ordinalizer.

Fixed some typos and code clean-up.
  • Loading branch information
akamud committed Apr 15, 2014
1 parent 18d47a5 commit b353f33
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 37 deletions.
4 changes: 2 additions & 2 deletions src/Humanizer.Tests/Localisation/es/OrdinalizeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void OrdinalizeStringFeminine(string number, string ordinalized)
[InlineData(102, "102º")]
[InlineData(103, "103º")]
[InlineData(1001, "1001º")]
public void OrdanizeNumber(int number, string ordinalized)
public void OrdinalizeNumber(int number, string ordinalized)
{
Assert.Equal(number.Ordinalize(GrammaticalGender.Masculine), ordinalized);
}
Expand All @@ -83,7 +83,7 @@ public void OrdanizeNumber(int number, string ordinalized)
[InlineData(102, "102ª")]
[InlineData(103, "103ª")]
[InlineData(1001, "1001ª")]
public void OrdanizeNumberFeminine(int number, string ordinalized)
public void OrdinalizeNumberFeminine(int number, string ordinalized)
{
Assert.Equal(number.Ordinalize(GrammaticalGender.Feminine), ordinalized);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Humanizer.Tests/Localisation/pt-BR/OrdinalizeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void OrdinalizeStringFeminine(string number, string ordinalized)
[InlineData(102, "102º")]
[InlineData(103, "103º")]
[InlineData(1001, "1001º")]
public void OrdanizeNumber(int number, string ordinalized)
public void OrdinalizeNumber(int number, string ordinalized)
{
Assert.Equal(number.Ordinalize(GrammaticalGender.Masculine), ordinalized);
}
Expand All @@ -83,7 +83,7 @@ public void OrdanizeNumber(int number, string ordinalized)
[InlineData(102, "102ª")]
[InlineData(103, "103ª")]
[InlineData(1001, "1001ª")]
public void OrdanizeNumberFeminine(int number, string ordinalized)
public void OrdinalizeNumberFeminine(int number, string ordinalized)
{
Assert.Equal(number.Ordinalize(GrammaticalGender.Feminine), ordinalized);
}
Expand Down
46 changes: 44 additions & 2 deletions src/Humanizer.Tests/OrdinalizeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public void OrdinalizeString(string number, string ordinalized)
Assert.Equal(number.Ordinalize(), ordinalized);
}

[Theory]
[InlineData(0, "0th")]
[InlineData(1, "1st")]
[InlineData(2, "2nd")]
Expand Down Expand Up @@ -72,10 +73,51 @@ public void OrdinalizeString(string number, string ordinalized)
[InlineData(110, "110th")]
[InlineData(1000, "1000th")]
[InlineData(1001, "1001st")]
[Theory]
public void OrdanizeNumber(int number, string ordinalized)
public void OrdinalizeNumber(int number, string ordinalized)
{
Assert.Equal(number.Ordinalize(), ordinalized);
}

[Theory]
[InlineData(0)]
[InlineData(1)]
[InlineData(8)]
[InlineData(9)]
[InlineData(10)]
[InlineData(11)]
[InlineData(12)]
[InlineData(23)]
[InlineData(24)]
[InlineData(100)]
[InlineData(101)]
[InlineData(102)]
[InlineData(1001)]
public void OrdinalizeNumberGenderIndifferent(int number)
{
string masculineOrdinalized = number.Ordinalize(GrammaticalGender.Masculine);
string feminineOrdinalized = number.Ordinalize(GrammaticalGender.Feminine);
Assert.Equal(masculineOrdinalized, feminineOrdinalized);
}

[Theory]
[InlineData("0")]
[InlineData("1")]
[InlineData("8")]
[InlineData("9")]
[InlineData("10")]
[InlineData("11")]
[InlineData("12")]
[InlineData("23")]
[InlineData("24")]
[InlineData("100")]
[InlineData("101")]
[InlineData("102")]
[InlineData("1001")]
public void OrdinalizeStringGenderIndifferent(string number)
{
string masculineOrdinalized = number.Ordinalize(GrammaticalGender.Masculine);
string feminineOrdinalized = number.Ordinalize(GrammaticalGender.Feminine);
Assert.Equal(masculineOrdinalized, feminineOrdinalized);
}
}
}
8 changes: 4 additions & 4 deletions src/Humanizer/Humanizer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,10 @@
<Compile Include="Localisation\NumberToWords\PolishNumberToWordsConverter.cs" />
<Compile Include="Localisation\NumberToWords\RussianNumberToWordsConverter.cs" />
<Compile Include="Localisation\Formatters\HebrewFormatter.cs" />
<Compile Include="Localisation\Ordinalize\SpanishOrdinalizeConverter.cs" />
<Compile Include="Localisation\Ordinalize\DefaultOrdinalizeConverter.cs" />
<Compile Include="Localisation\Ordinalize\EnglishOrdinalizeConverter.cs" />
<Compile Include="Localisation\Ordinalize\BrazilianPortugueseOrdinalizeConverter.cs" />
<Compile Include="Localisation\Ordinalizer\SpanishOrdinalizer.cs" />
<Compile Include="Localisation\Ordinalizer\DefaultOrdinalizer.cs" />
<Compile Include="Localisation\Ordinalizer\EnglishOrdinalizer.cs" />
<Compile Include="Localisation\Ordinalizer\BrazilianPortugueseOrdinalizer.cs" />
<Compile Include="Localisation\Tense.cs" />
<Compile Include="Localisation\NumberToWords\SpanishNumberToWordsConverter.cs" />
<Compile Include="TimeSpanHumanizeExtensions.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Humanizer.Localisation.Ordinalize
namespace Humanizer.Localisation.Ordinalizer
{
internal class BrazilianPortugueseOrdinalizeConverter : DefaultOrdinalizeConverter
internal class BrazilianPortugueseOrdinalizer : DefaultOrdinalizer
{
public override string Convert(int number, string numberString)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Humanizer.Localisation.Ordinalize
namespace Humanizer.Localisation.Ordinalizer
{
internal class DefaultOrdinalizeConverter
internal class DefaultOrdinalizer
{
public virtual string Convert(int number, string numberString, GrammaticalGender gender)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
namespace Humanizer.Localisation.Ordinalize
namespace Humanizer.Localisation.Ordinalizer
{
internal class EnglishOrdinalizeConverter : DefaultOrdinalizeConverter
internal class EnglishOrdinalizer : DefaultOrdinalizer
{
public override string Convert(int number, string numberString)
{
int nMod100 = number % 100;

if (nMod100 >= 11 && nMod100 <= 13)
{
return numberString + "th";
}

switch (number % 10)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Humanizer.Localisation.Ordinalize
namespace Humanizer.Localisation.Ordinalizer
{
internal class SpanishOrdinalizeConverter : DefaultOrdinalizeConverter
internal class SpanishOrdinalizer : DefaultOrdinalizer
{
public override string Convert(int number, string numberString)
{
Expand Down
34 changes: 17 additions & 17 deletions src/Humanizer/OrdinalizeExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Humanizer.Localisation.Ordinalize;
using Humanizer.Localisation.Ordinalizer;
using System;
using System.Collections.Generic;
using System.Globalization;
Expand All @@ -10,12 +10,12 @@ namespace Humanizer
/// </summary>
public static class OrdinalizeExtensions
{
private static readonly IDictionary<string, Func<DefaultOrdinalizeConverter>> ConverterFactories =
new Dictionary<string, Func<DefaultOrdinalizeConverter>>
private static readonly IDictionary<string, Func<DefaultOrdinalizer>> OrdinalizerFactories =
new Dictionary<string, Func<DefaultOrdinalizer>>
{
{"en", () => new EnglishOrdinalizeConverter()},
{"es", () => new SpanishOrdinalizeConverter()},
{"pt-BR", () => new BrazilianPortugueseOrdinalizeConverter()}
{"en", () => new EnglishOrdinalizer()},
{"es", () => new SpanishOrdinalizer()},
{"pt-BR", () => new BrazilianPortugueseOrdinalizer()}
};

/// <summary>
Expand All @@ -25,7 +25,7 @@ public static class OrdinalizeExtensions
/// <returns></returns>
public static string Ordinalize(this string numberString)
{
return Converter.Convert(int.Parse(numberString), numberString);
return Ordinalizer.Convert(int.Parse(numberString), numberString);
}

/// <summary>
Expand All @@ -39,7 +39,7 @@ public static string Ordinalize(this string numberString)
/// <returns></returns>
public static string Ordinalize(this string numberString, GrammaticalGender gender)
{
return Converter.Convert(int.Parse(numberString), numberString, gender);
return Ordinalizer.Convert(int.Parse(numberString), numberString, gender);
}

/// <summary>
Expand All @@ -49,7 +49,7 @@ public static string Ordinalize(this string numberString, GrammaticalGender gend
/// <returns></returns>
public static string Ordinalize(this int number)
{
return Converter.Convert(number, number.ToString(CultureInfo.InvariantCulture));
return Ordinalizer.Convert(number, number.ToString(CultureInfo.InvariantCulture));
}

/// <summary>
Expand All @@ -63,22 +63,22 @@ public static string Ordinalize(this int number)
/// <returns></returns>
public static string Ordinalize(this int number, GrammaticalGender gender)
{
return Converter.Convert(number, number.ToString(CultureInfo.InvariantCulture), gender);
return Ordinalizer.Convert(number, number.ToString(CultureInfo.InvariantCulture), gender);
}

private static DefaultOrdinalizeConverter Converter
private static DefaultOrdinalizer Ordinalizer
{
get
{
Func<DefaultOrdinalizeConverter> converterFactory;
Func<DefaultOrdinalizer> ordinalizerFactory;

if (ConverterFactories.TryGetValue(CultureInfo.CurrentUICulture.Name, out converterFactory))
return converterFactory();
if (OrdinalizerFactories.TryGetValue(CultureInfo.CurrentUICulture.Name, out ordinalizerFactory))
return ordinalizerFactory();

if (ConverterFactories.TryGetValue(CultureInfo.CurrentUICulture.TwoLetterISOLanguageName, out converterFactory))
return converterFactory();
if (OrdinalizerFactories.TryGetValue(CultureInfo.CurrentUICulture.TwoLetterISOLanguageName, out ordinalizerFactory))
return ordinalizerFactory();

return new DefaultOrdinalizeConverter();
return new DefaultOrdinalizer();
}
}
}
Expand Down

0 comments on commit b353f33

Please sign in to comment.