Skip to content

Commit

Permalink
Implement TestCultureAttribute
Browse files Browse the repository at this point in the history
  • Loading branch information
Youssef1313 committed Nov 12, 2024
1 parent ac9cc4c commit c98c4ed
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/Adapter/MSTest.TestAdapter/Execution/TestMethodInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ internal TestMethodInfo(
TestMethod = testMethod;
Parent = parent;
TestMethodOptions = testMethodOptions;
TestCultureName = GetAttributes<TestCultureAttribute>(inherit: false).FirstOrDefault()?.CultureName;
}

/// <summary>
Expand Down Expand Up @@ -87,6 +88,8 @@ internal TestMethodInfo(
/// </summary>
internal TestMethodOptions TestMethodOptions { get; }

internal string? TestCultureName { get; }

public Attribute[]? GetAllAttributes(bool inherit) => ReflectHelper.Instance.GetDerivedAttributes<Attribute>(TestMethod, inherit).ToArray();

public TAttributeType[] GetAttributes<TAttributeType>(bool inherit)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@ public override TestResult[] Execute(ITestMethod testMethod)
TestResult? result = null;
Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(
Windows.UI.Core.CoreDispatcherPriority.Normal,
() => result = testMethod.Invoke(null)).AsTask().GetAwaiter().GetResult();
() =>
{
using (SetCultureForTest(testMethod))
{
result = testMethod.Invoke(null);
}
}).AsTask().GetAwaiter().GetResult();

return [result!];
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace Microsoft.VisualStudio.TestTools.UnitTesting;

/// <summary>
/// Attribute to specify the CultureInfo.CurrentCulture and CultureInfo.CurrentUICulture when running the test.
/// </summary>
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
public sealed class TestCultureAttribute : Attribute
{
/// <summary>
/// Initializes a new instance of the <see cref="TestCultureAttribute"/> class.
/// </summary>
/// <param name="cultureName">
/// The culture to be used for the test. For example, "en-US".
/// </param>
public TestCultureAttribute(string cultureName) => CultureName = cultureName;

/// <summary>
/// Gets the owner.
/// </summary>
public string CultureName { get; }
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Globalization;

namespace Microsoft.VisualStudio.TestTools.UnitTesting;

/// <summary>
Expand All @@ -9,6 +11,29 @@ namespace Microsoft.VisualStudio.TestTools.UnitTesting;
[AttributeUsage(AttributeTargets.Method)]
public class TestMethodAttribute : Attribute
{
private protected readonly struct ScopedCultureDisposable : IDisposable
{
private readonly CultureInfo _previousCulture;
private readonly CultureInfo _previousUICulture;

public ScopedCultureDisposable(string cultureName)
{
_previousCulture = CultureInfo.CurrentCulture;
_previousUICulture = CultureInfo.CurrentUICulture;

var newCulture = new CultureInfo(cultureName);
// TODO: Should we set both? Should we have different attribute? Same attribute with two arguments?
CultureInfo.CurrentCulture = newCulture;
CultureInfo.CurrentUICulture = newCulture;
}

public void Dispose()
{
CultureInfo.CurrentCulture = _previousCulture;
CultureInfo.CurrentUICulture = _previousUICulture;
}
}

/// <summary>
/// Initializes a new instance of the <see cref="TestMethodAttribute"/> class.
/// </summary>
Expand Down Expand Up @@ -36,5 +61,19 @@ public TestMethodAttribute()
/// <param name="testMethod">The test method to execute.</param>
/// <returns>An array of TestResult objects that represent the outcome(s) of the test.</returns>
/// <remarks>Extensions can override this method to customize running a TestMethod.</remarks>
public virtual TestResult[] Execute(ITestMethod testMethod) => [testMethod.Invoke(null)];
public virtual TestResult[] Execute(ITestMethod testMethod)
{
using (SetCultureForTest(testMethod))
{
return [testMethod.Invoke(null)];
}
}

// TestMethodInfo isn't accessible here :/
// Can we add TestCultureName to the *public* interface?
// Or should we introduce an internal interface ITestMethod2 : ITestMethod :/
private protected static ScopedCultureDisposable? SetCultureForTest(ITestMethod testMethod)
=> testMethod is TestMethodInfo testMethodInfo && testMethodInfo.TestCultureName is { } culture

Check failure on line 76 in src/TestFramework/TestFramework/Attributes/TestMethod/TestMethodAttribute.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build Linux Release)

src/TestFramework/TestFramework/Attributes/TestMethod/TestMethodAttribute.cs#L76

src/TestFramework/TestFramework/Attributes/TestMethod/TestMethodAttribute.cs(76,26): error CS0246: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'TestMethodInfo' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 76 in src/TestFramework/TestFramework/Attributes/TestMethod/TestMethodAttribute.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build Linux Release)

src/TestFramework/TestFramework/Attributes/TestMethod/TestMethodAttribute.cs#L76

src/TestFramework/TestFramework/Attributes/TestMethod/TestMethodAttribute.cs(76,26): error CS0246: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'TestMethodInfo' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 76 in src/TestFramework/TestFramework/Attributes/TestMethod/TestMethodAttribute.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build Linux Release)

