-
Notifications
You must be signed in to change notification settings - Fork 391
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
Merged
AndriySvyryd
merged 5 commits into
main
from
copilot/add-cultured-conditional-attributes
Jul 14, 2026
+172
−0
Merged
Add CulturedConditionalFactAttribute and CulturedConditionalTheoryAttribute to XUnitV3Extensions #16825
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
613a7c1
Add CulturedConditionalFact/Theory attributes and tests
Copilot 61bf11c
Add braces around if blocks and remove redundant inheritance tests
Copilot e7bd79a
Remove extra using
AndriySvyryd 729688c
Add source information constructor parameters to avoid xUnit3003 errors
Copilot b46f25c
Merge branch 'main' into copilot/add-cultured-conditional-attributes
AndriySvyryd 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
36 changes: 36 additions & 0 deletions
36
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,36 @@ | ||
| // 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 System.Runtime.CompilerServices; | ||
| 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, | ||
| string[] conditionMemberNames, | ||
| [CallerFilePath] string sourceFilePath = null, | ||
| [CallerLineNumber] int sourceLineNumber = 0) | ||
| : base(cultures, sourceFilePath, sourceLineNumber) | ||
| { | ||
| CalleeType = calleeType; | ||
| ConditionMemberNames = conditionMemberNames; | ||
| string skipReason = ConditionalTestDiscoverer.EvaluateSkipConditions(calleeType, conditionMemberNames); | ||
| if (skipReason != null) | ||
| { | ||
| Skip = skipReason; | ||
| } | ||
| } | ||
| } | ||
| } |
36 changes: 36 additions & 0 deletions
36
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,36 @@ | ||
| // 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 System.Runtime.CompilerServices; | ||
| 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, | ||
| string[] conditionMemberNames, | ||
| [CallerFilePath] string sourceFilePath = null, | ||
| [CallerLineNumber] int sourceLineNumber = 0) | ||
| : base(cultures, sourceFilePath, sourceLineNumber) | ||
| { | ||
| 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), new[] { 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), new[] { nameof(AlwaysFalse) })] | ||
| public void CulturedConditionalFactFalse() | ||
| { | ||
| Assert.Fail("This test should have been skipped."); | ||
| } | ||
|
|
||
| [CulturedConditionalTheory(new[] { "en-US", "fr-FR" }, typeof(CulturedConditionalAttributeTests), new[] { 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), new[] { 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.
Uh oh!
There was an error while loading. Please reload this page.