Files
plist-cil/plist-cil.test/UseCultureAttribute.cs

85 lines
3.3 KiB
C#
Raw Permalink Normal View History

2017-03-16 23:37:42 +01:00
using System;
using System.Globalization;
using System.Reflection;
using System.Threading;
using Xunit.Sdk;
/// <summary>
/// Apply this attribute to your test method to replace the <see cref="Thread.CurrentThread" />
/// <see cref="CultureInfo.CurrentCulture" /> and <see cref="CultureInfo.CurrentUICulture" /> with another culture.
2017-03-16 23:37:42 +01:00
/// </summary>
2018-07-09 19:57:08 +01:00
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
2017-03-16 23:37:42 +01:00
public class UseCultureAttribute : BeforeAfterTestAttribute
{
readonly Lazy<CultureInfo> culture;
readonly Lazy<CultureInfo> uiCulture;
CultureInfo originalCulture;
CultureInfo originalUICulture;
/// <summary>Replaces the culture and UI culture of the current thread with <paramref name="culture" /></summary>
2017-03-16 23:37:42 +01:00
/// <param name="culture">The name of the culture.</param>
/// <remarks>
2018-07-09 19:57:08 +01:00
/// <para>
/// This constructor overload uses <paramref name="culture" /> for both <see cref="Culture" /> and
/// <see cref="UICulture" />.
2018-07-09 19:57:08 +01:00
/// </para>
2017-03-16 23:37:42 +01:00
/// </remarks>
public UseCultureAttribute(string culture) : this(culture, culture) {}
2017-03-16 23:37:42 +01:00
/// <summary>
/// Replaces the culture and UI culture of the current thread with <paramref name="culture" /> and
/// <paramref name="uiCulture" />
2017-03-16 23:37:42 +01:00
/// </summary>
/// <param name="culture">The name of the culture.</param>
/// <param name="uiCulture">The name of the UI culture.</param>
public UseCultureAttribute(string culture, string uiCulture)
{
2018-07-09 19:57:08 +01:00
this.culture = new Lazy<CultureInfo>(() => new CultureInfo(culture));
2017-03-16 23:37:42 +01:00
this.uiCulture = new Lazy<CultureInfo>(() => new CultureInfo(uiCulture));
}
/// <summary>Gets the culture.</summary>
2018-07-09 19:57:08 +01:00
public CultureInfo Culture => culture.Value;
2017-03-16 23:37:42 +01:00
/// <summary>Gets the UI culture.</summary>
2018-07-09 19:57:08 +01:00
public CultureInfo UICulture => uiCulture.Value;
2017-03-16 23:37:42 +01:00
/// <summary>
/// Stores the current <see cref="Thread.CurrentPrincipal" /> <see cref="CultureInfo.CurrentCulture" /> and
/// <see cref="CultureInfo.CurrentUICulture" /> and replaces them with the new cultures defined in the constructor.
2017-03-16 23:37:42 +01:00
/// </summary>
/// <param name="methodUnderTest">The method under test</param>
public override void Before(MethodInfo methodUnderTest)
{
2025-08-07 22:31:12 +01:00
#if NETCORE
2017-03-16 23:37:42 +01:00
originalCulture = CultureInfo.CurrentCulture;
originalUICulture = CultureInfo.CurrentUICulture;
CultureInfo.CurrentCulture = Culture;
CultureInfo.CurrentUICulture = Culture;
2025-08-07 22:31:12 +01:00
#else
2018-07-09 19:57:08 +01:00
originalCulture = Thread.CurrentThread.CurrentCulture;
2017-03-16 23:37:42 +01:00
originalUICulture = Thread.CurrentThread.CurrentUICulture;
2018-07-09 19:57:08 +01:00
Thread.CurrentThread.CurrentCulture = Culture;
2017-03-16 23:37:42 +01:00
Thread.CurrentThread.CurrentUICulture = UICulture;
2025-08-07 22:31:12 +01:00
#endif
2017-03-16 23:37:42 +01:00
}
/// <summary>
2018-07-09 19:57:08 +01:00
/// Restores the original <see cref="CultureInfo.CurrentCulture" /> and
/// <see cref="CultureInfo.CurrentUICulture" /> to <see cref="Thread.CurrentPrincipal" />
2017-03-16 23:37:42 +01:00
/// </summary>
/// <param name="methodUnderTest">The method under test</param>
public override void After(MethodInfo methodUnderTest)
{
2025-08-07 22:31:12 +01:00
#if NETCORE
2017-03-16 23:37:42 +01:00
CultureInfo.CurrentCulture = originalCulture;
CultureInfo.CurrentUICulture = originalUICulture;
2025-08-07 22:31:12 +01:00
#else
2018-07-09 19:57:08 +01:00
Thread.CurrentThread.CurrentCulture = originalCulture;
2017-03-16 23:37:42 +01:00
Thread.CurrentThread.CurrentUICulture = originalUICulture;
2025-08-07 22:31:12 +01:00
#endif
2017-03-16 23:37:42 +01:00
}
}