From 387425deb9b582ebdd2b0d26f05e416acdd75e65 Mon Sep 17 00:00:00 2001 From: Daniel Stolt Date: Tue, 29 Jul 2014 19:30:00 +0200 Subject: [PATCH] Fixed the last remaining failing test cases having to do with genitive month names. Turned out to be a bug in the .NET Framework that the test code now accomodates for. --- .../Localization/DefaultDateFormatterTests.cs | 22 ++++++++++++------- .../Services/DefaultDateFormatter.cs | 14 +++++++++--- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/src/Orchard.Tests/Localization/DefaultDateFormatterTests.cs b/src/Orchard.Tests/Localization/DefaultDateFormatterTests.cs index 6b6db5f43..7cc32844f 100644 --- a/src/Orchard.Tests/Localization/DefaultDateFormatterTests.cs +++ b/src/Orchard.Tests/Localization/DefaultDateFormatterTests.cs @@ -241,14 +241,6 @@ namespace Orchard.Framework.Tests.Localization { var allCultures = CultureInfo.GetCultures(CultureTypes.AllCultures); Parallel.ForEach(allCultures, options, culture => { // All cultures on the machine. - //if (culture.Name.StartsWith("ca") - // || culture.Name.StartsWith("es") - // || culture.Name.StartsWith("mn") - // || culture.Name.StartsWith("oc") - // || culture.Name.StartsWith("wo") - // ) - // return; - var container = InitializeContainer(culture.Name, "GregorianCalendar"); var formats = container.Resolve(); var target = container.Resolve(); @@ -270,6 +262,20 @@ namespace Orchard.Framework.Tests.Localization { try { var result = target.FormatDate(dateParts, dateFormat); var expected = date.ToString(dateFormat, cultureGregorian); + if (result != expected) { + // The .NET date formatting logic contains a bug that causes it to recognize 'd' and 'dd' + // as numerical day specifiers even when they are embedded in literals. Our implementation + // does not contain this bug. If we encounter an unexpected result and the .NET reference + // result contains the genitive month name, replace it with the non-genitive month name + // before asserting. + var numericalDayPattern = @"(\b|[^d])d{1,2}(\b|[^d])"; + var containsNumericalDay = Regex.IsMatch(dateFormat, numericalDayPattern); + if (containsNumericalDay) { + var monthName = formats.MonthNames[month - 1]; + var monthNameGenitive = formats.MonthNamesGenitive[month - 1]; + expected = expected.Replace(monthNameGenitive, monthName); + } + } Assert.AreEqual(expected, result); } catch (Exception ex) { diff --git a/src/Orchard/Localization/Services/DefaultDateFormatter.cs b/src/Orchard/Localization/Services/DefaultDateFormatter.cs index 7e3e0bcac..34ad43fd5 100644 --- a/src/Orchard/Localization/Services/DefaultDateFormatter.cs +++ b/src/Orchard/Localization/Services/DefaultDateFormatter.cs @@ -113,8 +113,7 @@ namespace Orchard.Localization.Services { } public virtual string FormatDate(DateParts parts, string format) { - var formatWithoutLiterals = Regex.Replace(format, @"(? GetDateParseReplacements() { return new Dictionary() { {"dddd", String.Format("(?{0})", String.Join("|", _dateTimeFormatProvider.DayNames.Select(x => EscapeForRegex(x))))}, @@ -297,7 +303,9 @@ namespace Orchard.Localization.Services { {"dd", "{7:00}"}, {"d", "{7:##}"}, {"MMMM", useMonthNameGenitive ? "{5:MMMM}" : "{3:MMMM}"}, - {"MMM", useMonthNameGenitive ? "{6:MMM}" : "{4:MMM}"}, + // The .NET formatting logic never uses the abbreviated genitive month name; doing the same for compatibility. + //{"MMM", useMonthNameGenitive ? "{6:MMM}" : "{4:MMM}"}, + {"MMM", "{4:MMM}"}, {"MM", "{2:00}"}, {"M", "{2:##}"}, {"yyyyy", "{0:00000}"},