-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathUseCultureAttribute.cs
More file actions
49 lines (41 loc) · 1.47 KB
/
UseCultureAttribute.cs
File metadata and controls
49 lines (41 loc) · 1.47 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
40
41
42
43
44
45
46
47
48
49
using System;
using System.Globalization;
using System.Reflection;
using Xunit.Sdk;
namespace Exercism.Tests
{
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public class UseCultureAttribute : BeforeAfterTestAttribute
{
private readonly CultureInfo _culture;
private readonly CultureInfo _uiCulture;
private CultureInfo _originalCulture;
private CultureInfo _originalUiCulture;
public UseCultureAttribute()
{
_culture = CultureInfo.InvariantCulture;
_uiCulture = CultureInfo.InvariantCulture;
}
public UseCultureAttribute(string culture)
: this(culture, culture)
{
}
public UseCultureAttribute(string culture, string uiCulture)
{
_culture = new CultureInfo(culture, false);
_uiCulture = new CultureInfo(uiCulture, false);
}
public override void Before(MethodInfo methodUnderTest)
{
_originalCulture = CultureInfo.CurrentCulture;
_originalUiCulture = CultureInfo.CurrentUICulture;
CultureInfo.CurrentCulture = _culture;
CultureInfo.CurrentUICulture = _uiCulture;
}
public override void After(MethodInfo methodUnderTest)
{
CultureInfo.CurrentCulture = _originalCulture;
CultureInfo.CurrentUICulture = _originalUiCulture;
}
}
}