using System;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using NUnit.Framework.Interfaces;
using NUnit.Framework.Internal;
namespace UnityEngine.TestTools
{
///
/// Use this attribute to define a specific set of platforms you want or do not want your test(s) to run on.
///
/// You can use this attribute on the test method, test class, or test assembly level. Use the supported enumeration values to specify the platforms. You can also specify which platforms to test by passing one or more `RuntimePlatform` values along with or without the include or exclude properties as parameters to the [Platform](https://github.com/nunit/docs/wiki/Platform-Attribute) attribute constructor.
///
/// The test(s) skips if the current target platform is:
/// - Not explicitly specified in the included platforms list
/// - In the excluded platforms list
///
[AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true)]
public class UnityPlatformAttribute : NUnitAttribute, IApplyToTest
{
///
/// A subset of platforms you need to have your tests run on.
///
public RuntimePlatform[] include { get; set; }
///
/// List the platforms you do not want to have your tests run on.
///
public RuntimePlatform[] exclude { get; set; }
private string m_skippedReason;
///
/// Constructs a new instance of the class.
///
public UnityPlatformAttribute()
{
include = new List().ToArray();
exclude = new List().ToArray();
}
///
/// Constructs a new instance of the class with a list of platforms to include.
///
/// The different to run the test on.
public UnityPlatformAttribute(params RuntimePlatform[] include)
: this()
{
this.include = include;
}
///
/// Modifies a test as defined for the specific attribute.
///
/// The test to modify
public void ApplyToTest(Test test)
{
if (test.RunState == RunState.NotRunnable || test.RunState == RunState.Ignored || IsPlatformSupported(Application.platform))
{
return;
}
test.RunState = RunState.Skipped;
test.Properties.Add(PropertyNames.SkipReason, m_skippedReason);
}
internal bool IsPlatformSupported(RuntimePlatform testTargetPlatform)
{
if (include.Any() && !include.Any(x => x == testTargetPlatform))
{
m_skippedReason = string.Format("Only supported on {0}", string.Join(", ", include.Select(x => x.ToString()).ToArray()));
return false;
}
if (exclude.Any(x => x == testTargetPlatform))
{
m_skippedReason = string.Format("Not supported on {0}", string.Join(", ", exclude.Select(x => x.ToString()).ToArray()));
return false;
}
return true;
}
}
}