Skip to content

Commit 1ec8763

Browse files
author
Christian Käser
committed
Implement UseCultureAttribute
This is necessary to make sure all tests use en-US to get standard texts without explicitly specifying the culture every time the resource file is accessed. The code for this class was taken from the xunit/sample.xunit repository which is (c) 2014 Outercurve Foundation and licensed under the Apache License, Version 2.0.
1 parent 0bab64a commit 1ec8763

2 files changed

Lines changed: 104 additions & 0 deletions

File tree

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
2+
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
3+
4+
namespace StyleCop.Analyzers.Test.Helpers
5+
{
6+
using System;
7+
using System.Globalization;
8+
using System.Reflection;
9+
using System.Threading;
10+
11+
using Xunit.Sdk;
12+
13+
/// <summary>
14+
/// Apply this attribute to your test method to replace the
15+
/// <see cref="Thread.CurrentThread" /> <see cref="CultureInfo.CurrentCulture" /> and
16+
/// <see cref="CultureInfo.CurrentUICulture" /> with another culture.
17+
/// </summary>
18+
/// <remarks>
19+
/// This code was adapted from
20+
/// https://github.com/xunit/samples.xunit/blob/885edfc/UseCulture/UseCultureAttribute.cs.
21+
/// The original code is (c) 2014 Outercurve Foundation and licensed under the Apache License,
22+
/// Version 2.0.
23+
/// </remarks>
24+
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
25+
public class UseCultureAttribute : BeforeAfterTestAttribute
26+
{
27+
private readonly Lazy<CultureInfo> culture;
28+
29+
#pragma warning disable SA1305
30+
private readonly Lazy<CultureInfo> uiCulture;
31+
#pragma warning restore SA1305
32+
33+
private CultureInfo originalCulture;
34+
35+
private CultureInfo originalUiCulture;
36+
37+
/// <summary>
38+
/// Initializes a new instance of the <see cref="UseCultureAttribute"/>
39+
/// class with a culture.
40+
/// </summary>
41+
/// <param name="culture">The name of the culture.</param>
42+
/// <remarks>
43+
/// <para>
44+
/// This constructor overload uses <paramref name="culture" /> for both
45+
/// <see cref="Culture" /> and <see cref="UiCulture" />.
46+
/// </para>
47+
/// </remarks>
48+
public UseCultureAttribute(string culture)
49+
: this(culture, culture)
50+
{
51+
}
52+
53+
/// <summary>
54+
/// Initializes a new instance of the <see cref="UseCultureAttribute"/>
55+
/// class with a culture and a UI culture.
56+
/// </summary>
57+
/// <param name="culture">The name of the culture.</param>
58+
/// <param name="uiCulture">The name of the UI culture.</param>
59+
public UseCultureAttribute(string culture, string uiCulture)
60+
{
61+
this.culture = new Lazy<CultureInfo>(() => new CultureInfo(culture));
62+
this.uiCulture = new Lazy<CultureInfo>(() => new CultureInfo(uiCulture));
63+
}
64+
65+
/// <summary>
66+
/// Gets the culture.
67+
/// </summary>
68+
/// <value>The culture.</value>
69+
public CultureInfo Culture => this.culture.Value;
70+
71+
/// <summary>
72+
/// Gets the UI culture.
73+
/// </summary>
74+
/// <value>The UI culture.</value>
75+
public CultureInfo UiCulture => this.uiCulture.Value;
76+
77+
/// <summary>
78+
/// Stores the current <see cref="Thread.CurrentPrincipal" />
79+
/// <see cref="CultureInfo.CurrentCulture" /> and <see cref="CultureInfo.CurrentUICulture" />
80+
/// and replaces them with the new cultures defined in the constructor.
81+
/// </summary>
82+
/// <param name="methodUnderTest">The method under test</param>
83+
public override void Before(MethodInfo methodUnderTest)
84+
{
85+
this.originalCulture = Thread.CurrentThread.CurrentCulture;
86+
this.originalUiCulture = Thread.CurrentThread.CurrentUICulture;
87+
88+
Thread.CurrentThread.CurrentCulture = this.Culture;
89+
Thread.CurrentThread.CurrentUICulture = this.UiCulture;
90+
}
91+
92+
/// <summary>
93+
/// Restores the original <see cref="CultureInfo.CurrentCulture" /> and
94+
/// <see cref="CultureInfo.CurrentUICulture" /> to <see cref="Thread.CurrentPrincipal" />
95+
/// </summary>
96+
/// <param name="methodUnderTest">The method under test</param>
97+
public override void After(MethodInfo methodUnderTest)
98+
{
99+
Thread.CurrentThread.CurrentCulture = this.originalCulture;
100+
Thread.CurrentThread.CurrentUICulture = this.originalUiCulture;
101+
}
102+
}
103+
}

StyleCop.Analyzers/StyleCop.Analyzers.Test/StyleCop.Analyzers.Test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@
180180
<Compile Include="Helpers\MetadataReferences.cs" />
181181
<Compile Include="Helpers\TestDiagnosticProvider.cs" />
182182
<Compile Include="Helpers\TestXmlReferenceResolver.cs" />
183+
<Compile Include="Helpers\UseCultureAttribute.cs" />
183184
<Compile Include="HelperTests\AccessLevelHelperTests.cs" />
184185
<Compile Include="HelperTests\IndentationHelperTests.cs" />
185186
<Compile Include="HelperTests\TokenHelperTests.cs" />

0 commit comments

Comments
 (0)