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