Unity-jump-proj
This commit is contained in:
@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 51557afa652635743b264a309f0a5c60
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@ -0,0 +1,12 @@
|
||||
using System.Reflection;
|
||||
|
||||
namespace UnityEngine.TestTools.Utils
|
||||
{
|
||||
internal class AssemblyLoadProxy : IAssemblyLoadProxy
|
||||
{
|
||||
public IAssemblyWrapper Load(string assemblyString)
|
||||
{
|
||||
return new AssemblyWrapper(Assembly.Load(assemblyString));
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: fb593906b7b6d824087dcaebf6c082e0
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@ -0,0 +1,33 @@
|
||||
using System;
|
||||
using System.Reflection;
|
||||
|
||||
namespace UnityEngine.TestTools.Utils
|
||||
{
|
||||
internal class AssemblyWrapper : IAssemblyWrapper
|
||||
{
|
||||
public AssemblyWrapper(Assembly assembly)
|
||||
{
|
||||
Assembly = assembly;
|
||||
Name = assembly.GetName();
|
||||
}
|
||||
|
||||
public Assembly Assembly { get; }
|
||||
|
||||
public AssemblyName Name { get; }
|
||||
|
||||
public virtual string Location
|
||||
{
|
||||
get
|
||||
{
|
||||
//Some platforms dont support this
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
public virtual AssemblyName[] GetReferencedAssemblies()
|
||||
{
|
||||
//Some platforms dont support this
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2e3b9bbf2c1a3cd4f88883ca32882ec6
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@ -0,0 +1,7 @@
|
||||
namespace UnityEngine.TestTools.Utils
|
||||
{
|
||||
internal interface IAssemblyLoadProxy
|
||||
{
|
||||
IAssemblyWrapper Load(string assemblyString);
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 12dfd4bdbb5c8e6419432fbc54ef25d9
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@ -0,0 +1,12 @@
|
||||
using System.Reflection;
|
||||
|
||||
namespace UnityEngine.TestTools.Utils
|
||||
{
|
||||
internal interface IAssemblyWrapper
|
||||
{
|
||||
Assembly Assembly { get; }
|
||||
AssemblyName Name { get; }
|
||||
string Location { get; }
|
||||
AssemblyName[] GetReferencedAssemblies();
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1c5afe945b715e149a70113a4be7b32a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@ -0,0 +1,7 @@
|
||||
namespace UnityEngine.TestTools.Utils
|
||||
{
|
||||
internal interface IScriptingRuntimeProxy
|
||||
{
|
||||
string[] GetAllUserAssemblies();
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: fe4aef60e4ace544c8430da8ef8acba2
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@ -0,0 +1,54 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using NUnit.Framework.Interfaces;
|
||||
using UnityEngine.TestTools.NUnitExtensions;
|
||||
|
||||
namespace UnityEngine.TestTools.Utils
|
||||
{
|
||||
internal class PlayerTestAssemblyProvider
|
||||
{
|
||||
private IAssemblyLoadProxy m_AssemblyLoadProxy;
|
||||
private readonly List<string> m_AssembliesToLoad;
|
||||
|
||||
//Cached until domain reload
|
||||
private static List<IAssemblyWrapper> m_LoadedAssemblies;
|
||||
|
||||
internal PlayerTestAssemblyProvider(IAssemblyLoadProxy assemblyLoadProxy, List<string> assembliesToLoad)
|
||||
{
|
||||
m_AssemblyLoadProxy = assemblyLoadProxy;
|
||||
m_AssembliesToLoad = assembliesToLoad;
|
||||
LoadAssemblies();
|
||||
}
|
||||
|
||||
public List<IAssemblyWrapper> GetUserAssemblies()
|
||||
{
|
||||
return m_LoadedAssemblies;
|
||||
}
|
||||
|
||||
private void LoadAssemblies()
|
||||
{
|
||||
if (m_LoadedAssemblies != null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
m_LoadedAssemblies = new List<IAssemblyWrapper>();
|
||||
|
||||
foreach (var userAssembly in m_AssembliesToLoad)
|
||||
{
|
||||
IAssemblyWrapper a;
|
||||
try
|
||||
{
|
||||
a = m_AssemblyLoadProxy.Load(userAssembly);
|
||||
}
|
||||
catch (FileNotFoundException)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (a != null)
|
||||
m_LoadedAssemblies.Add(a);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 43a3aec217baa9644a7cf34b5f93fed9
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@ -0,0 +1,10 @@
|
||||
namespace UnityEngine.TestTools.Utils
|
||||
{
|
||||
internal class ScriptingRuntimeProxy : IScriptingRuntimeProxy
|
||||
{
|
||||
public string[] GetAllUserAssemblies()
|
||||
{
|
||||
return ScriptingRuntime.GetAllUserAssemblies();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f3a361a6ad1aff14ba8f48976e94ad76
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@ -0,0 +1,45 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
|
||||
namespace UnityEngine.TestTools
|
||||
{
|
||||
internal static class AttributeHelper
|
||||
{
|
||||
internal static Type GetTargetClassFromName(string targetClassName, Type attributeInterface)
|
||||
{
|
||||
Type targetClass = null;
|
||||
foreach (var assemblyName in ScriptingRuntime.GetAllUserAssemblies())
|
||||
{
|
||||
// we need to pass the assembly name without the .dll extension, so removing that first
|
||||
var name = Path.GetFileNameWithoutExtension(assemblyName);
|
||||
targetClass = Type.GetType(targetClassName + "," + name);
|
||||
if (targetClass != null)
|
||||
break;
|
||||
}
|
||||
|
||||
if (targetClass == null)
|
||||
{
|
||||
Debug.LogWarningFormat("Class type not found: " + targetClassName);
|
||||
return null;
|
||||
}
|
||||
|
||||
ValidateTargetClass(targetClass, attributeInterface);
|
||||
return targetClass;
|
||||
}
|
||||
|
||||
private static void ValidateTargetClass(Type targetClass, Type attributeInterface)
|
||||
{
|
||||
var constructorInfos = targetClass.GetConstructors();
|
||||
if (constructorInfos.All(constructor => constructor.GetParameters().Length != 0))
|
||||
{
|
||||
Debug.LogWarningFormat("{0} does not implement default constructor", targetClass.Name);
|
||||
}
|
||||
|
||||
if (!attributeInterface.IsAssignableFrom(targetClass))
|
||||
{
|
||||
Debug.LogWarningFormat("{0} does not implement {1}", targetClass.Name, attributeInterface.Name);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ae8ce3ffe04ac2c42945fd27e0291fc3
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@ -0,0 +1,80 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace UnityEngine.TestTools.Utils
|
||||
{
|
||||
/// <summary>
|
||||
/// Use this class to compare two Color objects. ColorEqualityComparer.Instance has default calculation error value set to 0.01f. To set a test specific error value instantiate a comparer instance using the one argument constructor.
|
||||
/// </summary>
|
||||
public class ColorEqualityComparer : IEqualityComparer<Color>
|
||||
{
|
||||
private const float k_DefaultError = 0.01f;
|
||||
private readonly float AllowedError;
|
||||
|
||||
|
||||
private static readonly ColorEqualityComparer m_Instance = new ColorEqualityComparer();
|
||||
/// <summary>
|
||||
///A singleton instance of the comparer with a default error value set to 0.01f.
|
||||
/// </summary>
|
||||
public static ColorEqualityComparer Instance { get { return m_Instance; } }
|
||||
|
||||
private ColorEqualityComparer() : this(k_DefaultError)
|
||||
{
|
||||
}
|
||||
/// <summary>
|
||||
/// Creates an instance of the comparer with a custom error value.
|
||||
/// </summary>
|
||||
/// <param name="error">The custom error value.</param>
|
||||
public ColorEqualityComparer(float error)
|
||||
{
|
||||
this.AllowedError = error;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Compares the actual and expected Color objects for equality using <see cref="Utils.AreFloatsEqualAbsoluteError"/> to compare the RGB and Alpha attributes of Color. Returns true if expected and actual objects are equal otherwise, it returns false.
|
||||
/// </summary>
|
||||
/// <param name="expected">The expected Color value used to compare.</param>
|
||||
/// <param name="actual">The actual Color value to test.</param>
|
||||
/// <returns>True if actual and expected are equal, false otherwise</returns>
|
||||
/// <example>
|
||||
///<code>
|
||||
/// [TestFixture]
|
||||
/// public class ColorEqualityTest
|
||||
/// {
|
||||
/// [Test]
|
||||
/// public void GivenColorsAreEqual_WithAllowedCalculationError()
|
||||
/// {
|
||||
/// // Using default error
|
||||
/// var firstColor = new Color(0f, 0f, 0f, 0f);
|
||||
/// var secondColor = new Color(0f, 0f, 0f, 0f);
|
||||
///
|
||||
/// Assert.That(firstColor, Is.EqualTo(secondColor).Using(ColorEqualityComparer.Instance));
|
||||
///
|
||||
/// // Allowed error 10e-5f
|
||||
/// var comparer = new ColorEqualityComparer(10e-5f);
|
||||
/// firstColor = new Color(0f, 0f, 0f, 1f);
|
||||
/// secondColor = new Color(10e-6f, 0f, 0f, 1f);
|
||||
///
|
||||
/// Assert.That(firstColor, Is.EqualTo(secondColor).Using(comparer));
|
||||
/// }
|
||||
/// }
|
||||
/// </code>
|
||||
/// </example>
|
||||
public bool Equals(Color expected, Color actual)
|
||||
{
|
||||
return Utils.AreFloatsEqualAbsoluteError(expected.r, actual.r, AllowedError) &&
|
||||
Utils.AreFloatsEqualAbsoluteError(expected.g, actual.g, AllowedError) &&
|
||||
Utils.AreFloatsEqualAbsoluteError(expected.b, actual.b, AllowedError) &&
|
||||
Utils.AreFloatsEqualAbsoluteError(expected.a, actual.a, AllowedError);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Serves as the default hash function.
|
||||
/// </summary>
|
||||
/// <param name="color">A not null Color object.</param>
|
||||
/// <returns>Returns 0.</returns>
|
||||
public int GetHashCode(Color color)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d6105bc8cf5ce544487daca4cbc62583
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@ -0,0 +1,100 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using NUnit.Framework.Internal;
|
||||
using UnityEngine.TestRunner.NUnitExtensions.Runner;
|
||||
using UnityEngine.TestTools.TestRunner;
|
||||
|
||||
namespace UnityEngine.TestTools.Utils
|
||||
{
|
||||
internal class CoroutineRunner
|
||||
{
|
||||
private bool m_Running;
|
||||
private bool m_Timeout;
|
||||
private readonly MonoBehaviour m_Controller;
|
||||
private readonly UnityTestExecutionContext m_Context;
|
||||
private Coroutine m_TimeOutCoroutine;
|
||||
private IEnumerator m_TestCoroutine;
|
||||
|
||||
internal const int k_DefaultTimeout = 1000 * 180;
|
||||
|
||||
public CoroutineRunner(MonoBehaviour playmodeTestsController, UnityTestExecutionContext context)
|
||||
{
|
||||
m_Controller = playmodeTestsController;
|
||||
m_Context = context;
|
||||
}
|
||||
|
||||
public IEnumerator HandleEnumerableTest(IEnumerator testEnumerator)
|
||||
{
|
||||
if (m_Context.TestCaseTimeout == 0)
|
||||
{
|
||||
m_Context.TestCaseTimeout = k_DefaultTimeout;
|
||||
}
|
||||
do
|
||||
{
|
||||
if (!m_Running)
|
||||
{
|
||||
m_Running = true;
|
||||
m_TestCoroutine = ExMethod(testEnumerator, m_Context.TestCaseTimeout);
|
||||
m_Controller.StartCoroutine(m_TestCoroutine);
|
||||
}
|
||||
if (m_Context.ExecutionStatus == TestExecutionStatus.StopRequested || m_Context.ExecutionStatus == TestExecutionStatus.AbortRequested)
|
||||
{
|
||||
StopAllRunningCoroutines();
|
||||
yield break;
|
||||
}
|
||||
yield return null;
|
||||
}
|
||||
while (m_Running);
|
||||
}
|
||||
|
||||
private void StopAllRunningCoroutines()
|
||||
{
|
||||
if (m_TimeOutCoroutine != null)
|
||||
{
|
||||
m_Controller.StopCoroutine(m_TimeOutCoroutine);
|
||||
}
|
||||
|
||||
if (m_TestCoroutine != null)
|
||||
{
|
||||
m_Controller.StopCoroutine(m_TestCoroutine);
|
||||
}
|
||||
}
|
||||
|
||||
private IEnumerator ExMethod(IEnumerator e, int timeout)
|
||||
{
|
||||
m_TimeOutCoroutine = m_Controller.StartCoroutine(StartTimer(e, timeout,
|
||||
() =>
|
||||
{
|
||||
m_Timeout = true;
|
||||
m_Running = false;
|
||||
}));
|
||||
|
||||
yield return m_Controller.StartCoroutine(e);
|
||||
m_Controller.StopCoroutine(m_TimeOutCoroutine);
|
||||
m_Running = false;
|
||||
}
|
||||
|
||||
private IEnumerator StartTimer(IEnumerator coroutineToBeKilled, int timeout, Action onTimeout)
|
||||
{
|
||||
yield return new WaitForSecondsRealtime(timeout / 1000f);
|
||||
if (coroutineToBeKilled != null)
|
||||
m_Controller.StopCoroutine(coroutineToBeKilled);
|
||||
if (onTimeout != null)
|
||||
{
|
||||
onTimeout();
|
||||
StopAllRunningCoroutines();
|
||||
m_Context.CurrentResult.RecordException(new UnityTestTimeoutException(m_Context.TestCaseTimeout));
|
||||
}
|
||||
}
|
||||
|
||||
public bool HasFailedWithTimeout()
|
||||
{
|
||||
return m_Timeout;
|
||||
}
|
||||
|
||||
public int GetDefaultTimeout()
|
||||
{
|
||||
return k_DefaultTimeout;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 24a158219395ebf44a60547b97784ddc
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@ -0,0 +1,74 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace UnityEngine.TestTools.Utils
|
||||
{
|
||||
/// <summary>
|
||||
/// Use this class to compare two float values for equality with NUnit constraints. Use FloatEqualityComparer.Instance comparer to have the default error value set to 0.0001f. For any other error, use the one argument constructor to create a comparer.
|
||||
/// </summary>
|
||||
public class FloatEqualityComparer : IEqualityComparer<float>
|
||||
{
|
||||
private const float k_DefaultError = 0.0001f;
|
||||
private readonly float AllowedError;
|
||||
|
||||
private static readonly FloatEqualityComparer m_Instance = new FloatEqualityComparer();
|
||||
/// <summary>
|
||||
///A singleton instance of the comparer with a default error value set to 0.0001f.
|
||||
/// </summary>
|
||||
public static FloatEqualityComparer Instance { get { return m_Instance; } }
|
||||
|
||||
private FloatEqualityComparer() : this(k_DefaultError) {}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes an instance of a FloatEqualityComparer with a custom error value instead of the default 0.0001f.
|
||||
/// </summary>
|
||||
/// <param name="allowedError">The custom error value</param>
|
||||
public FloatEqualityComparer(float allowedError)
|
||||
{
|
||||
this.AllowedError = allowedError;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Compares the actual and expected float values for equality using <see cref="Utils.AreFloatsEqual"/>.
|
||||
/// </summary>
|
||||
/// <param name="expected">The expected float value used to compare.</param>
|
||||
/// <param name="actual">The actual float value to test.</param>
|
||||
/// <returns>True if the values are equals, false otherwise.</returns>
|
||||
/// <example>
|
||||
/// <code>
|
||||
/// [TestFixture]
|
||||
/// public class FloatsTest
|
||||
///{
|
||||
/// [Test]
|
||||
/// public void VerifyThat_TwoFloatsAreEqual()
|
||||
/// {
|
||||
/// var comparer = new FloatEqualityComparer(10e-6f);
|
||||
/// var actual = -0.00009f;
|
||||
/// var expected = 0.00009f;
|
||||
///
|
||||
/// Assert.That(actual, Is.EqualTo(expected).Using(comparer));
|
||||
///
|
||||
/// // Default relative error 0.0001f
|
||||
/// actual = 10e-8f;
|
||||
/// expected = 0f;
|
||||
///
|
||||
/// Assert.That(actual, Is.EqualTo(expected).Using(FloatEqualityComparer.Instance));
|
||||
/// }
|
||||
///}
|
||||
/// </code>
|
||||
/// </example>
|
||||
public bool Equals(float expected, float actual)
|
||||
{
|
||||
return Utils.AreFloatsEqual(expected, actual, AllowedError);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Serves as the default hash function.
|
||||
/// </summary>
|
||||
/// <param name="value">A not null float number.</param>
|
||||
/// <returns>Returns 0.</returns>
|
||||
public int GetHashCode(float value)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: af5042802f06c804c8abddd544b77a4a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@ -0,0 +1,21 @@
|
||||
using System.Collections;
|
||||
using NUnit.Framework.Interfaces;
|
||||
|
||||
namespace UnityEngine.TestTools
|
||||
{
|
||||
/// <summary>
|
||||
/// When implemented by an attribute, this interface implemented to provide actions to execute before setup and after teardown of tests.
|
||||
/// </summary>
|
||||
public interface IOuterUnityTestAction
|
||||
{
|
||||
/// <summary>Executed before each test is run</summary>
|
||||
/// <param name="test">The test that is going to be run.</param>
|
||||
/// <returns>Enumerable collection of actions to perform before test setup.</returns>
|
||||
IEnumerator BeforeTest(ITest test);
|
||||
|
||||
/// <summary>Executed after each test is run</summary>
|
||||
/// <param name="test">The test that has just been run.</param>
|
||||
/// <returns>Enumerable collection of actions to perform after test teardown.</returns>
|
||||
IEnumerator AfterTest(ITest test);
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b9c2a6302985d3846b7b9f6fd9e2da9a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@ -0,0 +1,13 @@
|
||||
namespace UnityEngine.TestTools
|
||||
{
|
||||
/// <summary>
|
||||
/// Implement this interface if you want to define a set of actions to execute as a post-build step. Cleanup runs right away for a standalone test run, but only after all the tests run within the Editor.
|
||||
/// </summary>
|
||||
public interface IPostBuildCleanup
|
||||
{
|
||||
/// <summary>
|
||||
/// Implement this method to specify actions that should run as a post-build cleanup step.
|
||||
/// </summary>
|
||||
void Cleanup();
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ff67c526455160f4690a44f74dee4cbe
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@ -0,0 +1,59 @@
|
||||
namespace UnityEngine.TestTools
|
||||
{
|
||||
/// <summary>
|
||||
/// Implement this interface if you want to define a set of actions to run as a pre-build step.
|
||||
/// </summary>
|
||||
public interface IPrebuildSetup
|
||||
{
|
||||
/// <summary>
|
||||
/// Implement this method to call actions automatically before the build process.
|
||||
/// </summary>
|
||||
/// <example>
|
||||
/// <code>
|
||||
/// [TestFixture]
|
||||
/// public class CreateSpriteTest : IPrebuildSetup
|
||||
/// {
|
||||
/// Texture2D m_Texture;
|
||||
/// Sprite m_Sprite;
|
||||
///
|
||||
/// public void Setup()
|
||||
/// {
|
||||
/// #if UNITY_EDITOR
|
||||
/// var spritePath = "Assets/Resources/Circle.png";
|
||||
///
|
||||
/// var ti = UnityEditor.AssetImporter.GetAtPath(spritePath) as UnityEditor.TextureImporter;
|
||||
///
|
||||
/// ti.textureCompression = UnityEditor.TextureImporterCompression.Uncompressed;
|
||||
///
|
||||
/// ti.SaveAndReimport();
|
||||
/// #endif
|
||||
/// }
|
||||
///
|
||||
/// [SetUp]
|
||||
/// public void SetUpTest()
|
||||
/// {
|
||||
/// m_Texture = Resources.Load<Texture2D>("Circle");
|
||||
/// }
|
||||
///
|
||||
/// [Test]
|
||||
/// public void WhenNullTextureIsPassed_CreateShouldReturnNullSprite()
|
||||
/// {
|
||||
/// // Check with Valid Texture.
|
||||
///
|
||||
/// LogAssert.Expect(LogType.Log, "Circle Sprite Created");
|
||||
///
|
||||
/// Sprite.Create(m_Texture, new Rect(0, 0, m_Texture.width, m_Texture.height), new Vector2(0.5f, 0.5f));
|
||||
///
|
||||
/// Debug.Log("Circle Sprite Created");
|
||||
///
|
||||
/// // Check with NULL Texture. Should return NULL Sprite.
|
||||
/// m_Sprite = Sprite.Create(null, new Rect(0, 0, m_Texture.width, m_Texture.height), new Vector2(0.5f, 0.5f));
|
||||
///
|
||||
/// Assert.That(m_Sprite, Is.Null, "Sprite created with null texture should be null");
|
||||
/// }
|
||||
/// }
|
||||
/// </code>
|
||||
/// </example>
|
||||
void Setup();
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: acc16f0c684508f44813662a300c574b
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@ -0,0 +1,31 @@
|
||||
using NUnit.Framework.Interfaces;
|
||||
|
||||
namespace UnityEngine.TestRunner
|
||||
{
|
||||
/// <summary>
|
||||
/// Interface for getting callsbacks on test progress directly from NUnit. This is available both in the editor and directly in the runtime. It is registered by using <see cref="TestRunCallbackAttribute"/>.
|
||||
/// </summary>
|
||||
public interface ITestRunCallback
|
||||
{
|
||||
/// <summary>
|
||||
/// A callback invoked when a test run is started.
|
||||
/// </summary>
|
||||
/// <param name="testsToRun">The full loaded test tree.</param>
|
||||
void RunStarted(ITest testsToRun);
|
||||
/// <summary>
|
||||
/// A callback invoked when a test run is finished.
|
||||
/// </summary>
|
||||
/// <param name="testResults">The result of the test run.</param>
|
||||
void RunFinished(ITestResult testResults);
|
||||
/// <summary>
|
||||
/// A callback invoked when each individual node of the test tree has started executing.
|
||||
/// </summary>
|
||||
/// <param name="test">The test node currently executed.</param>
|
||||
void TestStarted(ITest test);
|
||||
/// <summary>
|
||||
/// A callback invoked when each individual node of the test tree has finished executing.
|
||||
/// </summary>
|
||||
/// <param name="result">The result of the test tree node after it had been executed.</param>
|
||||
void TestFinished(ITestResult result);
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 38d0b8a87b967304da08a2ae9b955066
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ce8da628f68c7594b8b9a597fa52db7b
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@ -0,0 +1,11 @@
|
||||
namespace UnityEngine.TestTools
|
||||
{
|
||||
/// <summary>
|
||||
/// An interface implemented by a MonoBehaviour test.
|
||||
/// </summary>
|
||||
public interface IMonoBehaviourTest
|
||||
{
|
||||
/// <summary>True when the test is considered finished.</summary>
|
||||
bool IsTestFinished {get; }
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a002d3737b873954395b7cf862873ab8
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@ -0,0 +1,60 @@
|
||||
namespace UnityEngine.TestTools
|
||||
{
|
||||
/// <summary>
|
||||
/// This is a wrapper that allows running tests on MonoBehaviour scripts. Inherits from <see cref="CustomYieldInstruction"/>.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">A MonoBehaviour component created for the test and attached to the tests [GameObject](https://docs.unity3d.com/ScriptReference/GameObject.html).</typeparam>
|
||||
public class MonoBehaviourTest<T> : CustomYieldInstruction where T : MonoBehaviour, IMonoBehaviourTest
|
||||
{
|
||||
/// <summary>A MonoBehaviour component created for the test and attached to the tests [GameObject](https://docs.unity3d.com/ScriptReference/GameObject.html).</summary>
|
||||
public T component { get; }
|
||||
/// <summary>
|
||||
/// A `GameObject` created as a container for the test component.
|
||||
/// </summary>
|
||||
public GameObject gameObject { get { return component.gameObject; } }
|
||||
/// <summary>
|
||||
/// `MonoBehaviourTest` is a [coroutine](https://docs.unity3d.com/ScriptReference/Coroutine.html) and a helper for writing MonoBehaviour tests.
|
||||
/// Yield a `MonoBehaviour`Test when using the `UnityTest` attribute to instantiate the `MonoBehaviour` you wish to test and wait for it to finish running. Implement the `IMonoBehaviourTest` interface on the `MonoBehaviour` to state when the test completes.
|
||||
/// </summary>
|
||||
/// <param name="dontDestroyOnLoad"></param>
|
||||
/// <example>
|
||||
/// <code>
|
||||
/// [UnityTest]
|
||||
/// public IEnumerator MonoBehaviourTest_Works()
|
||||
/// {
|
||||
/// yield return new MonoBehaviourTest<MyMonoBehaviourTest>();
|
||||
/// }
|
||||
///
|
||||
/// public class MyMonoBehaviourTest : MonoBehaviour, IMonoBehaviourTest
|
||||
/// {
|
||||
/// private int frameCount;
|
||||
/// public bool IsTestFinished
|
||||
/// {
|
||||
/// get { return frameCount > 10; }
|
||||
/// }
|
||||
///
|
||||
/// void Update()
|
||||
/// {
|
||||
/// frameCount++;
|
||||
/// }
|
||||
/// }
|
||||
/// </code>
|
||||
/// </example>
|
||||
public MonoBehaviourTest(bool dontDestroyOnLoad = true)
|
||||
{
|
||||
var go = new GameObject("MonoBehaviourTest: " + typeof(T).FullName);
|
||||
component = go.AddComponent<T>();
|
||||
if (dontDestroyOnLoad)
|
||||
{
|
||||
Object.DontDestroyOnLoad(go);
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// (Inherited) Returns `true`` if the test is not finished yet, which keeps the coroutine suspended
|
||||
/// </summary>
|
||||
public override bool keepWaiting
|
||||
{
|
||||
get { return !component.IsTestFinished; }
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 164c9b1458eaab743a4b45c37a4d720d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@ -0,0 +1,31 @@
|
||||
using System;
|
||||
|
||||
namespace UnityEngine.TestTools
|
||||
{
|
||||
/// <summary>
|
||||
/// PostBuildCleanup attributes run if the respective test or test class is in the current test run. The test is included either by running all tests or setting a [filter](https://docs.unity3d.com/Packages/com.unity.test-framework@1.1/manual/workflow-create-test.html#filters) that includes the test. If multiple tests reference the same pre-built setup or post-build cleanup, then it only runs once.
|
||||
/// </summary>
|
||||
[AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Method)]
|
||||
public class PostBuildCleanupAttribute : Attribute
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes and returns an instance of PostBuildCleanupAttribute by type.
|
||||
/// </summary>
|
||||
/// <param name="targetClass">The type of the target class.</param>
|
||||
public PostBuildCleanupAttribute(Type targetClass)
|
||||
{
|
||||
TargetClass = targetClass;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes and returns an instance of PostBuildCleanupAttribute by class name.
|
||||
/// </summary>
|
||||
/// <param name="targetClassName">The name of the target class.</param>
|
||||
public PostBuildCleanupAttribute(string targetClassName)
|
||||
{
|
||||
TargetClass = AttributeHelper.GetTargetClassFromName(targetClassName, typeof(IPostBuildCleanup));
|
||||
}
|
||||
|
||||
internal Type TargetClass { get; private set; }
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 621fd19bcb071b64aa1d68f0271aa780
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@ -0,0 +1,74 @@
|
||||
using System;
|
||||
|
||||
namespace UnityEngine.TestTools
|
||||
{
|
||||
/// <summary>
|
||||
/// PrebuildSetup attribute run if the test or test class is in the current test run. The test is included either by running all tests or setting a filter that includes the test. If multiple tests reference the same pre-built setup or post-build cleanup, then it only runs once.
|
||||
/// </summary>
|
||||
[AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Method)]
|
||||
public class PrebuildSetupAttribute : Attribute
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes and returns an instance of PrebuildSetupAttribute by type.
|
||||
/// </summary>
|
||||
/// <param name="targetClass">The type of the target class.</param>
|
||||
public PrebuildSetupAttribute(Type targetClass)
|
||||
{
|
||||
TargetClass = targetClass;
|
||||
}
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="targetClassName"></param>
|
||||
/// <example>
|
||||
/// <code>
|
||||
/// [TestFixture]
|
||||
/// public class CreateSpriteTest : IPrebuildSetup
|
||||
/// {
|
||||
/// Texture2D m_Texture;
|
||||
/// Sprite m_Sprite;
|
||||
///
|
||||
/// public void Setup()
|
||||
/// {
|
||||
///
|
||||
/// #if UNITY_EDITOR
|
||||
///
|
||||
/// var spritePath = "Assets/Resources/Circle.png";
|
||||
/// var ti = UnityEditor.AssetImporter.GetAtPath(spritePath) as UnityEditor.TextureImporter;
|
||||
/// ti.textureCompression = UnityEditor.TextureImporterCompression.Uncompressed;
|
||||
/// ti.SaveAndReimport();
|
||||
///
|
||||
/// #endif
|
||||
/// }
|
||||
///
|
||||
/// [SetUp]
|
||||
/// public void SetUpTest()
|
||||
/// {
|
||||
/// m_Texture = Resources.Load<Texture2D>("Circle");
|
||||
/// }
|
||||
///
|
||||
/// [Test]
|
||||
/// public void WhenNullTextureIsPassed_CreateShouldReturnNullSprite()
|
||||
/// {
|
||||
///
|
||||
/// // Check with Valid Texture.
|
||||
/// LogAssert.Expect(LogType.Log, "Circle Sprite Created");
|
||||
/// Sprite.Create(m_Texture, new Rect(0, 0, m_Texture.width, m_Texture.height), new Vector2(0.5f, 0.5f));
|
||||
/// Debug.Log("Circle Sprite Created");
|
||||
///
|
||||
/// // Check with NULL Texture. Should return NULL Sprite.
|
||||
/// m_Sprite = Sprite.Create(null, new Rect(0, 0, m_Texture.width, m_Texture.heig`t), new Vector2(0.5f, 0.5f));
|
||||
/// Assert.That(m_Sprite, Is.Null, "Sprite created with null texture should be null");
|
||||
/// }
|
||||
/// }
|
||||
/// </code>
|
||||
/// Tip: Use `#if UNITY_EDITOR` if you want to access Editor only APIs, but the setup/cleanup is inside a **Play Mode** assembly.
|
||||
/// </example>
|
||||
public PrebuildSetupAttribute(string targetClassName)
|
||||
{
|
||||
TargetClass = AttributeHelper.GetTargetClassFromName(targetClassName, typeof(IPrebuildSetup));
|
||||
}
|
||||
|
||||
internal Type TargetClass { get; private set; }
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d1b7ce919aa8864409412e809073cf96
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@ -0,0 +1,80 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace UnityEngine.TestTools.Utils
|
||||
{
|
||||
/// <summary>
|
||||
/// Use this utility to compare two Quaternion objects for equality
|
||||
/// with NUnit assertion constraints.
|
||||
/// Use the static instance QuaternionEqualityComparer.Instance
|
||||
/// to have the default calculation error value set to 0.00001f.
|
||||
/// For any other custom error value, use the one argument constructor.
|
||||
/// </summary>
|
||||
public class QuaternionEqualityComparer : IEqualityComparer<Quaternion>
|
||||
{
|
||||
private const float k_DefaultError = 0.00001f;
|
||||
private readonly float AllowedError;
|
||||
|
||||
private static readonly QuaternionEqualityComparer m_Instance = new QuaternionEqualityComparer();
|
||||
/// <summary>
|
||||
///A comparer instance with the default error value 0.00001f.
|
||||
/// </summary>
|
||||
public static QuaternionEqualityComparer Instance { get { return m_Instance; } }
|
||||
|
||||
|
||||
private QuaternionEqualityComparer() : this(k_DefaultError) {}
|
||||
/// <summary>
|
||||
/// Creates an instance of the comparer with a custom allowed error value.
|
||||
/// </summary>
|
||||
/// <param name="allowedError">Describes the custom allowed error value</param>
|
||||
public QuaternionEqualityComparer(float allowedError)
|
||||
{
|
||||
AllowedError = allowedError;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Compares the actual and expected Quaternion objects
|
||||
/// for equality using the <see cref="Quaternion.Dot "/> method.
|
||||
/// </summary>
|
||||
/// <param name="expected">Expected Quaternion value used for comparison</param>
|
||||
/// <param name="actual">Actual Quaternion value to test</param>
|
||||
/// <returns>True if the quaternion are equals, false otherwise.</returns>
|
||||
/// <example>
|
||||
/// The following example shows how to verify if two Quaternion are equals
|
||||
/// <code>
|
||||
/// [TestFixture]
|
||||
/// public class QuaternionTest
|
||||
/// {
|
||||
/// [Test]
|
||||
/// public void VerifyThat_TwoQuaternionsAreEqual()
|
||||
/// {
|
||||
/// var actual = new Quaternion(10f, 0f, 0f, 0f);
|
||||
/// var expected = new Quaternion(1f, 10f, 0f, 0f);
|
||||
/// var comparer = new QuaternionEqualityComparer(10e-6f);
|
||||
///
|
||||
/// Assert.That(actual, Is.EqualTo(expected).Using(comparer));
|
||||
///
|
||||
/// //Using default error 0.00001f
|
||||
/// actual = new Quaternion(10f, 0f, 0.1f, 0f);
|
||||
/// expected = new Quaternion(1f, 10f, 0.1f, 0f);
|
||||
///
|
||||
/// Assert.That(actual, Is.EqualTo(expected).Using(QuaternionEqualityComparer.Instance));
|
||||
/// }
|
||||
/// }
|
||||
/// </code>
|
||||
/// </example>
|
||||
public bool Equals(Quaternion expected, Quaternion actual)
|
||||
{
|
||||
return Mathf.Abs(Quaternion.Dot(expected, actual)) > (1.0f - AllowedError);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Serves as the default hash function.
|
||||
/// </summary>
|
||||
/// <param name="quaternion">A not null Quaternion</param>
|
||||
/// <returns>Returns 0</returns>
|
||||
public int GetHashCode(Quaternion quaternion)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3b28913f21577de429da928d6d05219f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@ -0,0 +1,43 @@
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace UnityEngine.TestTools.Utils
|
||||
{
|
||||
internal static class StackTraceFilter
|
||||
{
|
||||
private static readonly string[] s_FilteredLogMessages =
|
||||
{
|
||||
@"UnityEngine.DebugLogHandler:Internal_Log",
|
||||
@"UnityEngine.DebugLogHandler:Log",
|
||||
@"UnityEngine.Logger:Log",
|
||||
@"UnityEngine.Debug"
|
||||
};
|
||||
|
||||
private static readonly string[] s_LastMessages =
|
||||
{
|
||||
@"System.Reflection.MonoMethod:InternalInvoke(Object, Object[], Exception&)",
|
||||
@"UnityEditor.TestTools.TestRunner.EditModeRunner:InvokeDelegator"
|
||||
};
|
||||
|
||||
public static string Filter(string inputStackTrace)
|
||||
{
|
||||
int idx;
|
||||
foreach (var lastMessage in s_LastMessages)
|
||||
{
|
||||
idx = inputStackTrace.IndexOf(lastMessage);
|
||||
if (idx != -1)
|
||||
inputStackTrace = inputStackTrace.Substring(0, idx);
|
||||
}
|
||||
|
||||
var inputStackTraceLines = inputStackTrace.Split('\n');
|
||||
var result = new StringBuilder();
|
||||
foreach (var line in inputStackTraceLines)
|
||||
{
|
||||
if (s_FilteredLogMessages.Any(s => line.StartsWith(s)))
|
||||
continue;
|
||||
result.AppendLine(line);
|
||||
}
|
||||
return result.ToString();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: fc748d99f1f0d484a811a566fc7915ec
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@ -0,0 +1,68 @@
|
||||
using System;
|
||||
using UnityEngine.Scripting;
|
||||
|
||||
namespace UnityEngine.TestRunner
|
||||
{
|
||||
/// <summary>
|
||||
/// An assembly level attribute that indicates that a given type should be subscribed for receiving updates on the test progress.
|
||||
/// </summary>
|
||||
/// <example>
|
||||
/// <code>
|
||||
/// using NUnit.Framework.Interfaces;
|
||||
/// using UnityEngine;
|
||||
/// using UnityEngine.TestRunner;
|
||||
///
|
||||
/// [assembly:TestRunCallback(typeof(TestListener))]
|
||||
///
|
||||
/// public class TestListener : ITestRunCallback
|
||||
/// {
|
||||
/// public void RunStarted(ITest testsToRun)
|
||||
/// {
|
||||
///
|
||||
/// }
|
||||
///
|
||||
/// public void RunFinished(ITestResult testResults)
|
||||
/// {
|
||||
/// Debug.Log($"Run finished with result {testResults.ResultState}.");
|
||||
/// }
|
||||
///
|
||||
/// public void TestStarted(ITest test)
|
||||
/// {
|
||||
///
|
||||
/// }
|
||||
///
|
||||
/// public void TestFinished(ITestResult result)
|
||||
/// {
|
||||
///
|
||||
/// }
|
||||
///}
|
||||
/// </code>
|
||||
/// </example>
|
||||
[AttributeUsage(AttributeTargets.Assembly)]
|
||||
public class TestRunCallbackAttribute : Attribute
|
||||
{
|
||||
private Type m_Type;
|
||||
|
||||
/// <summary>
|
||||
/// Constructs a new instance of the <see cref="TestRunCallbackAttribute"/> class.
|
||||
/// </summary>
|
||||
/// <param name="type">A target type that implements <see cref="ITestRunCallback"/>.</param>
|
||||
/// <exception cref="ArgumentException">Throws an ArgumentException if the provided type does not implement <see cref="ITestRunCallback"/>.</exception>
|
||||
public TestRunCallbackAttribute(Type type)
|
||||
{
|
||||
var interfaceType = typeof(ITestRunCallback);
|
||||
if (!interfaceType.IsAssignableFrom(type))
|
||||
{
|
||||
throw new ArgumentException(string.Format(
|
||||
"Type {2} provided to {0} does not implement {1}. If the stripping level is set to high, the implementing class should have the {3}.",
|
||||
this.GetType().Name, interfaceType.Name, type.Name, typeof(PreserveAttribute).Name));
|
||||
}
|
||||
m_Type = type;
|
||||
}
|
||||
|
||||
internal ITestRunCallback ConstructCallback()
|
||||
{
|
||||
return Activator.CreateInstance(m_Type) as ITestRunCallback;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 76b3a3296de548f48b0c3d088fb4b490
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@ -0,0 +1,63 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using NUnit.Framework.Interfaces;
|
||||
using NUnit.Framework.Internal;
|
||||
using UnityEngine.TestTools.TestRunner;
|
||||
|
||||
namespace UnityEngine.TestRunner.Utils
|
||||
{
|
||||
internal class TestRunCallbackListener : ScriptableObject, ITestRunnerListener
|
||||
{
|
||||
private ITestRunCallback[] m_Callbacks;
|
||||
public void RunStarted(ITest testsToRun)
|
||||
{
|
||||
InvokeAllCallbacks(callback => callback.RunStarted(testsToRun));
|
||||
}
|
||||
|
||||
private static ITestRunCallback[] GetAllCallbacks()
|
||||
{
|
||||
var allAssemblies = AppDomain.CurrentDomain.GetAssemblies();
|
||||
allAssemblies = allAssemblies.Where(x => x.GetReferencedAssemblies().Any(z => z.Name == "UnityEngine.TestRunner")).ToArray();
|
||||
var attributes = allAssemblies.SelectMany(assembly => assembly.GetCustomAttributes(typeof(TestRunCallbackAttribute), true).OfType<TestRunCallbackAttribute>()).ToArray();
|
||||
return attributes.Select(attribute => attribute.ConstructCallback()).ToArray();
|
||||
}
|
||||
|
||||
private void InvokeAllCallbacks(Action<ITestRunCallback> invoker)
|
||||
{
|
||||
if (m_Callbacks == null)
|
||||
{
|
||||
m_Callbacks = GetAllCallbacks();
|
||||
}
|
||||
|
||||
foreach (var testRunCallback in m_Callbacks)
|
||||
{
|
||||
try
|
||||
{
|
||||
invoker(testRunCallback);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Debug.LogException(e);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void RunFinished(ITestResult testResults)
|
||||
{
|
||||
InvokeAllCallbacks(callback => callback.RunFinished(testResults));
|
||||
}
|
||||
|
||||
public void TestStarted(ITest test)
|
||||
{
|
||||
InvokeAllCallbacks(callback => callback.TestStarted(test));
|
||||
}
|
||||
|
||||
public void TestFinished(ITestResult result)
|
||||
{
|
||||
InvokeAllCallbacks(callback => callback.TestFinished(result));
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 68f09f0f82599b5448579854e622a4c1
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@ -0,0 +1,94 @@
|
||||
using System;
|
||||
|
||||
namespace UnityEngine.TestTools.Utils
|
||||
{
|
||||
/// <summary>
|
||||
/// This contains test utility functions for float value comparison and creating primitives.
|
||||
/// </summary>
|
||||
public static class Utils
|
||||
{
|
||||
/// <summary>
|
||||
/// Relative epsilon comparison of two float values for equality.
|
||||
/// The relative error is the absolute error divided by the magnitude of the exact value.
|
||||
/// </summary>
|
||||
/// <param name="expected">The expected float value used to compare.</param>
|
||||
/// <param name="actual">The actual float value to test.</param>
|
||||
/// <param name="epsilon"> Epsilon is the relative error to be used in relative epsilon comparison.</param>
|
||||
/// <returns>Returns true if the actual value is equivalent to the expected value.</returns>
|
||||
/// <example>
|
||||
/// <code>
|
||||
/// [TestFixture]
|
||||
/// class UtilsTests
|
||||
/// {
|
||||
/// [Test]
|
||||
/// public void CheckThat_FloatsAreEqual()
|
||||
/// {
|
||||
/// float expected = 10e-8f;
|
||||
/// float actual = 0f;
|
||||
/// float allowedRelativeError = 10e-6f;
|
||||
///
|
||||
/// Assert.That(Utils.AreFloatsEqual(expected, actual, allowedRelativeError), Is.True);
|
||||
/// }
|
||||
/// }
|
||||
/// </code>
|
||||
/// </example>
|
||||
public static bool AreFloatsEqual(float expected, float actual, float epsilon)
|
||||
{
|
||||
// special case for infinity
|
||||
if (expected == Mathf.Infinity || actual == Mathf.Infinity || expected == Mathf.NegativeInfinity || actual == Mathf.NegativeInfinity)
|
||||
return expected == actual;
|
||||
|
||||
// we cover both relative and absolute tolerance with this check
|
||||
// which is better than just relative in case of small (in abs value) args
|
||||
// please note that "usually" approximation is used [i.e. abs(x)+abs(y)+1]
|
||||
// but we speak about test code so we dont care that much about performance
|
||||
// but we do care about checks being more precise
|
||||
return Math.Abs(actual - expected) <= epsilon * Mathf.Max(Mathf.Max(Mathf.Abs(actual), Mathf.Abs(expected)), 1.0f);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Compares two floating point numbers for equality under the given absolute tolerance.
|
||||
/// </summary>
|
||||
/// <param name="expected">The expected float value used to compare.</param>
|
||||
/// <param name="actual">The actual float value to test.</param>
|
||||
/// <param name="allowedAbsoluteError">AllowedAbsoluteError is the permitted error tolerance.</param>
|
||||
/// <returns> Returns true if the actual value is equivalent to the expected value under the given tolerance.
|
||||
/// </returns>
|
||||
/// <example>
|
||||
/// <code>
|
||||
/// [TestFixture]
|
||||
/// class UtilsTests
|
||||
/// {
|
||||
/// [Test]
|
||||
/// public void ChechThat_FloatsAreAbsoluteEqual()
|
||||
/// {
|
||||
/// float expected = 0f;
|
||||
/// float actual = 10e-6f;
|
||||
/// float error = 10e-5f;
|
||||
///
|
||||
/// Assert.That(Utils.AreFloatsEqualAbsoluteError(expected, actual, error), Is.True);
|
||||
/// }
|
||||
/// }
|
||||
/// </code>
|
||||
/// </example>
|
||||
public static bool AreFloatsEqualAbsoluteError(float expected, float actual, float allowedAbsoluteError)
|
||||
{
|
||||
return Math.Abs(actual - expected) <= allowedAbsoluteError;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Analogous to GameObject.CreatePrimitive, but creates a primitive mesh renderer with fast shader instead of a default builtin shader.
|
||||
/// Optimized for testing performance.
|
||||
/// </summary>
|
||||
/// <returns>A GameObject with primitive mesh renderer and collider.</returns>
|
||||
/// <param name="type">The type of primitive object to create.</param>
|
||||
public static GameObject CreatePrimitive(PrimitiveType type)
|
||||
{
|
||||
var prim = GameObject.CreatePrimitive(type);
|
||||
var renderer = prim.GetComponent<Renderer>();
|
||||
if (renderer)
|
||||
renderer.sharedMaterial = new Material(Shader.Find("VertexLit"));
|
||||
return prim;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9502550ba4785e3499d6c9251fa2114b
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@ -0,0 +1,53 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace UnityEngine.TestTools.Utils
|
||||
{
|
||||
/// <summary>
|
||||
/// Use these classes to compare two objects of the same type for equality within the range of a given tolerance using NUnit or custom constraints . Call Instance to apply the default calculation error value to the comparison.
|
||||
/// </summary>
|
||||
public class Vector2ComparerWithEqualsOperator : IEqualityComparer<Vector2>
|
||||
{
|
||||
private static readonly Vector2ComparerWithEqualsOperator m_Instance = new Vector2ComparerWithEqualsOperator();
|
||||
/// <summary>
|
||||
/// A singleton instance of the comparer with a predefined default error value.
|
||||
/// </summary>
|
||||
public static Vector2ComparerWithEqualsOperator Instance { get { return m_Instance; } }
|
||||
|
||||
private Vector2ComparerWithEqualsOperator() {}
|
||||
/// <summary>
|
||||
/// Compares the actual and expected objects for equality using a custom comparison mechanism.
|
||||
/// </summary>
|
||||
/// <param name="expected">Expected Vector2 used to compare</param>
|
||||
/// <param name="actual">Actual Vector2 value to test.</param>
|
||||
/// <returns>Returns true if expected and actual objects are equal, otherwise it returns false.</returns>
|
||||
/// <example>
|
||||
/// <code>
|
||||
/// [TestFixture]
|
||||
/// public class Vector2Test
|
||||
/// {
|
||||
/// [Test]
|
||||
/// public void VerifyThat_TwoVector2ObjectsAreEqual()
|
||||
/// {
|
||||
/// var actual = new Vector2(10e-7f, 10e-7f);
|
||||
/// var expected = new Vector2(0f, 0f);
|
||||
///
|
||||
/// Assert.That(actual, Is.EqualTo(expected).Using(Vector2ComparerWithEqualsOperator.Instance));
|
||||
/// }
|
||||
/// }
|
||||
/// </code>
|
||||
/// </example>
|
||||
public bool Equals(Vector2 expected, Vector2 actual)
|
||||
{
|
||||
return expected == actual;
|
||||
}
|
||||
/// <summary>
|
||||
/// Serves as the default hash function.
|
||||
/// </summary>
|
||||
/// <param name="vec2"> A not null Vector2 object</param>
|
||||
/// <returns>Returns 0</returns>
|
||||
public int GetHashCode(Vector2 vec2)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 65701ebe8bada6b4785e9c7afe7f5bee
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@ -0,0 +1,82 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace UnityEngine.TestTools.Utils
|
||||
{
|
||||
/// <summary>
|
||||
/// Use this class to compare two Vector2 objects for
|
||||
/// equality with NUnit constraints. Use the static
|
||||
/// <see cref="Vector2EqualityComparer.Instance"/>
|
||||
/// to have the calculation error value set to default 0.0001f.
|
||||
/// For any other error value, instantiate a new comparer
|
||||
/// object with the one argument constructor.
|
||||
/// </summary>
|
||||
public class Vector2EqualityComparer : IEqualityComparer<Vector2>
|
||||
{
|
||||
private const float k_DefaultError = 0.0001f;
|
||||
private readonly float AllowedError;
|
||||
|
||||
private static readonly Vector2EqualityComparer m_Instance = new Vector2EqualityComparer();
|
||||
|
||||
/// <summary>
|
||||
/// A comparer instance with the default error value set to 0.0001f.
|
||||
///</summary>
|
||||
public static Vector2EqualityComparer Instance { get { return m_Instance; } }
|
||||
|
||||
private Vector2EqualityComparer() : this(k_DefaultError)
|
||||
{
|
||||
}
|
||||
/// <summary>
|
||||
/// Initializes an instance of Vector2Equality comparer with custom allowed calculation error.
|
||||
/// </summary>
|
||||
/// <param name="error">This value identifies the calculation error allowed.</param>
|
||||
public Vector2EqualityComparer(float error)
|
||||
{
|
||||
this.AllowedError = error;
|
||||
}
|
||||
/// <summary>
|
||||
/// Compares the actual and expected Vector2 objects for equality using the <see cref="Utils.AreFloatsEqual"/> method.
|
||||
/// </summary>
|
||||
/// <param name="expected">The expected Vector2 used for comparison</param>
|
||||
/// <param name="actual">The actual Vector2 to test</param>
|
||||
/// <returns>True if the vectors are equals, false otherwise.</returns>
|
||||
/// <example>
|
||||
/// The following example shows how to verify if two Vector2 are equals
|
||||
///<code>
|
||||
///[TestFixture]
|
||||
/// public class Vector2Test
|
||||
/// {
|
||||
/// [Test]
|
||||
/// public void VerifyThat_TwoVector2ObjectsAreEqual()
|
||||
/// {
|
||||
/// // Custom calculation error
|
||||
/// var actual = new Vector2(10e-7f, 10e-7f);
|
||||
/// var expected = new Vector2(0f, 0f);
|
||||
/// var comparer = new Vector2EqualityComparer(10e-6f);
|
||||
///
|
||||
/// Assert.That(actual, Is.EqualTo(expected).Using(comparer));
|
||||
///
|
||||
/// //Default error 0.0001f
|
||||
/// actual = new Vector2(0.01f, 0.01f);
|
||||
/// expected = new Vector2(0.01f, 0.01f);
|
||||
///
|
||||
/// Assert.That(actual, Is.EqualTo(expected).Using(Vector2EqualityComparer.Instance));
|
||||
/// }
|
||||
/// }
|
||||
/// </code>
|
||||
/// </example>
|
||||
public bool Equals(Vector2 expected, Vector2 actual)
|
||||
{
|
||||
return Utils.AreFloatsEqual(expected.x, actual.x, AllowedError) &&
|
||||
Utils.AreFloatsEqual(expected.y, actual.y, AllowedError);
|
||||
}
|
||||
/// <summary>
|
||||
/// Serves as the default hash function.
|
||||
/// </summary>
|
||||
/// <param name="vec2">A not null Vector2</param>
|
||||
/// <returns>Returns 0</returns>
|
||||
public int GetHashCode(Vector2 vec2)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 58ad09607a0d62d458a78d7174665566
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@ -0,0 +1,53 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace UnityEngine.TestTools.Utils
|
||||
{
|
||||
/// <summary>
|
||||
/// Use these classes to compare two objects of the same type for equality within the range of a given tolerance using NUnit or custom constraints . Call Instance to apply the default calculation error value to the comparison.
|
||||
/// </summary>
|
||||
public class Vector3ComparerWithEqualsOperator : IEqualityComparer<Vector3>
|
||||
{
|
||||
private static readonly Vector3ComparerWithEqualsOperator m_Instance = new Vector3ComparerWithEqualsOperator();
|
||||
/// <summary>
|
||||
/// A singleton instance of the comparer with a predefined default error value.
|
||||
/// </summary>
|
||||
public static Vector3ComparerWithEqualsOperator Instance { get { return m_Instance; } }
|
||||
|
||||
private Vector3ComparerWithEqualsOperator() {}
|
||||
/// <summary>
|
||||
/// Compares the actual and expected objects for equality using a custom comparison mechanism.
|
||||
/// </summary>
|
||||
/// <param name="expected">Expected Vector3 used to compare</param>
|
||||
/// <param name="actual">Actual Vector3 value to test.</param>
|
||||
/// <returns>Returns true if expected and actual objects are equal, otherwise it returns false.</returns>
|
||||
/// <example>
|
||||
/// <code>
|
||||
/// [TestFixture]
|
||||
/// public class Vector3Test
|
||||
/// {
|
||||
/// [Test]
|
||||
/// public void VerifyThat_TwoVector3ObjectsAreEqual()
|
||||
/// {
|
||||
/// var actual = new Vector2(10e-7f, 10e-7f, 10e-7f);
|
||||
/// var expected = new Vector2(0f, 0f, 0f);
|
||||
///
|
||||
/// Assert.That(actual, Is.EqualTo(expected).Using(Vector3ComparerWithEqualsOperator.Instance));
|
||||
/// }
|
||||
/// }
|
||||
/// </code>
|
||||
/// </example>
|
||||
public bool Equals(Vector3 expected, Vector3 actual)
|
||||
{
|
||||
return expected == actual;
|
||||
}
|
||||
/// <summary>
|
||||
/// Serves as the default hash function.
|
||||
/// </summary>
|
||||
/// <param name="vec3"> A not null Vector3 object</param>
|
||||
/// <returns>Returns 0</returns>
|
||||
public int GetHashCode(Vector3 vec3)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5b994928117e3db418da69c821da7e19
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@ -0,0 +1,82 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
|
||||
namespace UnityEngine.TestTools.Utils
|
||||
{
|
||||
/// <summary>
|
||||
/// Use this class to compare two Vector3 objects for equality with NUnit constraints. Call Vector3EqualityComparer.Instance comparer to perform a comparison with the default calculation error value 0.0001f. To specify a different error value, use the one argument constructor to instantiate a new comparer.
|
||||
/// </summary>
|
||||
public class Vector3EqualityComparer : IEqualityComparer<Vector3>
|
||||
{
|
||||
private const float k_DefaultError = 0.0001f;
|
||||
private readonly float AllowedError;
|
||||
|
||||
private static readonly Vector3EqualityComparer m_Instance = new Vector3EqualityComparer();
|
||||
|
||||
/// <summary>
|
||||
/// A comparer instance with the default calculation error value equal to 0.0001f.
|
||||
///</summary>
|
||||
public static Vector3EqualityComparer Instance { get { return m_Instance; } }
|
||||
|
||||
private Vector3EqualityComparer() : this(k_DefaultError) {}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes an instance of Vector3Equality comparer with custom allowed calculation error.
|
||||
/// </summary>
|
||||
/// <param name="allowedError">This value identifies the calculation error allowed.</param>
|
||||
public Vector3EqualityComparer(float allowedError)
|
||||
{
|
||||
this.AllowedError = allowedError;
|
||||
}
|
||||
///<summary>
|
||||
/// Compares the actual and expected Vector3 objects
|
||||
/// for equality using <see cref="Utils.AreFloatsEqual"/> to compare the x, y, and z attributes of Vector3.
|
||||
/// </summary>
|
||||
/// <param name="expected">The expected Vector3 used for comparison</param>
|
||||
/// <param name="actual">The actual Vector3 to test</param>
|
||||
/// <returns>True if the vectors are equals, false otherwise.</returns>
|
||||
/// <example>
|
||||
/// The following example shows how to verify if two Vector3 are equals
|
||||
/// <code>
|
||||
/// [TestFixture]
|
||||
/// public class Vector3Test
|
||||
/// {
|
||||
/// [Test]
|
||||
/// public void VerifyThat_TwoVector3ObjectsAreEqual()
|
||||
/// {
|
||||
/// //Custom error 10e-6f
|
||||
/// var actual = new Vector3(10e-8f, 10e-8f, 10e-8f);
|
||||
/// var expected = new Vector3(0f, 0f, 0f);
|
||||
/// var comparer = new Vector3EqualityComparer(10e-6f);
|
||||
///
|
||||
///
|
||||
/// Assert.That(actual, Is.EqualTo(expected).Using(comparer));
|
||||
///
|
||||
/// //Default error 0.0001f
|
||||
/// actual = new Vector3(0.01f, 0.01f, 0f);
|
||||
/// expected = new Vector3(0.01f, 0.01f, 0f);
|
||||
///
|
||||
/// Assert.That(actual, Is.EqualTo(expected).Using(Vector3EqualityComparer.Instance));
|
||||
/// }
|
||||
/// }
|
||||
/// </code>
|
||||
///</example>
|
||||
|
||||
public bool Equals(Vector3 expected, Vector3 actual)
|
||||
{
|
||||
return Utils.AreFloatsEqual(expected.x, actual.x, AllowedError) &&
|
||||
Utils.AreFloatsEqual(expected.y, actual.y, AllowedError) &&
|
||||
Utils.AreFloatsEqual(expected.z, actual.z, AllowedError);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Serves as the default hash function.
|
||||
/// </summary>
|
||||
/// <param name="vec3">A not null Vector3</param>
|
||||
/// <returns>Returns 0</returns>
|
||||
public int GetHashCode(Vector3 vec3)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4bd2bc28ff24d5c488844851cb785db0
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@ -0,0 +1,53 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace UnityEngine.TestTools.Utils
|
||||
{
|
||||
/// <summary>
|
||||
/// Use these classes to compare two objects of the same type for equality within the range of a given tolerance using NUnit or custom constraints . Call Instance to apply the default calculation error value to the comparison.
|
||||
/// </summary>
|
||||
public class Vector4ComparerWithEqualsOperator : IEqualityComparer<Vector4>
|
||||
{
|
||||
private static readonly Vector4ComparerWithEqualsOperator m_Instance = new Vector4ComparerWithEqualsOperator();
|
||||
/// <summary>
|
||||
/// A singleton instance of the comparer with a predefined default error value.
|
||||
/// </summary>
|
||||
public static Vector4ComparerWithEqualsOperator Instance { get { return m_Instance; } }
|
||||
|
||||
private Vector4ComparerWithEqualsOperator() {}
|
||||
/// <summary>
|
||||
/// Compares the actual and expected objects for equality using a custom comparison mechanism.
|
||||
/// </summary>
|
||||
/// <param name="expected">Expected Vector4 used to compare</param>
|
||||
/// <param name="actual">Actual Vector4 value to test.</param>
|
||||
/// <returns>Returns true if expected and actual objects are equal, otherwise it returns false.</returns>
|
||||
/// <example>
|
||||
/// <code>
|
||||
/// [TestFixture]
|
||||
/// public class Vector4Test
|
||||
/// {
|
||||
/// [Test]
|
||||
/// public void VerifyThat_TwoVector4ObjectsAreEqual()
|
||||
/// {
|
||||
/// var actual = new Vector4(10e-7f, 10e-7f, 10e-7f, 10e-7f);
|
||||
/// var expected = new Vector4(0f, 0f, 0f, 0f);
|
||||
///
|
||||
/// Assert.That(actual, Is.EqualTo(expected).Using(Vector4ComparerWithEqualsOperator.Instance));
|
||||
/// }
|
||||
/// }
|
||||
/// </code>
|
||||
/// </example>
|
||||
public bool Equals(Vector4 expected, Vector4 actual)
|
||||
{
|
||||
return expected == actual;
|
||||
}
|
||||
/// <summary>
|
||||
/// Serves as the default hash function.
|
||||
/// </summary>
|
||||
/// <param name="vec4"> A not null Vector4 object</param>
|
||||
/// <returns>Returns 0</returns>
|
||||
public int GetHashCode(Vector4 vec4)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 44100f5f60f351348b9719b46d46cebe
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@ -0,0 +1,77 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace UnityEngine.TestTools.Utils
|
||||
{
|
||||
/// <summary>
|
||||
/// Use this class to compare two Vector4 objects for equality with NUnit constraints. Call Vector4EqualityComparer.Instance to perform comparisons using default calculation error value 0.0001f. To set a custom test value, instantiate a new comparer using the one argument constructor.
|
||||
/// </summary>
|
||||
public class Vector4EqualityComparer : IEqualityComparer<Vector4>
|
||||
{
|
||||
private const float k_DefaultError = 0.0001f;
|
||||
private readonly float AllowedError;
|
||||
|
||||
private static readonly Vector4EqualityComparer m_Instance = new Vector4EqualityComparer();
|
||||
/// <summary>
|
||||
/// A comparer instance with the default calculation error value set to 0.0001f.
|
||||
/// </summary>
|
||||
public static Vector4EqualityComparer Instance { get { return m_Instance; } }
|
||||
|
||||
private Vector4EqualityComparer() : this(k_DefaultError) {}
|
||||
/// <summary>
|
||||
/// Initializes an instance of Vector4Equality comparer with custom allowed calculation error.
|
||||
/// </summary>
|
||||
/// <param name="allowedError">This value identifies the calculation error allowed.</param>
|
||||
public Vector4EqualityComparer(float allowedError)
|
||||
{
|
||||
this.AllowedError = allowedError;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Compares the actual and expected Vector4 objects for equality using <see cref="Utils.AreFloatsEqual"/> to compare the x, y, z, and w attributes of Vector4.
|
||||
/// </summary>
|
||||
/// <param name="expected">The expected Vector4 used for comparison</param>
|
||||
/// <param name="actual">The actual Vector4 to test</param>
|
||||
/// <returns>True if the vectors are equals, false otherwise.</returns>
|
||||
/// <example>
|
||||
/// <code>
|
||||
///[TestFixture]
|
||||
/// public class Vector4Test
|
||||
/// {
|
||||
/// [Test]
|
||||
/// public void VerifyThat_TwoVector4ObjectsAreEqual()
|
||||
/// {
|
||||
/// // Custom error 10e-6f
|
||||
/// var actual = new Vector4(0, 0, 1e-6f, 1e-6f);
|
||||
/// var expected = new Vector4(1e-6f, 0f, 0f, 0f);
|
||||
/// var comparer = new Vector4EqualityComparer(10e-6f);
|
||||
///
|
||||
/// Assert.That(actual, Is.EqualTo(expected).Using(comparer));
|
||||
///
|
||||
/// // Default error 0.0001f
|
||||
/// actual = new Vector4(0.01f, 0.01f, 0f, 0f);
|
||||
/// expected = new Vector4(0.01f, 0.01f, 0f, 0f);
|
||||
///
|
||||
/// Assert.That(actual, Is.EqualTo(expected).Using(Vector4EqualityComparer.Instance));
|
||||
/// }
|
||||
/// }
|
||||
/// </code>
|
||||
/// </example>
|
||||
public bool Equals(Vector4 expected, Vector4 actual)
|
||||
{
|
||||
return Utils.AreFloatsEqual(expected.x, actual.x, AllowedError) &&
|
||||
Utils.AreFloatsEqual(expected.y, actual.y, AllowedError) &&
|
||||
Utils.AreFloatsEqual(expected.z, actual.z, AllowedError) &&
|
||||
Utils.AreFloatsEqual(expected.w, actual.w, AllowedError);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Serves as the default hash function.
|
||||
/// </summary>
|
||||
/// <param name="vec4"> A not null Vector4 object</param>
|
||||
/// <returns>Returns 0</returns>
|
||||
public int GetHashCode(Vector4 vec4)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 32da81683c22faf458026716a2b821aa
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in New Issue
Block a user