OSPlatformTranslator
Marks an assembly, test fixture or test method as applying to a specific platform.
using NUnit.Framework.Interfaces;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.CompilerServices;
namespace NUnit.Framework
{
[System.Runtime.CompilerServices.NullableContext(1)]
[System.Runtime.CompilerServices.Nullable(0)]
internal static class OSPlatformTranslator
{
[System.Runtime.CompilerServices.Nullable(2)]
private static readonly Type OsPlatformAttributeType = Type.GetType("System.Runtime.Versioning.OSPlatformAttribute, System.Runtime", false);
[System.Runtime.CompilerServices.Nullable(2)]
private static readonly PropertyInfo PlatformNameProperty = OsPlatformAttributeType?.GetProperty("PlatformName", typeof(string));
private static readonly int[] KnownWindowsVersions = new int[4] {
7,
8,
10,
11
};
private static readonly char[] Digits = new char[10] {
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9'
};
public static IEnumerable<IApplyToTest> RetrieveAndTranslate(this ICustomAttributeProvider provider)
{
IApplyToTest[] attributes = provider.GetAttributes<IApplyToTest>(true);
Attribute[] osPlatformAttributes = ((object)OsPlatformAttributeType == null) ? Array.Empty<Attribute>() : ((Attribute[])provider.GetCustomAttributes(OsPlatformAttributeType, true));
return Translate(osPlatformAttributes, attributes);
}
internal static IEnumerable<IApplyToTest> Translate(Attribute[] osPlatformAttributes, IEnumerable<IApplyToTest> applyToTestAttributes)
{
HashSet<string> includes = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
HashSet<string> excludes = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
foreach (IApplyToTest applyToTestAttribute in applyToTestAttributes) {
PlatformAttribute platformAttribute = applyToTestAttribute as PlatformAttribute;
if (platformAttribute != null) {
<Translate>g__Add|4_0(includes, platformAttribute.Include);
<Translate>g__Add|4_0(excludes, platformAttribute.Exclude);
} else
yield return applyToTestAttribute;
}
foreach (Attribute attribute in osPlatformAttributes) {
string text = (string)PlatformNameProperty?.GetValue(attribute);
if (text != null) {
IEnumerable<string> other = Translate(text);
Type type = attribute.GetType();
if (type.FullName == "System.Runtime.Versioning.SupportedOSPlatformAttribute")
includes.UnionWith(other);
else if (type.FullName == "System.Runtime.Versioning.UnsupportedOSPlatformAttribute") {
excludes.UnionWith(other);
}
}
}
if (includes.Count > 0 || excludes.Count > 0)
yield return (IApplyToTest)new PlatformAttribute {
Include = ((includes.Count == 0) ? null : string.Join(",", includes)),
Exclude = ((excludes.Count == 0) ? null : string.Join(",", excludes))
};
}
internal static IEnumerable<string> Translate(string platformName)
{
ParseOSAndVersion(platformName, out string os, out int majorVersion);
return Translate(os, majorVersion);
}
private static void ParseOSAndVersion(string plaformName, out string os, out int majorVersion)
{
int num = plaformName.IndexOfAny(Digits);
if (num > 0) {
os = plaformName.Substring(0, num);
int num2 = plaformName.IndexOf('.', num);
int num3 = (num2 > num) ? num2 : plaformName.Length;
int length = num3 - num;
majorVersion = int.Parse(plaformName.Substring(num, length));
} else {
os = plaformName;
majorVersion = 0;
}
}
private static IEnumerable<string> Translate(string osName, int majorVersion)
{
switch (osName.ToUpperInvariant()) {
case "WINDOWS":
if (majorVersion < 7)
yield return "Win";
else {
int[] knownWindowsVersions = KnownWindowsVersions;
for (int i = 0; i < knownWindowsVersions.Length; i++) {
int num = knownWindowsVersions[i];
if (num >= majorVersion)
yield return "Windows" + num.ToString();
}
if (majorVersion <= 10)
yield return "WindowsServer10";
}
break;
case "OSX":
case "MACOS":
yield return "MacOsX";
break;
case "LINUX":
yield return "Linux";
break;
default:
yield return osName;
break;
}
}
}
}