Code refactor and clean-up to follow new C# 9 conventions and features where applicable.

This commit is contained in:
2021-04-30 13:06:04 +01:00
parent a72e7ad0e4
commit 19756723a3
34 changed files with 2467 additions and 2691 deletions

View File

@@ -5,9 +5,8 @@ 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.
/// 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.
/// </summary>
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public class UseCultureAttribute : BeforeAfterTestAttribute
@@ -18,22 +17,19 @@ public class UseCultureAttribute : BeforeAfterTestAttribute
CultureInfo originalCulture;
CultureInfo originalUICulture;
/// <summary>
/// Replaces the culture and UI culture of the current thread with
/// <paramref name="culture" />
/// </summary>
/// <summary>Replaces the culture and UI culture of the current thread with <paramref name="culture" /></summary>
/// <param name="culture">The name of the culture.</param>
/// <remarks>
/// <para>
/// This constructor overload uses <paramref name="culture" /> for both
/// <see cref="Culture" /> and <see cref="UICulture" />.
/// This constructor overload uses <paramref name="culture" /> for both <see cref="Culture" /> and
/// <see cref="UICulture" />.
/// </para>
/// </remarks>
public UseCultureAttribute(string culture) : this(culture, culture) { }
public UseCultureAttribute(string culture) : this(culture, culture) {}
/// <summary>
/// Replaces the culture and UI culture of the current thread with
/// <paramref name="culture" /> and <paramref name="uiCulture" />
/// Replaces the culture and UI culture of the current thread with <paramref name="culture" /> and
/// <paramref name="uiCulture" />
/// </summary>
/// <param name="culture">The name of the culture.</param>
/// <param name="uiCulture">The name of the UI culture.</param>
@@ -43,37 +39,32 @@ public class UseCultureAttribute : BeforeAfterTestAttribute
this.uiCulture = new Lazy<CultureInfo>(() => new CultureInfo(uiCulture));
}
/// <summary>
/// Gets the culture.
/// </summary>
/// <summary>Gets the culture.</summary>
public CultureInfo Culture => culture.Value;
/// <summary>
/// Gets the UI culture.
/// </summary>
/// <summary>Gets the UI culture.</summary>
public CultureInfo UICulture => uiCulture.Value;
/// <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.
/// 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.
/// </summary>
/// <param name="methodUnderTest">The method under test</param>
public override void Before(MethodInfo methodUnderTest)
{
#if NETCORE
#if NETCORE
originalCulture = CultureInfo.CurrentCulture;
originalUICulture = CultureInfo.CurrentUICulture;
CultureInfo.CurrentCulture = Culture;
CultureInfo.CurrentUICulture = Culture;
#else
#else
originalCulture = Thread.CurrentThread.CurrentCulture;
originalUICulture = Thread.CurrentThread.CurrentUICulture;
Thread.CurrentThread.CurrentCulture = Culture;
Thread.CurrentThread.CurrentUICulture = UICulture;
#endif
#endif
}
/// <summary>
@@ -83,12 +74,12 @@ public class UseCultureAttribute : BeforeAfterTestAttribute
/// <param name="methodUnderTest">The method under test</param>
public override void After(MethodInfo methodUnderTest)
{
#if NETCORE
#if NETCORE
CultureInfo.CurrentCulture = originalCulture;
CultureInfo.CurrentUICulture = originalUICulture;
#else
#else
Thread.CurrentThread.CurrentCulture = originalCulture;
Thread.CurrentThread.CurrentUICulture = originalUICulture;
#endif
#endif
}
}