Skip to content

Commit

Permalink
feat: Hide SetCulture as Category
Browse files Browse the repository at this point in the history
  • Loading branch information
samtrion committed Dec 17, 2024
1 parent fe631c8 commit c3c22ac
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ void IApplyToTest.ApplyToTest(Test test)
return;
}

base.ApplyToTest(test);
ApplyToTest(test);

if (!string.IsNullOrEmpty(Id))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ IAttributeInfo traitAttribute
yield break;
}

var category = GetNamedArgument(traitAttribute, Internals.Category);
var category = GetNamedArgument<string>(traitAttribute, Internals.Category);
if (string.IsNullOrWhiteSpace(category))
{
yield break;
}

yield return new KeyValuePair<string, string>(Internals.TestCategory, category!);

var id = GetNamedArgument(traitAttribute, Internals.Id);
var id = GetNamedArgument<string>(traitAttribute, Internals.Id);

if (!string.IsNullOrWhiteSpace(id))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,17 @@ public abstract class CultureAttributeBase : BeforeAfterTestAttribute, ITraitAtt
/// </summary>
public string Culture { get; }

/// <summary>
/// As hidden category
/// </summary>
public bool AsHiddenCategory { get; }

/// <inheritdoc/>
protected CultureAttributeBase(string category, string culture)
protected CultureAttributeBase(string category, string culture, bool asHiddenCategory)
{
Category = category;
Culture = culture;
AsHiddenCategory = asHiddenCategory;
_culture = CreateCultureInfo(culture);
}

Expand Down
15 changes: 12 additions & 3 deletions src/NetEvolve.Extensions.XUnit/Internal/CultureTraitDiscoverer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,25 @@ IAttributeInfo traitAttribute
yield break;
}

var category = GetNamedArgument(traitAttribute, Internals.Category);
var asHiddenCategory = GetNamedArgument<bool>(
traitAttribute,
nameof(CultureAttributeBase.AsHiddenCategory)
);
if (asHiddenCategory)
{
yield break;

Check warning on line 28 in src/NetEvolve.Extensions.XUnit/Internal/CultureTraitDiscoverer.cs

View check run for this annotation

Codecov / codecov/patch

src/NetEvolve.Extensions.XUnit/Internal/CultureTraitDiscoverer.cs#L28

Added line #L28 was not covered by tests
}

var category = GetNamedArgument<string>(traitAttribute, Internals.Category);
if (string.IsNullOrWhiteSpace(category))
{
yield break;
}

var culture = GetNamedArgument(traitAttribute, Internals.Culture);
var culture = GetNamedArgument<string>(traitAttribute, Internals.Culture);
yield return new KeyValuePair<string, string>(category!, culture!);

if (category!.Equals("SetCulture", System.StringComparison.Ordinal))
if (category!.Equals("SetCulture", StringComparison.Ordinal))
{
yield return new KeyValuePair<string, string>("SetUICulture", culture!);
}
Expand Down
6 changes: 3 additions & 3 deletions src/NetEvolve.Extensions.XUnit/Internal/DiscovererBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@ public abstract IEnumerable<KeyValuePair<string, string>> GetTraits(
IAttributeInfo traitAttribute
);

private protected static string? GetNamedArgument(
private protected static TValue? GetNamedArgument<TValue>(
IAttributeInfo traitAttribute,
string argumentName
)
{
try
{
return traitAttribute.GetNamedArgument<string>(argumentName);
return traitAttribute.GetNamedArgument<TValue>(argumentName);
}
catch (ArgumentException)
{
// Ignore
}
return null;
return default;
}
}
12 changes: 7 additions & 5 deletions src/NetEvolve.Extensions.XUnit/SetCultureAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,24 @@ public sealed class SetCultureAttribute : CultureAttributeBase
/// Initializes a new instance of the <see cref="SetCultureAttribute"/> class.
/// </summary>
public SetCultureAttribute()
: this(string.Empty, string.Empty) { }
: this(string.Empty, string.Empty, false) { }

