Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Fix and enable Globalization tests that differ based on OS and culture #4625

Merged
merged 5 commits into from
Nov 24, 2015
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -45,33 +45,18 @@ public void TestInvalidValue()
});
}

// TestLocaleFirstDay: Verify value of property CalendarWeekRule for specific locale
[Fact]
public void TestLocaleFirstDay()
{
CultureInfo myTestCulture = new CultureInfo("en-US");
DateTimeFormatInfo dti = myTestCulture.DateTimeFormat;
CalendarWeekRule actual = dti.CalendarWeekRule;
Assert.Equal(CalendarWeekRule.FirstDay, actual);
}

// TestLocaleFirstFourDayWeek: Verify value of property CalendarWeekRule for specific locale
[Fact]
public void TestLocaleFirstFourDayWeek()
// Verify value of property CalendarWeekRule for specific locales
[Theory]
[InlineData("en-US")]
[InlineData("br-FR")]
public void TestLocale(string localeName)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While I appreciate the complexities of br-FR's calendar rule, I'm wondering if we couldn't help clarity by having some more fixed points called out in something like

[Theory]
[InlineData("en-US", CalendarWeekRule.FirstDay)]
[InlineData("fr-FR", CalendarWeekRule.SomeConst)]
[InlineData("ja-JP", CalendarWeekRule.SomeConst)]
public void VerifyCalendarWeekRule(string localeName, CalendarWeekRule expected)
{
...

just so that we can get breadth-at-a-glance.

Then have br-FR and maybe a couple other complex cases in another test method group.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@steveharter added this test, so he can comment as he wants.

IMO, adding more cultures won't really help increase the coverage since we are getting this data from ICU. We just need to test that we are getting the values correctly from ICU. Also, adding more cultures increases the chance that we will get conflicting values between all the differing systems we are testing on.

I think the two cultures are sufficient for testing coverage, so the question becomes, should I split the test back out into 2 separate Facts instead of using a Theory.

{
CultureInfo myTestCulture = new CultureInfo("br-FR");
CultureInfo myTestCulture = new CultureInfo(localeName);
DateTimeFormatInfo dti = myTestCulture.DateTimeFormat;
CalendarWeekRule actual = dti.CalendarWeekRule;

if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
// note for Win10 this returns FirstFourDayWeek, not FirstFullWeek, this should be cleaned up as part of #3243
Assert.True(actual == CalendarWeekRule.FirstFullWeek || actual == CalendarWeekRule.FirstFourDayWeek);
}
else
{
Assert.Equal(CalendarWeekRule.FirstFourDayWeek, actual);
}
CalendarWeekRule expected = DateTimeFormatInfoData.GetCalendarWeekRule(myTestCulture);
Assert.Equal(expected, actual);
}

private void VerificationHelper(DateTimeFormatInfo info, CalendarWeekRule expected, bool setter)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Runtime.InteropServices;
using Xunit;

