Skip to content

Commit 2c04584

Browse files
Chris Martinezcommonsensesoftware
Chris Martinez
authored andcommitted
Run tests under 'en-US' locale
1 parent 33ec9e1 commit 2c04584

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

src/Abstractions/test/Asp.Versioning.Abstractions.Tests/ApiVersionFormatProviderTest.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public void get_format_should_return_expected_format_provider()
4141
}
4242

4343
[Theory]
44+
[AssumeCulture( "en-us" )]
4445
[MemberData( nameof( FormatProvidersData ) )]
4546
public void format_should_allow_null_or_empty_format_string( ApiVersionFormatProvider provider )
4647
{
@@ -56,6 +57,7 @@ public void format_should_allow_null_or_empty_format_string( ApiVersionFormatPro
5657
}
5758

5859
[Theory]
60+
[AssumeCulture( "en-us" )]
5961
[MemberData( nameof( FormatProvidersData ) )]
6062
public void format_should_return_full_formatted_string_without_optional_components( ApiVersionFormatProvider provider )
6163
{
@@ -70,6 +72,7 @@ public void format_should_return_full_formatted_string_without_optional_componen
7072
}
7173

7274
[Theory]
75+
[AssumeCulture( "en-us" )]
7376
[MemberData( nameof( FormatProvidersData ) )]
7477
public void format_should_return_full_formatted_string_with_optional_components( ApiVersionFormatProvider provider )
7578
{
@@ -84,6 +87,7 @@ public void format_should_return_full_formatted_string_with_optional_components(
8487
}
8588

8689
[Theory]
90+
[AssumeCulture( "en-us" )]
8791
[MemberData( nameof( FormatProvidersData ) )]
8892
public void format_should_return_original_string_format_when_argument_cannot_be_formatted( ApiVersionFormatProvider provider )
8993
{
@@ -113,6 +117,7 @@ public void format_should_not_allow_malformed_literal_string( ApiVersionFormatPr
113117
}
114118

115119
[Theory]
120+
[AssumeCulture( "en-us" )]
116121
[MemberData( nameof( GroupVersionFormatData ) )]
117122
public void format_should_return_formatted_group_version_string( ApiVersionFormatProvider provider, string format )
118123
{
@@ -129,6 +134,7 @@ public void format_should_return_formatted_group_version_string( ApiVersionForma
129134
}
130135

131136
[Theory]
137+
[AssumeCulture( "en-us" )]
132138
[MemberData( nameof( FormatProvidersData ) )]
133139
public void format_should_return_formatted_minor_version_string( ApiVersionFormatProvider provider )
134140
{
@@ -143,6 +149,7 @@ public void format_should_return_formatted_minor_version_string( ApiVersionForma
143149
}
144150

145151
[Theory]
152+
[AssumeCulture( "en-us" )]
146153
[MemberData( nameof( FormatProvidersData ) )]
147154
public void format_should_return_formatted_major_version_string( ApiVersionFormatProvider provider )
148155
{
@@ -157,6 +164,7 @@ public void format_should_return_formatted_major_version_string( ApiVersionForma
157164
}
158165

159166
[Theory]
167+
[AssumeCulture( "en-us" )]
160168
[MemberData( nameof( FormatProvidersData ) )]
161169
public void format_should_return_formatted_major_and_minor_version_string( ApiVersionFormatProvider provider )
162170
{
@@ -171,6 +179,7 @@ public void format_should_return_formatted_major_and_minor_version_string( ApiVe
171179
}
172180

173181
[Theory]
182+
[AssumeCulture( "en-us" )]
174183
[MemberData( nameof( FormatProvidersData ) )]
175184
public void format_should_return_formatted_short_version_string( ApiVersionFormatProvider provider )
176185
{
@@ -185,6 +194,7 @@ public void format_should_return_formatted_short_version_string( ApiVersionForma
185194
}
186195

187196
[Theory]
197+
[AssumeCulture( "en-us" )]
188198
[MemberData( nameof( FormatProvidersData ) )]
189199
public void format_should_return_formatted_long_version_string( ApiVersionFormatProvider provider )
190200
{
@@ -199,6 +209,7 @@ public void format_should_return_formatted_long_version_string( ApiVersionFormat
199209
}
200210

201211
[Theory]
212+
[AssumeCulture( "en-us" )]
202213
[MemberData( nameof( FormatProvidersData ) )]
203214
public void format_should_return_formatted_status_string( ApiVersionFormatProvider provider )
204215
{
@@ -213,6 +224,7 @@ public void format_should_return_formatted_status_string( ApiVersionFormatProvid
213224
}
214225

215226
[Theory]
227+
[AssumeCulture( "en-us" )]
216228
[MemberData( nameof( PaddedMinorVersionFormatData ) )]
217229
public void format_should_return_formatted_minor_version_with_padding_string( ApiVersionFormatProvider provider, string format )
218230
{
@@ -233,6 +245,7 @@ public void format_should_return_formatted_minor_version_with_padding_string( Ap
233245
}
234246

235247
[Theory]
248+
[AssumeCulture( "en-us" )]
236249
[MemberData( nameof( PaddedMajorVersionFormatData ) )]
237250
public void format_should_return_formatted_major_version_with_padding_string( ApiVersionFormatProvider provider, string format )
238251
{
@@ -253,6 +266,7 @@ public void format_should_return_formatted_major_version_with_padding_string( Ap
253266
}
254267

255268
[Theory]
269+
[AssumeCulture( "en-us" )]
256270
[MemberData( nameof( CustomFormatData ) )]
257271
public void format_should_return_custom_format_string( Func<ApiVersion, string> format, string expected )
258272
{
@@ -268,6 +282,7 @@ public void format_should_return_custom_format_string( Func<ApiVersion, string>
268282
}
269283

270284
[Theory]
285+
[AssumeCulture( "en-us" )]
271286
[MemberData( nameof( MultipleFormatParameterData ) )]
272287
public void format_should_return_formatted_string_with_multiple_parameters( ApiVersionFormatProvider provider, string format, object secondArgument, string expected )
273288
{
@@ -284,6 +299,7 @@ public void format_should_return_formatted_string_with_multiple_parameters( ApiV
284299
}
285300

286301
[Fact]
302+
[AssumeCulture( "en-us" )]
287303
public void format_should_return_formatted_string_with_escape_sequence()
288304
{
289305
// arrange
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright (c) .NET Foundation and contributors. All rights reserved.
2+
3+
namespace Asp.Versioning;
4+
5+
using System.Globalization;
6+
using System.Reflection;
7+
using Xunit.Sdk;
8+
using static System.AttributeTargets;
9+
using static System.Threading.Thread;
10+
11+
/// <summary>
12+
/// Allows a test method to assume that it is running in a specific locale.
13+
/// </summary>
14+
[AttributeUsage( Class | Method, AllowMultiple = false, Inherited = true )]
15+
public sealed class AssumeCultureAttribute : BeforeAfterTestAttribute
16+
{
17+
private CultureInfo originalCulture;
18+
private CultureInfo originalUICulture;
19+
20+
public AssumeCultureAttribute( string name ) => Name = name;
21+
22+
public string Name { get; }
23+
24+
public override void Before( MethodInfo methodUnderTest )
25+
{
26+
originalCulture = CurrentThread.CurrentCulture;
27+
originalUICulture = CurrentThread.CurrentUICulture;
28+
29+
var culture = CultureInfo.CreateSpecificCulture( Name );
30+
31+
CurrentThread.CurrentCulture = culture;
32+
CurrentThread.CurrentUICulture = culture;
33+
}
34+
35+
public override void After( MethodInfo methodUnderTest )
36+
{
37+
CurrentThread.CurrentCulture = originalCulture;
38+
CurrentThread.CurrentUICulture = originalUICulture;
39+
}
40+
}

0 commit comments

Comments
 (0)