Skip to content

Commit

Permalink
[tests] Fix ignoring members based on OSPlatformAttributes.
Browse files Browse the repository at this point in the history
We were ignorning members based on whether a member had
ObsoletedOSPlatformAttribute/UnavailableOSPlatformAttribute, not taking into
account that the attribute might not apply to the current platform.

So implement logic to only take into account
[Obsoleted|Unavailable]OSPlatformAttributes for the current platform.
  • Loading branch information
rolfbjarne committed Sep 4, 2024
1 parent 74166b0 commit cdeb6af
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
54 changes: 54 additions & 0 deletions tests/common/TestRuntime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using System.Runtime.InteropServices;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.Versioning;

using AVFoundation;
using CoreBluetooth;
Expand Down Expand Up @@ -116,6 +117,59 @@ public static Version OSXVersion {
}
#endif

public static ApplePlatform CurrentPlatform {
get {
#if __MACCATALYST__
return ApplePlatform.MacCatalyst;
#elif __IOS__
return ApplePlatform.iOS;
#elif __TVOS__
return ApplePlatform.TVOS;
#elif __MACOS__
return ApplePlatform.MacOSX;
#elif __WATCHOS__
return ApplePlatform.WatchOS;
#else
#error Unknown platform
#endif
}
}

// This returns the string for the platform as used in the OSPlatformAttribute's PlatformName property.
public static string GetOSPlatformName (ApplePlatform platform)
{
switch (platform) {
case ApplePlatform.iOS:
return "ios";
case ApplePlatform.MacOSX:
return "macos";
case ApplePlatform.TVOS:
return "tvos";
case ApplePlatform.MacCatalyst:
return "maccatalyst";
default:
throw new Exception ($"Unknown platform: {platform}");
}
}

public static bool HasOSPlatformAttributeForCurrentPlatform<T> (ICustomAttributeProvider provider) where T: OSPlatformAttribute
{
return HasOSPlatformAttribute<T> (provider, CurrentPlatform);
}

public static bool HasOSPlatformAttribute<T> (ICustomAttributeProvider provider, ApplePlatform platform) where T: OSPlatformAttribute
{
var attribs = provider.GetCustomAttributes (false);
var platformName = GetOSPlatformName (platform);
foreach (var attrib in attribs) {
if (attrib is T platformAttribute) {
if (platformAttribute.PlatformName.StartsWith (platformName, StringComparison.OrdinalIgnoreCase))
return true;
}
}
return false;
}

public static Version GetSDKVersion ()
{
var v = dyld_get_program_sdk_version ();
Expand Down
11 changes: 10 additions & 1 deletion tests/introspection/ApiBaseTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,16 @@ protected bool SkipDueToRejectedTypes (Type type)
public bool MemberHasObsolete (MemberInfo member)
{
#if NET
return member.GetCustomAttributes<ObsoletedOSPlatformAttribute> (false).Any ();
return TestRuntime.HasOSPlatformAttributeForCurrentPlatform<ObsoletedOSPlatformAttribute> (member);
#else
return member.GetCustomAttribute<ObsoleteAttribute> () is not null;
#endif
}

public bool MemberHasUnsupported (MemberInfo member)
{
#if NET
return TestRuntime.HasOSPlatformAttributeForCurrentPlatform<UnsupportedOSPlatformAttribute> (member);
#else
return member.GetCustomAttribute<ObsoleteAttribute> () is not null;
#endif
Expand Down
8 changes: 7 additions & 1 deletion tests/introspection/ApiSignatureTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Reflection;
using System.Text;
using NUnit.Framework;
Expand Down Expand Up @@ -864,10 +865,15 @@ static bool IsDiscouraged (MemberInfo mi)
case "AdviceAttribute":
case "ObsoletedAttribute":
case "DeprecatedAttribute":
case "UnsupportedOSPlatformAttribute":
return true;
}
}

#if NET
if (TestRuntime.HasOSPlatformAttributeForCurrentPlatform<UnsupportedOSPlatformAttribute> (mi))
return true;
#endif

return false;
}

Expand Down

0 comments on commit cdeb6af

Please sign in to comment.