/// <summary>
/// Initializes a new instance of the <see cref="SetCultureAttribute"/> class.
/// </summary>
/// <param name="culture">Culture to use.</param>
public SetCultureAttribute(string culture)
: this(culture, culture) { }
/// <param name="asHiddenCategory">If <see langword="true"/>, this test will be hidden from test explorer.</param>
public SetCultureAttribute(string culture, bool asHiddenCategory = false)
: this(culture, culture, asHiddenCategory) { }

/// <summary>
/// Initializes a new instance of the <see cref="SetCultureAttribute"/> class.
/// </summary>
/// <param name="culture">Culture to use.</param>
/// <param name="uiCulture">UI culture to use.</param>
public SetCultureAttribute(string culture, string? uiCulture)
: base("SetCulture", culture)
/// <param name="asHiddenCategory">If <see langword="true"/>, this test will be hidden from test explorer.</param>
public SetCultureAttribute(string culture, string? uiCulture, bool asHiddenCategory = false)
: base("SetCulture", culture, asHiddenCategory)
{
if (string.IsNullOrWhiteSpace(uiCulture))
{
Expand Down
7 changes: 4 additions & 3 deletions src/NetEvolve.Extensions.XUnit/SetUICultureAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ public sealed class SetUICultureAttribute : CultureAttributeBase
/// Initializes a new instance of the <see cref="SetUICultureAttribute"/> class.
/// </summary>
public SetUICultureAttribute()
: base("SetUICulture", string.Empty) { }
: base("SetUICulture", string.Empty, false) { }

/// <summary>
/// Initializes a new instance of the <see cref="SetUICultureAttribute"/> class.
/// </summary>
/// <param name="culture">UI culture to use.</param>
public SetUICultureAttribute(string culture)
: base("SetUICulture", culture) { }
/// <param name="asHiddenCategory">If <see langword="true"/>, this test will be hidden from test explorer.</param>
public SetUICultureAttribute(string culture, bool asHiddenCategory = false)
: base("SetUICulture", culture, asHiddenCategory) { }

private protected override bool SetCulture(CultureInfo culture)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public abstract class AttributeTestsBase
/// </summary>
/// <param name="methodName">Name of the caller Method</param>
/// <returns>List of <see cref="KeyValuePair{TKey,TValue}"/></returns>
protected IEnumerable<KeyValuePair<string, string>> GetTraits(
protected static IEnumerable<KeyValuePair<string, string>> GetTraits(
[CallerMemberName] string? methodName = null
)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,16 @@
public sealed class SetCultureAttribute : NetEvolve.Extensions.XUnit.Internal.CultureAttributeBase
{
public SetCultureAttribute() { }
public SetCultureAttribute(string culture) { }
public SetCultureAttribute(string culture, string? uiCulture) { }
public SetCultureAttribute(string culture, bool asHiddenCategory = false) { }
public SetCultureAttribute(string culture, string? uiCulture, bool asHiddenCategory = false) { }
public string? UICulture { get; }
public override void After(System.Reflection.MethodInfo methodUnderTest) { }
public override void Before(System.Reflection.MethodInfo methodUnderTest) { }
}
public sealed class SetUICultureAttribute : NetEvolve.Extensions.XUnit.Internal.CultureAttributeBase
{
public SetUICultureAttribute() { }
public SetUICultureAttribute(string culture) { }
public SetUICultureAttribute(string culture, bool asHiddenCategory = false) { }
}
public sealed class UnitTestAttribute : NetEvolve.Extensions.XUnit.Internal.CategoryTraitBaseAttribute
{
Expand Down Expand Up @@ -115,7 +115,8 @@ namespace NetEvolve.Extensions.XUnit.Internal
[Xunit.Sdk.TraitDiscoverer("NetEvolve.Extensions.XUnit.Internal.CultureTraitDiscoverer", "NetEvolve.Extensions.XUnit")]
public abstract class CultureAttributeBase : Xunit.Sdk.BeforeAfterTestAttribute, Xunit.Sdk.ITraitAttribute
{
protected CultureAttributeBase(string category, string culture) { }
protected CultureAttributeBase(string category, string culture, bool asHiddenCategory) { }
public bool AsHiddenCategory { get; }
public string Category { get; }
public string Culture { get; }
public override void After(System.Reflection.MethodInfo methodUnderTest) { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,16 @@
public sealed class SetCultureAttribute : NetEvolve.Extensions.XUnit.Internal.CultureAttributeBase
{
public SetCultureAttribute() { }
public SetCultureAttribute(string culture) { }
public SetCultureAttribute(string culture, string? uiCulture) { }
public SetCultureAttribute(string culture, bool asHiddenCategory = false) { }
public SetCultureAttribute(string culture, string? uiCulture, bool asHiddenCategory = false) { }
public string? UICulture { get; }
public override void After(System.Reflection.MethodInfo methodUnderTest) { }
public override void Before(System.Reflection.MethodInfo methodUnderTest) { }
}
public sealed class SetUICultureAttribute : NetEvolve.Extensions.XUnit.Internal.CultureAttributeBase
{
public SetUICultureAttribute() { }
public SetUICultureAttribute(string culture) { }
public SetUICultureAttribute(string culture, bool asHiddenCategory = false) { }
}
public sealed class UnitTestAttribute : NetEvolve.Extensions.XUnit.Internal.CategoryTraitBaseAttribute
{
Expand Down Expand Up @@ -115,7 +115,8 @@ namespace NetEvolve.Extensions.XUnit.Internal
[Xunit.Sdk.TraitDiscoverer("NetEvolve.Extensions.XUnit.Internal.CultureTraitDiscoverer", "NetEvolve.Extensions.XUnit")]
public abstract class CultureAttributeBase : Xunit.Sdk.BeforeAfterTestAttribute, Xunit.Sdk.ITraitAttribute
{
protected CultureAttributeBase(string category, string culture) { }
protected CultureAttributeBase(string category, string culture, bool asHiddenCategory) { }
public bool AsHiddenCategory { get; }
public string Category { get; }
public string Culture { get; }
public override void After(System.Reflection.MethodInfo methodUnderTest) { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,16 @@
public sealed class SetCultureAttribute : NetEvolve.Extensions.XUnit.Internal.CultureAttributeBase
{
public SetCultureAttribute() { }
public SetCultureAttribute(string culture) { }
public SetCultureAttribute(string culture, string? uiCulture) { }
public SetCultureAttribute(string culture, bool asHiddenCategory = false) { }
public SetCultureAttribute(string culture, string? uiCulture, bool asHiddenCategory = false) { }
public string? UICulture { get; }
public override void After(System.Reflection.MethodInfo methodUnderTest) { }
public override void Before(System.Reflection.MethodInfo methodUnderTest) { }
}
public sealed class SetUICultureAttribute : NetEvolve.Extensions.XUnit.Internal.CultureAttributeBase
{
public SetUICultureAttribute() { }
public SetUICultureAttribute(string culture) { }
public SetUICultureAttribute(string culture, bool asHiddenCategory = false) { }
}
public sealed class UnitTestAttribute : NetEvolve.Extensions.XUnit.Internal.CategoryTraitBaseAttribute
{
Expand Down Expand Up @@ -115,7 +115,8 @@ namespace NetEvolve.Extensions.XUnit.Internal
[Xunit.Sdk.TraitDiscoverer("NetEvolve.Extensions.XUnit.Internal.CultureTraitDiscoverer", "NetEvolve.Extensions.XUnit")]
public abstract class CultureAttributeBase : Xunit.Sdk.BeforeAfterTestAttribute, Xunit.Sdk.ITraitAttribute
{
protected CultureAttributeBase(string category, string culture) { }
protected CultureAttributeBase(string category, string culture, bool asHiddenCategory) { }
public bool AsHiddenCategory { get; }
public string Category { get; }
public string Culture { get; }
public override void After(System.Reflection.MethodInfo methodUnderTest) { }
Expand Down

0 comments on commit c3c22ac

Please sign in to comment.