CultureAttribute
Marks an assembly, test fixture or test method as applying to a specific Culture.
using NUnit.Framework.Interfaces;
using NUnit.Framework.Internal;
using System;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Runtime.CompilerServices;
namespace NUnit.Framework
{
[System.Runtime.CompilerServices.NullableContext(1)]
[System.Runtime.CompilerServices.Nullable(0)]
[AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
public class CultureAttribute : IncludeExcludeAttribute, IApplyToTest
{
private readonly CultureDetector _cultureDetector = new CultureDetector();
private readonly CultureInfo _currentCulture = CultureInfo.CurrentCulture;
public CultureAttribute()
{
}
[System.Runtime.CompilerServices.NullableContext(2)]
public CultureAttribute(string cultures)
: base(cultures)
{
}
public void ApplyToTest(Test test)
{
if (test.RunState != 0 && !IsCultureSupported(out string reason)) {
test.RunState = RunState.Skipped;
base.Reason = reason;
test.Properties.Set("_SKIPREASON", reason);
}
}
[System.Runtime.CompilerServices.NullableContext(2)]
private bool IsCultureSupported([NotNullWhen(false)] out string reason)
{
if (base.Include != null && !_cultureDetector.IsCultureSupported(base.Include)) {
reason = "Only supported under culture " + base.Include;
return false;
}
if (base.Exclude != null && _cultureDetector.IsCultureSupported(base.Exclude)) {
reason = "Not supported under culture " + base.Exclude;
return false;
}
reason = null;
return true;
}
public bool IsCultureSupported(string culture)
{
culture = culture.Trim();
if (culture.IndexOf(',') >= 0) {
if (IsCultureSupported(culture.Split(',', StringSplitOptions.None)))
return true;
} else if (_currentCulture.Name == culture || _currentCulture.TwoLetterISOLanguageName == culture) {
return true;
}
return false;
}
public bool IsCultureSupported(string[] cultures)
{
foreach (string culture in cultures) {
if (IsCultureSupported(culture))
return true;
}
return false;
}
}
}