src/TestFramework/TestFramework/Attributes/TestMethod/TestMethodAttribute.cs#L76

src/TestFramework/TestFramework/Attributes/TestMethod/TestMethodAttribute.cs(76,26): error CS0246: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'TestMethodInfo' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 76 in src/TestFramework/TestFramework/Attributes/TestMethod/TestMethodAttribute.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build Linux Release)

src/TestFramework/TestFramework/Attributes/TestMethod/TestMethodAttribute.cs#L76

src/TestFramework/TestFramework/Attributes/TestMethod/TestMethodAttribute.cs(76,26): error CS0246: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'TestMethodInfo' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 76 in src/TestFramework/TestFramework/Attributes/TestMethod/TestMethodAttribute.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build Linux Release)

src/TestFramework/TestFramework/Attributes/TestMethod/TestMethodAttribute.cs#L76

src/TestFramework/TestFramework/Attributes/TestMethod/TestMethodAttribute.cs(76,26): error CS0246: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'TestMethodInfo' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 76 in src/TestFramework/TestFramework/Attributes/TestMethod/TestMethodAttribute.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build Linux Debug)

src/TestFramework/TestFramework/Attributes/TestMethod/TestMethodAttribute.cs#L76

src/TestFramework/TestFramework/Attributes/TestMethod/TestMethodAttribute.cs(76,26): error CS0246: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'TestMethodInfo' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 76 in src/TestFramework/TestFramework/Attributes/TestMethod/TestMethodAttribute.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build Linux Debug)

src/TestFramework/TestFramework/Attributes/TestMethod/TestMethodAttribute.cs#L76

src/TestFramework/TestFramework/Attributes/TestMethod/TestMethodAttribute.cs(76,26): error CS0246: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'TestMethodInfo' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 76 in src/TestFramework/TestFramework/Attributes/TestMethod/TestMethodAttribute.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build Linux Debug)

src/TestFramework/TestFramework/Attributes/TestMethod/TestMethodAttribute.cs#L76

src/TestFramework/TestFramework/Attributes/TestMethod/TestMethodAttribute.cs(76,26): error CS0246: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'TestMethodInfo' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 76 in src/TestFramework/TestFramework/Attributes/TestMethod/TestMethodAttribute.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build Linux Debug)

src/TestFramework/TestFramework/Attributes/TestMethod/TestMethodAttribute.cs#L76

src/TestFramework/TestFramework/Attributes/TestMethod/TestMethodAttribute.cs(76,26): error CS0246: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'TestMethodInfo' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 76 in src/TestFramework/TestFramework/Attributes/TestMethod/TestMethodAttribute.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build Linux Debug)

src/TestFramework/TestFramework/Attributes/TestMethod/TestMethodAttribute.cs#L76

src/TestFramework/TestFramework/Attributes/TestMethod/TestMethodAttribute.cs(76,26): error CS0246: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'TestMethodInfo' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 76 in src/TestFramework/TestFramework/Attributes/TestMethod/TestMethodAttribute.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx

src/TestFramework/TestFramework/Attributes/TestMethod/TestMethodAttribute.cs#L76

src/TestFramework/TestFramework/Attributes/TestMethod/TestMethodAttribute.cs(76,26): error CS0246: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'TestMethodInfo' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 76 in src/TestFramework/TestFramework/Attributes/TestMethod/TestMethodAttribute.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx

src/TestFramework/TestFramework/Attributes/TestMethod/TestMethodAttribute.cs#L76

src/TestFramework/TestFramework/Attributes/TestMethod/TestMethodAttribute.cs(76,26): error CS0246: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'TestMethodInfo' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 76 in src/TestFramework/TestFramework/Attributes/TestMethod/TestMethodAttribute.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx

src/TestFramework/TestFramework/Attributes/TestMethod/TestMethodAttribute.cs#L76

src/TestFramework/TestFramework/Attributes/TestMethod/TestMethodAttribute.cs(76,26): error CS0246: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'TestMethodInfo' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 76 in src/TestFramework/TestFramework/Attributes/TestMethod/TestMethodAttribute.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx

src/TestFramework/TestFramework/Attributes/TestMethod/TestMethodAttribute.cs#L76

src/TestFramework/TestFramework/Attributes/TestMethod/TestMethodAttribute.cs(76,26): error CS0246: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'TestMethodInfo' could not be found (are you missing a using directive or an assembly reference?)
? new ScopedCultureDisposable(culture)
: (ScopedCultureDisposable?)null;
}
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
#nullable enable
Microsoft.VisualStudio.TestTools.UnitTesting.TestCultureAttribute
Microsoft.VisualStudio.TestTools.UnitTesting.TestCultureAttribute.CultureName.get -> string!
Microsoft.VisualStudio.TestTools.UnitTesting.TestCultureAttribute.TestCultureAttribute(string! cultureName) -> void

0 comments on commit c98c4ed

Please sign in to comment.