From 2344553b4e14e6f6c71762c5b8ca27cfc1c6da12 Mon Sep 17 00:00:00 2001 From: Scott Kirkland Date: Sat, 19 Mar 2011 06:02:26 +0800 Subject: [PATCH 1/3] accounted for a few more test cases provided by Bas Jansen on my blog --- Inflector.Tests/PluralizeTests.cs | 11 +++++++++++ Inflector/Inflector.cs | 10 +++++++--- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/Inflector.Tests/PluralizeTests.cs b/Inflector.Tests/PluralizeTests.cs index da20969..005e124 100644 --- a/Inflector.Tests/PluralizeTests.cs +++ b/Inflector.Tests/PluralizeTests.cs @@ -119,6 +119,17 @@ public PluralizeTests() TestData.Add("horse", "horses"); TestData.Add("prize", "prizes"); TestData.Add("edge", "edges"); + + /* Tests added by Bas Jansen */ + TestData.Add("goose", "geese"); + TestData.Add("deer", "deer"); + TestData.Add("sheep", "sheep"); + TestData.Add("wolf", "wolves"); + TestData.Add("volcano", "volcanoes"); + TestData.Add("aircraft", "aircraft"); + TestData.Add("alumna", "alumnae"); + TestData.Add("alumnus", "alumni"); + TestData.Add("fungus", "fungi"); } } } diff --git a/Inflector/Inflector.cs b/Inflector/Inflector.cs index 9da4662..a0a4fa5 100644 --- a/Inflector/Inflector.cs +++ b/Inflector/Inflector.cs @@ -12,10 +12,10 @@ static Inflector() AddPlural("$", "s"); AddPlural("s$", "s"); AddPlural("(ax|test)is$", "$1es"); - AddPlural("(octop|vir)us$", "$1i"); + AddPlural("(octop|vir|alumn|fung)us$", "$1i"); AddPlural("(alias|status)$", "$1es"); AddPlural("(bu)s$", "$1ses"); - AddPlural("(buffal|tomat)o$", "$1oes"); + AddPlural("(buffal|tomat|volcan)o$", "$1oes"); AddPlural("([ti])um$", "$1a"); AddPlural("sis$", "ses"); AddPlural("(?:([^f])fe|([lr])f)$", "$1$2ves"); @@ -45,7 +45,7 @@ static Inflector() AddSingular("(o)es$", "$1"); AddSingular("(shoe)s$", "$1"); AddSingular("(cris|ax|test)es$", "$1is"); - AddSingular("(octop|vir)i$", "$1us"); + AddSingular("(octop|vir|alumn|fung)i$", "$1us"); AddSingular("(alias|status)es$", "$1"); AddSingular("^(ox)en", "$1"); AddSingular("(vert|ind)ices$", "$1ex"); @@ -57,6 +57,8 @@ static Inflector() AddIrregular("child", "children"); AddIrregular("sex", "sexes"); AddIrregular("move", "moves"); + AddIrregular("goose", "geese"); + AddIrregular("alumna", "alumnae"); AddUncountable("equipment"); AddUncountable("information"); @@ -66,6 +68,8 @@ static Inflector() AddUncountable("series"); AddUncountable("fish"); AddUncountable("sheep"); + AddUncountable("deer"); + AddUncountable("aircraft"); } #endregion From 2976b45899143ecaf371d2b6e8c5ee4e97fa0b2a Mon Sep 17 00:00:00 2001 From: David Kemp Date: Fri, 1 Apr 2011 15:28:06 +0100 Subject: [PATCH 2/3] made .Net 3.5 compatible, tidied up references and made Ordinalize work with ints --- Inflector.Tests/Inflector.Tests.csproj | 12 ---- Inflector.Tests/OrdinalizeTests.cs | 78 +++++++++++++++++++++++++- Inflector/Inflector.cs | 28 ++++++--- Inflector/Inflector.csproj | 8 +-- 4 files changed, 98 insertions(+), 28 deletions(-) diff --git a/Inflector.Tests/Inflector.Tests.csproj b/Inflector.Tests/Inflector.Tests.csproj index e6f136a..0f53c2a 100644 --- a/Inflector.Tests/Inflector.Tests.csproj +++ b/Inflector.Tests/Inflector.Tests.csproj @@ -34,19 +34,7 @@ ..\packages\NUnit.2.5.9.10348\lib\nunit.framework.dll - - ..\packages\NUnit.2.5.9.10348\lib\nunit.mocks.dll - - - ..\packages\NUnit.2.5.9.10348\lib\pnunit.framework.dll - - - - - - - diff --git a/Inflector.Tests/OrdinalizeTests.cs b/Inflector.Tests/OrdinalizeTests.cs index 0bd9a40..c9f116b 100644 --- a/Inflector.Tests/OrdinalizeTests.cs +++ b/Inflector.Tests/OrdinalizeTests.cs @@ -1,10 +1,15 @@ -using NUnit.Framework; +using System; +using System.Collections; +using System.Collections.Generic; +using NUnit.Framework; namespace Inflector.Tests { [TestFixture] public class OrdinalizeTests : InflectorTestBase { + private readonly Dictionary TestDataNumbers; + [Test] public void Ordinalize() { @@ -12,6 +17,44 @@ public void Ordinalize() { Assert.AreEqual(pair.Key.Ordinalize(), pair.Value); } + + foreach (var testDataNumber in TestDataNumbers) + { + Assert.AreEqual(testDataNumber.Key.Ordinalize(), testDataNumber.Value); + } + } + + [TestCase(0, "0th")] + [TestCase(1, "1st")] + [TestCase(2, "2nd")] + [TestCase(3, "3rd")] + [TestCase(4, "4th")] + [TestCase(5, "5th")] + [TestCase(6, "6th")] + [TestCase(7, "7th")] + [TestCase(8, "8th")] + [TestCase(9, "9th")] + [TestCase(10, "10th")] + [TestCase(11, "11th")] + [TestCase(12, "12th")] + [TestCase(13, "13th")] + [TestCase(14, "14th")] + [TestCase(20, "20th")] + [TestCase(21, "21st")] + [TestCase(22, "22nd")] + [TestCase(23, "23rd")] + [TestCase(24, "24th")] + [TestCase(100, "100th")] + [TestCase(101, "101st")] + [TestCase(102, "102nd")] + [TestCase(103, "103rd")] + [TestCase(104, "104th")] + [TestCase(110, "110th")] + [TestCase(1000, "1000th")] + [TestCase(1001, "1001st")] + public void OrdanizeNumbersTest(int number, string ordanized) + { + Assert.AreEqual(number.Ordinalize(), ordanized); } public OrdinalizeTests() @@ -45,6 +88,39 @@ public OrdinalizeTests() TestData.Add("1000", "1000th"); TestData.Add("1001", "1001st"); + TestDataNumbers = + new Dictionary + { + {0, "0th"}, + {1, "1st"}, + {2, "2nd"}, + {3, "3rd"}, + {4, "4th"}, + {5, "5th"}, + {6, "6th"}, + {7, "7th"}, + {8, "8th"}, + {9, "9th"}, + {10, "10th"}, + {11, "11th"}, + {12, "12th"}, + {13, "13th"}, + {14, "14th"}, + {20, "20th"}, + {21, "21st"}, + {22, "22nd"}, + {23, "23rd"}, + {24, "24th"}, + {100, "100th"}, + {101, "101st"}, + {102, "102nd"}, + {103, "103rd"}, + {104, "104th"}, + {110, "110th"}, + {1000, "1000th"}, + {1001, "1001st"}, + }; + } } } \ No newline at end of file diff --git a/Inflector/Inflector.cs b/Inflector/Inflector.cs index a0a4fa5..3b871fe 100644 --- a/Inflector/Inflector.cs +++ b/Inflector/Inflector.cs @@ -195,29 +195,39 @@ public static string Uncapitalize(this string word) return word.Substring(0, 1).ToLower() + word.Substring(1); } - public static string Ordinalize(this string number) + public static string Ordinalize(this string numberString) { - int n = int.Parse(number); - int nMod100 = n % 100; + return Ordanize(int.Parse(numberString), numberString); + } + + public static string Ordinalize(this int number) + { + return Ordanize(number, number.ToString()); + } + + private static string Ordanize(int number, string numberString) + { + int nMod100 = number % 100; if (nMod100 >= 11 && nMod100 <= 13) { - return number + "th"; + return numberString + "th"; } - switch (n % 10) + switch (number % 10) { case 1: - return number + "st"; + return numberString + "st"; case 2: - return number + "nd"; + return numberString + "nd"; case 3: - return number + "rd"; + return numberString + "rd"; default: - return number + "th"; + return numberString + "th"; } } + public static string Dasherize(this string underscoredWord) { return underscoredWord.Replace('_', '-'); diff --git a/Inflector/Inflector.csproj b/Inflector/Inflector.csproj index cda6e68..ebab15b 100644 --- a/Inflector/Inflector.csproj +++ b/Inflector/Inflector.csproj @@ -10,8 +10,9 @@ Properties Inflector Inflector - v4.0 + v3.5 512 + true @@ -33,11 +34,6 @@ - - - - - From a7818f347c0bfba04c6d068e3e822771a230699d Mon Sep 17 00:00:00 2001 From: David Kemp Date: Fri, 1 Apr 2011 15:29:49 +0100 Subject: [PATCH 3/3] removed redundant testing code --- Inflector.Tests/OrdinalizeTests.cs | 41 ------------------------------ 1 file changed, 41 deletions(-) diff --git a/Inflector.Tests/OrdinalizeTests.cs b/Inflector.Tests/OrdinalizeTests.cs index c9f116b..1dad26d 100644 --- a/Inflector.Tests/OrdinalizeTests.cs +++ b/Inflector.Tests/OrdinalizeTests.cs @@ -8,8 +8,6 @@ namespace Inflector.Tests [TestFixture] public class OrdinalizeTests : InflectorTestBase { - private readonly Dictionary TestDataNumbers; - [Test] public void Ordinalize() { @@ -17,11 +15,6 @@ public void Ordinalize() { Assert.AreEqual(pair.Key.Ordinalize(), pair.Value); } - - foreach (var testDataNumber in TestDataNumbers) - { - Assert.AreEqual(testDataNumber.Key.Ordinalize(), testDataNumber.Value); - } } [TestCase(0, "0th")] @@ -87,40 +80,6 @@ public OrdinalizeTests() TestData.Add("110", "110th"); TestData.Add("1000", "1000th"); TestData.Add("1001", "1001st"); - - TestDataNumbers = - new Dictionary - { - {0, "0th"}, - {1, "1st"}, - {2, "2nd"}, - {3, "3rd"}, - {4, "4th"}, - {5, "5th"}, - {6, "6th"}, - {7, "7th"}, - {8, "8th"}, - {9, "9th"}, - {10, "10th"}, - {11, "11th"}, - {12, "12th"}, - {13, "13th"}, - {14, "14th"}, - {20, "20th"}, - {21, "21st"}, - {22, "22nd"}, - {23, "23rd"}, - {24, "24th"}, - {100, "100th"}, - {101, "101st"}, - {102, "102nd"}, - {103, "103rd"}, - {104, "104th"}, - {110, "110th"}, - {1000, "1000th"}, - {1001, "1001st"}, - }; - } } } \ No newline at end of file