From 5790142f81f2a94ad073834b1534acbf9b02ea3c Mon Sep 17 00:00:00 2001 From: 杨宇千 Date: Fri, 25 Oct 2019 18:36:02 +0800 Subject: Add NeutralResourcesLanguage. Conform to best practices. --- Timeline.Tests/Helpers/UseCultureAttribute.cs | 91 +++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 Timeline.Tests/Helpers/UseCultureAttribute.cs (limited to 'Timeline.Tests/Helpers') diff --git a/Timeline.Tests/Helpers/UseCultureAttribute.cs b/Timeline.Tests/Helpers/UseCultureAttribute.cs new file mode 100644 index 00000000..f0064c01 --- /dev/null +++ b/Timeline.Tests/Helpers/UseCultureAttribute.cs @@ -0,0 +1,91 @@ +using System; +using System.Globalization; +using System.Linq; +using System.Reflection; +using System.Threading; +using Xunit.Sdk; + +// Copied from https://github.com/xunit/samples.xunit/blob/master/UseCulture/UseCultureAttribute.cs + +/// +/// Apply this attribute to your test method to replace the +/// and +/// with another culture. +/// +[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)] +public class UseCultureAttribute : BeforeAfterTestAttribute +{ + readonly Lazy culture; + readonly Lazy uiCulture; + + CultureInfo originalCulture; + CultureInfo originalUICulture; + + /// + /// Replaces the culture and UI culture of the current thread with + /// + /// + /// The name of the culture. + /// + /// + /// This constructor overload uses for both + /// and . + /// + /// + public UseCultureAttribute(string culture) + : this(culture, culture) { } + + /// + /// Replaces the culture and UI culture of the current thread with + /// and + /// + /// The name of the culture. + /// The name of the UI culture. + public UseCultureAttribute(string culture, string uiCulture) + { + this.culture = new Lazy(() => new CultureInfo(culture, false)); + this.uiCulture = new Lazy(() => new CultureInfo(uiCulture, false)); + } + + /// + /// Gets the culture. + /// + public CultureInfo Culture { get { return culture.Value; } } + + /// + /// Gets the UI culture. + /// + public CultureInfo UICulture { get { return uiCulture.Value; } } + + /// + /// Stores the current + /// and + /// and replaces them with the new cultures defined in the constructor. + /// + /// The method under test + public override void Before(MethodInfo methodUnderTest) + { + originalCulture = Thread.CurrentThread.CurrentCulture; + originalUICulture = Thread.CurrentThread.CurrentUICulture; + + Thread.CurrentThread.CurrentCulture = Culture; + Thread.CurrentThread.CurrentUICulture = UICulture; + + CultureInfo.CurrentCulture.ClearCachedData(); + CultureInfo.CurrentUICulture.ClearCachedData(); + } + + /// + /// Restores the original and + /// to + /// + /// The method under test + public override void After(MethodInfo methodUnderTest) + { + Thread.CurrentThread.CurrentCulture = originalCulture; + Thread.CurrentThread.CurrentUICulture = originalUICulture; + + CultureInfo.CurrentCulture.ClearCachedData(); + CultureInfo.CurrentUICulture.ClearCachedData(); + } +} -- cgit v1.2.3