暫無描述
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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. public BuildTarget[] platforms { get; private set; }
  27. /// <summary>
  28. /// Modifies a test as defined for the specific attribute.
  29. /// </summary>
  30. /// <param name="test">The test to modify</param>
  31. void IApplyToTest.ApplyToTest(Test test)
  32. {
  33. test.Properties.Add(PropertyNames.Category, string.Format("RequirePlatformSupport({0})", string.Join(", ", platforms.Select(p => p.ToString()).OrderBy(p => p).ToArray())));
  34. if (!platforms.All(p => BuildPipeline.IsBuildTargetSupported(BuildTargetGroup.Unknown, p)))
  35. {
  36. var missingPlatforms = platforms.Where(p => !BuildPipeline.IsBuildTargetSupported(BuildTargetGroup.Unknown, p)).Select(p => p.ToString()).ToArray();
  37. string skipReason = "Test cannot be run as it requires support for the following platforms to be installed: " + string.Join(", ", missingPlatforms);
  38. test.RunState = RunState.Skipped;
  39. test.Properties.Add(PropertyNames.SkipReason, skipReason);
  40. }
  41. }
  42. }
  43. }