Няма описание
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RequirePlatformSupportAttribute.cs 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using System;
  2. using System.Linq;
  3. using NUnit.Framework;
  4. using NUnit.Framework.Interfaces;
  5. using NUnit.Framework.Internal;
  6. namespace UnityEditor.TestTools
  7. {
  8. /// <summary>
  9. /// The `RequirePlatformSupportAttribute` attribute can be applied to test assemblies (will affect every test in the assembly), fixtures (will
  10. /// affect every test in the fixture), or to individual test methods.
  11. /// </summary>
  12. [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Method)]
  13. public class RequirePlatformSupportAttribute : NUnitAttribute, IApplyToTest
  14. {
  15. /// <summary>
  16. /// Initializes and returns an instance of RequirePlatformSupportAttribute.
  17. /// </summary>
  18. /// <param name="platforms">The <see cref="BuildTarget"/> platform to run the test on.</param>
  19. public RequirePlatformSupportAttribute(params BuildTarget[] platforms)
  20. {
  21. this.platforms = platforms;
  22. }
  23. /// <summary>
  24. /// The build target platform, see [BuildTarget](https://docs.unity3d.com/ScriptReference/BuildTarget.html).
  25. /// </summary>
  26. /// <returns>The <see cref="BuildTarget"/> platform to run the test on.</returns>
  27. public BuildTarget[] platforms { get; private set; }
  28. /// <summary>
  29. /// Modifies a test as defined for the specific attribute.
  30. /// </summary>
  31. /// <param name="test">The test to modify</param>
  32. void IApplyToTest.ApplyToTest(Test test)
  33. {
  34. test.Properties.Add(PropertyNames.Category, string.Format("RequirePlatformSupport({0})", string.Join(", ", platforms.Select(p => p.ToString()).OrderBy(p => p).ToArray())));
  35. if (!platforms.All(p => BuildPipeline.IsBuildTargetSupported(BuildTargetGroup.Unknown, p)))
  36. {
  37. var missingPlatforms = platforms.Where(p => !BuildPipeline.IsBuildTargetSupported(BuildTargetGroup.Unknown, p)).Select(p => p.ToString()).ToArray();
  38. string skipReason = "Test cannot be run as it requires support for the following platforms to be installed: " + string.Join(", ", missingPlatforms);
  39. test.RunState = RunState.Skipped;
  40. test.Properties.Add(PropertyNames.SkipReason, skipReason);
  41. }
  42. }
  43. }
  44. }