forked from dotnet/aspnet-api-versioning
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssumeCultureAttribute.cs
More file actions
39 lines (30 loc) · 1.22 KB
/
AssumeCultureAttribute.cs
File metadata and controls
39 lines (30 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Copyright (c) .NET Foundation and contributors. All rights reserved.
namespace Asp.Versioning;
using System.Globalization;
using System.Reflection;
using static System.AttributeTargets;
using static System.Threading.Thread;
/// <summary>
/// Allows a test method to assume that it is running in a specific locale.
/// </summary>
[AttributeUsage( Class | Method, AllowMultiple = false, Inherited = true )]
internal sealed class AssumeCultureAttribute : BeforeAfterTestAttribute
{
private CultureInfo originalCulture;
private CultureInfo originalUICulture;
public AssumeCultureAttribute( string name ) => Name = name;
public string Name { get; }
public override void Before( MethodInfo methodUnderTest, IXunitTest test )
{
originalCulture = CurrentThread.CurrentCulture;
originalUICulture = CurrentThread.CurrentUICulture;
var culture = CultureInfo.CreateSpecificCulture( Name );
CurrentThread.CurrentCulture = culture;
CurrentThread.CurrentUICulture = culture;
}
public override void After( MethodInfo methodUnderTest, IXunitTest test )
{
CurrentThread.CurrentCulture = originalCulture;
CurrentThread.CurrentUICulture = originalUICulture;
}
}