Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
}
}
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;
}
}
}
}
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.
Copy link
Copy Markdown
Member

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?

Copy link
Copy Markdown
Member

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?


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)!;
}
}
}
Loading