PlatformFactAttribute.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #nullable enable
  2. using System;
  3. using System.Runtime.InteropServices;
  4. using Xunit;
  5. namespace Avalonia
  6. {
  7. [Flags]
  8. internal enum TestPlatforms
  9. {
  10. Windows = 0x01,
  11. MacOS = 0x02,
  12. Linux = 0x04,
  13. All = Windows | MacOS | Linux,
  14. }
  15. internal class PlatformFactAttribute : FactAttribute
  16. {
  17. private readonly string? _reason;
  18. private string? _skip;
  19. public PlatformFactAttribute(TestPlatforms platforms, string? reason = null)
  20. {
  21. _reason = reason;
  22. Platforms = platforms;
  23. }
  24. public TestPlatforms Platforms { get; }
  25. public override string? Skip
  26. {
  27. get
  28. {
  29. if (_skip is not null)
  30. return _skip;
  31. if (!IsSupported())
  32. return $"Ignored on {RuntimeInformation.OSDescription}" +
  33. (_reason is not null ? $" reason: '{_reason}'" : "");
  34. return null;
  35. }
  36. set => _skip = value;
  37. }
  38. private bool IsSupported()
  39. {
  40. if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
  41. return Platforms.HasFlag(TestPlatforms.Windows);
  42. if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
  43. return Platforms.HasFlag(TestPlatforms.MacOS);
  44. if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
  45. return Platforms.HasFlag(TestPlatforms.Linux);
  46. return false;
  47. }
  48. }
  49. }