-
Notifications
You must be signed in to change notification settings - Fork 388
Add CulturedConditionalFactAttribute and CulturedConditionalTheoryAttribute to XUnitV3Extensions #16825
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Copilot
wants to merge
3
commits into
main
Choose a base branch
from
copilot/add-cultured-conditional-attributes
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add CulturedConditionalFactAttribute and CulturedConditionalTheoryAttribute to XUnitV3Extensions #16825
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
src/Microsoft.DotNet.XUnitV3Extensions/src/CulturedConditionalFactAttribute.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System; | ||
| using System.Diagnostics.CodeAnalysis; | ||
| using Microsoft.DotNet.XUnitExtensions; | ||
|
|
||
| namespace Xunit | ||
| { | ||
| [AttributeUsage(AttributeTargets.Method, AllowMultiple = false)] | ||
| public sealed class CulturedConditionalFactAttribute : CulturedFactAttribute | ||
| { | ||
| [DynamicallyAccessedMembers(StaticReflectionConstants.ConditionalMemberKinds)] | ||
| public Type CalleeType { get; private set; } | ||
| public string[] ConditionMemberNames { get; private set; } | ||
|
|
||
| public CulturedConditionalFactAttribute( | ||
| string[] cultures, | ||
| [DynamicallyAccessedMembers(StaticReflectionConstants.ConditionalMemberKinds)] | ||
| Type calleeType, | ||
| params string[] conditionMemberNames) | ||
| : base(cultures) | ||
| { | ||
| CalleeType = calleeType; | ||
| ConditionMemberNames = conditionMemberNames; | ||
| string skipReason = ConditionalTestDiscoverer.EvaluateSkipConditions(calleeType, conditionMemberNames); | ||
| if (skipReason != null) | ||
| { | ||
| Skip = skipReason; | ||
| } | ||
| } | ||
| } | ||
| } |
33 changes: 33 additions & 0 deletions
33
src/Microsoft.DotNet.XUnitV3Extensions/src/CulturedConditionalTheoryAttribute.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System; | ||
| using System.Diagnostics.CodeAnalysis; | ||
| using Microsoft.DotNet.XUnitExtensions; | ||
|
|
||
| namespace Xunit | ||
| { | ||
| [AttributeUsage(AttributeTargets.Method, AllowMultiple = false)] | ||
| public sealed class CulturedConditionalTheoryAttribute : CulturedTheoryAttribute | ||
| { | ||
| [DynamicallyAccessedMembers(StaticReflectionConstants.ConditionalMemberKinds)] | ||
| public Type CalleeType { get; private set; } | ||
| public string[] ConditionMemberNames { get; private set; } | ||
|
|
||
| public CulturedConditionalTheoryAttribute( | ||
| string[] cultures, | ||
| [DynamicallyAccessedMembers(StaticReflectionConstants.ConditionalMemberKinds)] | ||
| Type calleeType, | ||
| params string[] conditionMemberNames) | ||
| : base(cultures) | ||
| { | ||
| CalleeType = calleeType; | ||
| ConditionMemberNames = conditionMemberNames; | ||
| string skipReason = ConditionalTestDiscoverer.EvaluateSkipConditions(calleeType, conditionMemberNames); | ||
| if (skipReason != null) | ||
| { | ||
| Skip = skipReason; | ||
| } | ||
| } | ||
| } | ||
| } |
100 changes: 100 additions & 0 deletions
100
src/Microsoft.DotNet.XUnitV3Extensions/tests/CulturedConditionalAttributeTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Globalization; | ||
| using System.Reflection; | ||
| using Xunit; | ||
|
|
||
| namespace Microsoft.DotNet.XUnitExtensions.Tests | ||
| { | ||
| public class CulturedConditionalAttributeTests | ||
| { | ||
| // These tests validate the xunit v3 cultured conditional attributes without relying on | ||
| // execution order, which the v3 runner does not guarantee for this scenario. | ||
|
|
||
| private static readonly string[] s_cultures = new[] { "en-US", "fr-FR" }; | ||
|
|
||
| public static bool AlwaysTrue => true; | ||
| public static bool AlwaysFalse => false; | ||
|
|
||
| [CulturedConditionalFact(new[] { "en-US", "fr-FR" }, typeof(CulturedConditionalAttributeTests), nameof(AlwaysTrue))] | ||
| public void CulturedConditionalFactTrue() | ||
| { | ||
| // The current culture must be one of the requested cultures when the test runs. | ||
| Assert.Contains(CultureInfo.CurrentCulture.Name, s_cultures); | ||
| } | ||
|
|
||
| [CulturedConditionalFact(new[] { "en-US", "fr-FR" }, typeof(CulturedConditionalAttributeTests), nameof(AlwaysFalse))] | ||
| public void CulturedConditionalFactFalse() | ||
| { | ||
| Assert.Fail("This test should have been skipped."); | ||
| } | ||
|
|
||
| [CulturedConditionalTheory(new[] { "en-US", "fr-FR" }, typeof(CulturedConditionalAttributeTests), nameof(AlwaysTrue))] | ||
| [InlineData(1, "one")] | ||
| [InlineData(2, "two")] | ||
| public void CulturedConditionalTheoryTrue(int number, string name) | ||
| { | ||
| // Verify the arguments were actually passed through and the culture was set. | ||
| Assert.True(number > 0); | ||
| Assert.False(string.IsNullOrEmpty(name)); | ||
| Assert.Contains(CultureInfo.CurrentCulture.Name, s_cultures); | ||
| } | ||
|
|
||
| [CulturedConditionalTheory(new[] { "en-US", "fr-FR" }, typeof(CulturedConditionalAttributeTests), nameof(AlwaysFalse))] | ||
| [InlineData(1)] | ||
| [InlineData(2)] | ||
| #pragma warning disable xUnit1026 // Theory methods should use all of their parameters | ||
| public void CulturedConditionalTheoryFalse(int value) | ||
| #pragma warning restore xUnit1026 | ||
| { | ||
| Assert.Fail($"This test should have been skipped, but ran with value {value}."); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ValidateCulturedConditionalFactSkipState() | ||
| { | ||
| Assert.Null(GetCulturedConditionalFactAttribute(nameof(CulturedConditionalFactTrue)).Skip); | ||
| Assert.Equal("Condition(s) not met: \"AlwaysFalse\"", GetCulturedConditionalFactAttribute(nameof(CulturedConditionalFactFalse)).Skip); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ValidateCulturedConditionalTheorySkipState() | ||
| { | ||
| Assert.Null(GetCulturedConditionalTheoryAttribute(nameof(CulturedConditionalTheoryTrue)).Skip); | ||
| Assert.Equal("Condition(s) not met: \"AlwaysFalse\"", GetCulturedConditionalTheoryAttribute(nameof(CulturedConditionalTheoryFalse)).Skip); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ValidateCulturedConditionalFactProperties() | ||
| { | ||
| CulturedConditionalFactAttribute attribute = GetCulturedConditionalFactAttribute(nameof(CulturedConditionalFactTrue)); | ||
| Assert.Equal(typeof(CulturedConditionalAttributeTests), attribute.CalleeType); | ||
| Assert.Equal(new[] { nameof(AlwaysTrue) }, attribute.ConditionMemberNames); | ||
| Assert.Equal(s_cultures, attribute.Cultures); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ValidateCulturedConditionalTheoryProperties() | ||
| { | ||
| CulturedConditionalTheoryAttribute attribute = GetCulturedConditionalTheoryAttribute(nameof(CulturedConditionalTheoryTrue)); | ||
| Assert.Equal(typeof(CulturedConditionalAttributeTests), attribute.CalleeType); | ||
| Assert.Equal(new[] { nameof(AlwaysTrue) }, attribute.ConditionMemberNames); | ||
| Assert.Equal(s_cultures, attribute.Cultures); | ||
| } | ||
|
|
||
| private static CulturedConditionalFactAttribute GetCulturedConditionalFactAttribute(string methodName) | ||
| { | ||
| return (CulturedConditionalFactAttribute)typeof(CulturedConditionalAttributeTests) | ||
| .GetMethod(methodName, BindingFlags.Instance | BindingFlags.Public)! | ||
| .GetCustomAttribute(typeof(CulturedConditionalFactAttribute), inherit: false)!; | ||
| } | ||
|
|
||
| private static CulturedConditionalTheoryAttribute GetCulturedConditionalTheoryAttribute(string methodName) | ||
| { | ||
| return (CulturedConditionalTheoryAttribute)typeof(CulturedConditionalAttributeTests) | ||
| .GetMethod(methodName, BindingFlags.Instance | BindingFlags.Public)! | ||
| .GetCustomAttribute(typeof(CulturedConditionalTheoryAttribute), inherit: false)!; | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@AndriySvyryd won't this potentially cause issues if the tests run concurrently with other tests and the process culture is changed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That would be an issue in XUnit.
https://github.com/xunit/xunit/blob/main/src/xunit.v3.core/ObjectModel/CulturedXunitTestCase.cs#L103 sets it globally, but in V2 it was set only for a specific thread:
https://github.com/xunit/xunit/blob/main/src/xunit.v2.tests/TestUtility/CultureAwareTesting/CulturedXunitTestCase.cs#L53
This looks like a deliberate change, so there must be some mechanism to isolate the other tests.
@bradwilson Would you like to weigh it?