-
Notifications
You must be signed in to change notification settings - Fork 509
Expand file tree
/
Copy pathUseCultureAttribute.cs
More file actions
executable file
·122 lines (105 loc) · 5.09 KB
/
UseCultureAttribute.cs
File metadata and controls
executable file
·122 lines (105 loc) · 5.09 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
#nullable disable
namespace StyleCop.Analyzers.Test.Helpers
{
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.
/// </summary>
/// <remarks>
/// <para>This code was adapted from
/// https://github.com/xunit/samples.xunit/blob/885edfc/UseCulture/UseCultureAttribute.cs.
/// The original code is (c) 2014 Outercurve Foundation and licensed under the Apache License,
/// Version 2.0.</para>
/// </remarks>
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public class UseCultureAttribute : BeforeAfterTestAttribute
{
private readonly Lazy<CultureInfo> culture;
#pragma warning disable IDE0079 // Remove unnecessary suppression
#pragma warning disable SA1305 // Field names should not use Hungarian notation
private readonly Lazy<CultureInfo> uiCulture;
#pragma warning restore SA1305 // Field names should not use Hungarian notation
#pragma warning restore IDE0079 // Remove unnecessary suppression
private CultureInfo originalCulture;
private CultureInfo originalUiCulture;
private CultureInfo originalDefaultCulture;
private CultureInfo originalDefaultUiCulture;
/// <summary>
/// Initializes a new instance of the <see cref="UseCultureAttribute"/>
/// class with a 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" />.
/// </para>
/// </remarks>
public UseCultureAttribute(string culture)
: this(culture, culture)
{
}
#pragma warning disable IDE0079 // Remove unnecessary suppression
#pragma warning disable SA1305 // Field names should not use Hungarian notation
/// <summary>
/// Initializes a new instance of the <see cref="UseCultureAttribute"/>
/// class with a culture and a UI culture.
/// </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)
#pragma warning restore SA1305 // Field names should not use Hungarian notation
#pragma warning restore IDE0079 // Remove unnecessary suppression
{
this.culture = new Lazy<CultureInfo>(() => new CultureInfo(culture));
this.uiCulture = new Lazy<CultureInfo>(() => new CultureInfo(uiCulture));
}
/// <summary>
/// Gets the culture.
/// </summary>
/// <value>The culture.</value>
public CultureInfo Culture => this.culture.Value;
/// <summary>
/// Gets the UI culture.
/// </summary>
/// <value>The UI culture.</value>
public CultureInfo UiCulture => this.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.
/// </summary>
/// <param name="methodUnderTest">The method under test.</param>
public override void Before(MethodInfo methodUnderTest)
{
this.originalCulture = Thread.CurrentThread.CurrentCulture;
this.originalUiCulture = Thread.CurrentThread.CurrentUICulture;
this.originalDefaultCulture = CultureInfo.DefaultThreadCurrentCulture;
this.originalDefaultUiCulture = CultureInfo.DefaultThreadCurrentUICulture;
Thread.CurrentThread.CurrentCulture = this.Culture;
Thread.CurrentThread.CurrentUICulture = this.UiCulture;
CultureInfo.DefaultThreadCurrentCulture = this.Culture;
CultureInfo.DefaultThreadCurrentUICulture = this.UiCulture;
}
/// <summary>
/// Restores the original <see cref="CultureInfo.CurrentCulture" /> and
/// <see cref="CultureInfo.CurrentUICulture" /> to <see cref="Thread.CurrentPrincipal" />.
/// </summary>
/// <param name="methodUnderTest">The method under test.</param>
public override void After(MethodInfo methodUnderTest)
{
Thread.CurrentThread.CurrentCulture = this.originalCulture;
Thread.CurrentThread.CurrentUICulture = this.originalUiCulture;
CultureInfo.DefaultThreadCurrentCulture = this.originalDefaultCulture;
CultureInfo.DefaultThreadCurrentUICulture = this.originalDefaultUiCulture;
}
}
}