using System;
using System.Collections.Generic;
using NUnit.Framework;
using NUnit.Framework.Interfaces;
using NUnit.Framework.Internal;
namespace UnityEngine.TestTools
{
///
/// This attribute is an alternative to the standard `Ignore` attribute in [NUnit](https://nunit.org/). It allows for ignoring tests only under a specified condition. The condition evaluates during `OnLoad`, referenced by ID.
///
public class ConditionalIgnoreAttribute : NUnitAttribute, IApplyToTest
{
private string m_ConditionKey;
private string m_IgnoreReason;
///
/// Initializes a new instance of the class with a condition key.
///
/// The key to check for enabling the conditional ignore. The condition is set with the static method.
/// The reason for the ignore.
public ConditionalIgnoreAttribute(string conditionKey, string ignoreReason)
{
m_ConditionKey = conditionKey;
m_IgnoreReason = ignoreReason;
}
///
/// Modifies a test as defined for the specific attribute.
///
/// The test to modify
public void ApplyToTest(Test test)
{
var key = m_ConditionKey.ToLowerInvariant();
if (m_ConditionMap.ContainsKey(key) && m_ConditionMap[key])
{
test.RunState = RunState.Ignored;
string skipReason = string.Format(m_IgnoreReason);
test.Properties.Add(PropertyNames.SkipReason, skipReason);
}
}
private static Dictionary m_ConditionMap = new Dictionary();
///
/// Adds a flag indicating whether tests with the same key should be ignored.
///
/// The key to ignore tests for.
/// A boolean value indicating whether the tests should be ignored.
///
/// An example in which tests are ignored in the Mac editor only.
///
/// using UnityEditor;
/// using NUnit.Framework;
/// using UnityEngine.TestTools;
///
/// [InitializeOnLoad]
/// public class OnLoad
/// {
/// static OnLoad()
/// {
/// var editorIsOSX = false;
/// #if UNITY_EDITOR_OSX
/// editorIsOSX = true;
/// #endif
///
/// ConditionalIgnoreAttribute.AddConditionalIgnoreMapping("IgnoreInMacEditor", editorIsOSX);
/// }
/// }
///
/// public class MyTestClass
/// {
/// [Test, ConditionalIgnore("IgnoreInMacEditor", "Ignored on Mac editor.")]
/// public void TestNeverRunningInMacEditor()
/// {
/// Assert.Pass();
/// }
/// }
///
///
public static void AddConditionalIgnoreMapping(string key, bool value)
{
m_ConditionMap.Add(key.ToLowerInvariant(), value);
}
}
}