Skip to content

Commit 5c45157

Browse files
sander1095commonsensesoftware
authored andcommitted
Add AssumeCultureAttribute for locale-specific testing and update API descriptions
The AssumeCultureAttribute is, for now, copied from another test project. PR feedback can indicate if a shared approach is preferred, and, if so, how that can be achieved
1 parent a69fcec commit 5c45157

File tree

4 files changed

+46
-3
lines changed

4 files changed

+46
-3
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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 static System.AttributeTargets;
8+
using static System.Threading.Thread;
9+
10+
/// <summary>
11+
/// Allows a test method to assume that it is running in a specific locale.
12+
/// </summary>
13+
[AttributeUsage( Class | Method, AllowMultiple = false, Inherited = true )]
14+
internal sealed class AssumeCultureAttribute : BeforeAfterTestAttribute
15+
{
16+
private CultureInfo originalCulture;
17+
private CultureInfo originalUICulture;
18+
19+
public AssumeCultureAttribute( string name ) => Name = name;
20+
21+
public string Name { get; }
22+
23+
public override void Before( MethodInfo methodUnderTest, IXunitTest test )
24+
{
25+
originalCulture = CurrentThread.CurrentCulture;
26+
originalUICulture = CurrentThread.CurrentUICulture;
27+
28+
var culture = CultureInfo.CreateSpecificCulture( Name );
29+
30+
CurrentThread.CurrentCulture = culture;
31+
CurrentThread.CurrentUICulture = culture;
32+
}
33+
34+
public override void After( MethodInfo methodUnderTest, IXunitTest test )
35+
{
36+
CurrentThread.CurrentCulture = originalCulture;
37+
CurrentThread.CurrentUICulture = originalUICulture;
38+
}
39+
}

src/AspNetCore/WebApi/test/Asp.Versioning.OpenApi.Tests/Content/v1-minimal.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"openapi": "3.1.1",
33
"info": {
44
"title": "Test API | v1",
5-
"description": "The API was deprecated on 01/01/2026. The API was sunset on 02/10/2026.\n\n### Links\n\n- [Version Deprecation Policy](http://my.api.com/policies/versions/deprecated.html)\n- [Version Sunset Policy](http://my.api.com/policies/versions/sunset.html)",
5+
"description": "The API was deprecated on 1/1/2026. The API was sunset on 2/10/2026.\r\n\r\n### Links\r\n\r\n- [Version Deprecation Policy](http://my.api.com/policies/versions/deprecated.html)\r\n- [Version Sunset Policy](http://my.api.com/policies/versions/sunset.html)",
66
"version": "1.0"
77
},
88
"servers": [

src/AspNetCore/WebApi/test/Asp.Versioning.OpenApi.Tests/Content/v1-mixed.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"openapi": "3.1.1",
33
"info": {
44
"title": "Test API | v1",
5-
"description": "The API was deprecated on 01/01/2026. The API was sunset on 02/10/2026.\n\n### Links\n\n- [Version Deprecation Policy](http://my.api.com/policies/versions/deprecated.html)\n- [Version Sunset Policy](http://my.api.com/policies/versions/sunset.html)",
5+
"description": "The API was deprecated on 1/1/2026. The API was sunset on 2/10/2026.\r\n\r\n### Links\r\n\r\n- [Version Deprecation Policy](http://my.api.com/policies/versions/deprecated.html)\r\n- [Version Sunset Policy](http://my.api.com/policies/versions/sunset.html)",
66
"version": "1.0"
77
},
88
"servers": [

src/AspNetCore/WebApi/test/Asp.Versioning.OpenApi.Tests/Transformers/AcceptanceTest.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ namespace Asp.Versioning.OpenApi.Transformers;
77
using Microsoft.AspNetCore.Http;
88
using Microsoft.AspNetCore.TestHost;
99
using Microsoft.Extensions.DependencyInjection;
10+
using System.Globalization;
1011
using System.Net.Http.Json;
1112
using System.Text.Json.Nodes;
1213

@@ -20,11 +21,12 @@ public class AcceptanceTest
2021
/// </summary>
2122
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
2223
[Fact]
24+
[AssumeCulture( "en-US" )]
2325
public async Task minimal_api_should_generate_expected_open_api_document()
2426
{
2527
// arrange
2628
var builder = WebApplication.CreateBuilder();
27-
29+
var culture = CultureInfo.CurrentCulture;
2830
builder.WebHost.UseTestServer();
2931
builder.Services.AddApiVersioning( options => AddPolicies( options ) )
3032
.AddApiExplorer( options => options.GroupNameFormat = "'v'VVV" )
@@ -57,6 +59,7 @@ public async Task minimal_api_should_generate_expected_open_api_document()
5759
}
5860

5961
[Fact]
62+
[AssumeCulture( "en-US" )]
6063
public async Task controller_should_generate_expected_open_api_document()
6164
{
6265
// arrange
@@ -91,6 +94,7 @@ public async Task controller_should_generate_expected_open_api_document()
9194
}
9295

9396
[Fact]
97+
[AssumeCulture( "en-US" )]
9498
public async Task mixed_api_should_generate_expected_open_api_document()
9599
{
96100
// arrange

0 commit comments

Comments
 (0)