namespace System.Globalization.Tests
{
internal static class DateTimeFormatInfoData
{
private static bool s_isWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
private static int s_WindowsVersion = GetWindowsVersion();
private static bool s_isOSX = RuntimeInformation.IsOSPlatform(OSPlatform.OSX);

public static string GetEraName(CultureInfo cultureInfo)
{
Expand Down Expand Up @@ -41,11 +44,144 @@ public static string GetAbbreviatedEraName(CultureInfo cultureInfo)
throw GetCultureNotSupportedException(cultureInfo);
}

private static Exception GetCultureNotSupportedException(CultureInfo cultureInfo)
internal static string[] GetDayNames(CultureInfo cultureInfo)
{
return new NotSupportedException(string.Format("The culture '{0}' with calendar '{1}' is not supported.",
cultureInfo.Name,
if (string.Equals(cultureInfo.Name, "en-US", StringComparison.OrdinalIgnoreCase))
{
return new string[]
{
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"
};
}
if (string.Equals(cultureInfo.Name, "fr-FR", StringComparison.OrdinalIgnoreCase))
{
string[] dayNames = new string[]
{
"dimanche",
"lundi",
"mardi",
"mercredi",
"jeudi",
"vendredi",
"samedi"
};

if (s_isOSX)
{
CapitalizeStrings(dayNames);
}
return dayNames;
}

throw GetCultureNotSupportedException(cultureInfo);
}

internal static string[] GetAbbreviatedDayNames(CultureInfo cultureInfo)
{
if (string.Equals(cultureInfo.Name, "en-US", StringComparison.OrdinalIgnoreCase))
{
return new string[]
{
"Sun",
"Mon",
"Tue",
"Wed",
"Thu",
"Fri",
"Sat"
};
}
if (string.Equals(cultureInfo.Name, "fr-FR", StringComparison.OrdinalIgnoreCase))
{
string[] dayNames = new string[]
{
"dim.",
"lun.",
"mar.",
"mer.",
"jeu.",
"ven.",
"sam."
};

if (s_isOSX)
{
CapitalizeStrings(dayNames);
}
return dayNames;
}

throw GetCultureNotSupportedException(cultureInfo);
}

private static void CapitalizeStrings(string[] strings)
{
for (int i = 0; i < strings.Length; i++)
{
strings[i] = strings[i].Substring(0, 1).ToUpper() + strings[i].Substring(1);
}
}

internal static CalendarWeekRule GetCalendarWeekRule(CultureInfo cultureInfo)
{
if (string.Equals(cultureInfo.Name, "en-US", StringComparison.OrdinalIgnoreCase))
{
return CalendarWeekRule.FirstDay;
}
if (string.Equals(cultureInfo.Name, "br-FR", StringComparison.OrdinalIgnoreCase))
{
if (s_isWindows && s_WindowsVersion < 10)
{
return CalendarWeekRule.FirstFullWeek;
}
else
{
return CalendarWeekRule.FirstFourDayWeek;
}
}

throw GetCultureNotSupportedException(cultureInfo);
}

public static Exception GetCultureNotSupportedException(CultureInfo cultureInfo)
{
return new NotSupportedException(string.Format("The culture '{0}' with calendar '{1}' is not supported.",
cultureInfo.Name,
cultureInfo.Calendar.GetType().Name));
}

public static int GetWindowsVersion()
{
if (s_isWindows)
{
RTL_OSVERSIONINFOEX osvi = new RTL_OSVERSIONINFOEX();
osvi.dwOSVersionInfoSize = (uint)Marshal.SizeOf(osvi);
Assert.Equal(0, RtlGetVersion(out osvi));
return (int)osvi.dwMajorVersion;
}

return -1;
}

[DllImport("ntdll.dll")]
private static extern int RtlGetVersion(out RTL_OSVERSIONINFOEX lpVersionInformation);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this API intended to be kernel mode and it is not nice to call it from the user mode even if it is working. I would suggest you use IsWindowsVersionOrGreater instead

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the function that the new OSDescription API is going to use, according to #4334.

If the function will work for the product, then it should be OK to call in the tests, right?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting. I didn't know that. I am fine then.


[StructLayout(LayoutKind.Sequential)]
internal struct RTL_OSVERSIONINFOEX
{
internal uint dwOSVersionInfoSize;
internal uint dwMajorVersion;
internal uint dwMinorVersion;
internal uint dwBuildNumber;
internal uint dwPlatformId;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
internal string szCSDVersion;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Globalization;
using Xunit;

namespace System.Globalization.Tests
Expand All @@ -26,40 +24,17 @@ public void PosTest1()
VerificationHelper(info, expected);
}

// PosTest2: Call GetAbbreviatedDayName on en-us culture DateTimeFormatInfo instance
[Fact]
public void PosTest2()
{
DateTimeFormatInfo info = new CultureInfo("en-us").DateTimeFormat;
string[] expected = new string[] {
"Sun",
"Mon",
"Tue",
"Wed",
"Thu",
"Fri",
"Sat"
};

VerificationHelper(info, expected);
}

// PosTest3: Call GetAbbreviatedDayName on fr-FR culture DateTimeFormatInfo instance
[Fact]
[ActiveIssue(3243, PlatformID.OSX)]
public void PosTest3()
// Call GetAbbreviatedDayName on en-us culture DateTimeFormatInfo instance
// Call GetAbbreviatedDayName on fr-FR culture DateTimeFormatInfo instance
[Theory]
[InlineData("en-us")]
[InlineData("fr-FR")]
public void PosTest2(string localeName)
{
DateTimeFormatInfo info = new CultureInfo("fr-FR").DateTimeFormat;
string[] expected = new string[] {
"dim.",
"lun.",
"mar.",
"mer.",
"jeu.",
"ven.",
"sam."
};
CultureInfo culture = new CultureInfo(localeName);
string[] expected = DateTimeFormatInfoData.GetAbbreviatedDayNames(culture);

DateTimeFormatInfo info = culture.DateTimeFormat;
VerificationHelper(info, expected);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Globalization;
using Xunit;

namespace System.Globalization.Tests
Expand All @@ -12,7 +10,7 @@ public class DateTimeFormatInfoGetAbbreviatedMonthName
private const int c_MIN_MONTH_VALUE = 1;
private const int c_MAX_MONTH_VALUE = 13;

// PosTest1: Call GetAbbreviatedDayName on default invariant DateTimeFormatInfo instance
// PosTest1: Call GetAbbreviatedMonthName on default invariant DateTimeFormatInfo instance
[Fact]
public void PosTest1()
{
Expand All @@ -37,7 +35,7 @@ public void PosTest1()
VerificationHelper(info, expected);
}

// PosTest2: Call GetAbbreviatedDayName on en-us culture DateTimeFormatInfo instance
// PosTest2: Call GetAbbreviatedMonthName on en-us culture DateTimeFormatInfo instance
[Fact]
public void PosTest2()
{
Expand All @@ -62,9 +60,8 @@ public void PosTest2()
VerificationHelper(info, expected);
}

// PosTest3: Call GetAbbreviatedDayName on fr-FR culture DateTimeFormatInfo instance
// PosTest3: Call GetAbbreviatedMonthName on fr-FR culture DateTimeFormatInfo instance
[Fact]
[ActiveIssue(3243, PlatformID.OSX)]
public void PosTest3()
{
DateTimeFormatInfo info = new CultureInfo("fr-FR").DateTimeFormat;
Expand All @@ -88,7 +85,7 @@ public void PosTest3()
VerificationHelper(info, expected);
}

// PosTest4: Call GetAbbreviatedDayName on DateTimeFormatInfo instance created from ctor
// PosTest4: Call GetAbbreviatedMonthName on DateTimeFormatInfo instance created from ctor
[Fact]
public void PosTest4()
{
Expand Down
Loading