summaryrefslogtreecommitdiff
path: root/Library/PackageCache/com.unity.test-framework@1.1.11/UnityEditor.TestRunner/TestLaunchers/AttributeFinderBase.cs
diff options
context:
space:
mode:
authorAndrew Lee <alee14498@protonmail.com>2020-04-19 17:19:32 -0400
committerAndrew Lee <alee14498@protonmail.com>2020-04-19 17:19:32 -0400
commitc55fba8ab2a1c9d3df65eda4a5a1e957f4aa1f78 (patch)
treeee4d51c7c1d633e11f46453ef1edd3c77c4ef9f7 /Library/PackageCache/com.unity.test-framework@1.1.11/UnityEditor.TestRunner/TestLaunchers/AttributeFinderBase.cs
downloadProject-Sandbox-c55fba8ab2a1c9d3df65eda4a5a1e957f4aa1f78.tar.gz
Project-Sandbox-c55fba8ab2a1c9d3df65eda4a5a1e957f4aa1f78.tar.bz2
Project-Sandbox-c55fba8ab2a1c9d3df65eda4a5a1e957f4aa1f78.zip
Inital commit
Diffstat (limited to 'Library/PackageCache/com.unity.test-framework@1.1.11/UnityEditor.TestRunner/TestLaunchers/AttributeFinderBase.cs')
-rw-r--r--Library/PackageCache/com.unity.test-framework@1.1.11/UnityEditor.TestRunner/TestLaunchers/AttributeFinderBase.cs104
1 files changed, 104 insertions, 0 deletions
diff --git a/Library/PackageCache/com.unity.test-framework@1.1.11/UnityEditor.TestRunner/TestLaunchers/AttributeFinderBase.cs b/Library/PackageCache/com.unity.test-framework@1.1.11/UnityEditor.TestRunner/TestLaunchers/AttributeFinderBase.cs
new file mode 100644
index 0000000..c1690a2
--- /dev/null
+++ b/Library/PackageCache/com.unity.test-framework@1.1.11/UnityEditor.TestRunner/TestLaunchers/AttributeFinderBase.cs
@@ -0,0 +1,104 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using NUnit.Framework.Interfaces;
+using UnityEngine;
+using UnityEngine.TestTools;
+
+namespace UnityEditor.TestTools.TestRunner
+{
+ internal abstract class AttributeFinderBase : IAttributeFinder
+ {
+ public abstract IEnumerable<Type> Search(ITest tests, ITestFilter filter, RuntimePlatform testTargetPlatform);
+ }
+
+ internal interface IAttributeFinder
+ {
+ IEnumerable<Type> Search(ITest tests, ITestFilter filter, RuntimePlatform testTargetPlatform);
+ }
+
+ internal abstract class AttributeFinderBase<T1, T2> : AttributeFinderBase where T2 : Attribute
+ {
+ private readonly Func<T2, Type> m_TypeSelector;
+ protected AttributeFinderBase(Func<T2, Type> typeSelector)
+ {
+ m_TypeSelector = typeSelector;
+ }
+
+ public override IEnumerable<Type> Search(ITest tests, ITestFilter filter, RuntimePlatform testTargetPlatform)
+ {
+ var selectedTests = new List<ITest>();
+ GetMatchingTests(tests, filter, ref selectedTests, testTargetPlatform);
+
+ var result = new List<Type>();
+ result.AddRange(GetTypesFromPrebuildAttributes(selectedTests));
+ result.AddRange(GetTypesFromInterface(selectedTests, testTargetPlatform));
+
+ return result.Distinct();
+ }
+
+ private static void GetMatchingTests(ITest tests, ITestFilter filter, ref List<ITest> resultList, RuntimePlatform testTargetPlatform)
+ {
+ foreach (var test in tests.Tests)
+ {
+ if (IsTestEnabledOnPlatform(test, testTargetPlatform))
+ {
+ if (test.IsSuite)
+ {
+ GetMatchingTests(test, filter, ref resultList, testTargetPlatform);
+ }
+ else
+ {
+ if (filter.Pass(test))
+ resultList.Add(test);
+ }
+ }
+ }
+ }
+
+ private static bool IsTestEnabledOnPlatform(ITest test, RuntimePlatform testTargetPlatform)
+ {
+ if (test.Method == null)
+ {
+ return true;
+ }
+
+ var attributesFromMethods = test.Method.GetCustomAttributes<UnityPlatformAttribute>(true).Select(attribute => attribute);
+ var attributesFromTypes = test.Method.TypeInfo.GetCustomAttributes<UnityPlatformAttribute>(true).Select(attribute => attribute);
+
+ if (!attributesFromMethods.All(a => a.IsPlatformSupported(testTargetPlatform)))
+ {
+ return false;
+ }
+
+ if (!attributesFromTypes.All(a => a.IsPlatformSupported(testTargetPlatform)))
+ {
+ return false;
+ }
+
+ return true;
+ }
+
+ private IEnumerable<Type> GetTypesFromPrebuildAttributes(IEnumerable<ITest> tests)
+ {
+ var allAssemblies = AppDomain.CurrentDomain.GetAssemblies();
+ allAssemblies = allAssemblies.Where(x => x.GetReferencedAssemblies().Any(z => z.Name == "UnityEditor.TestRunner")).ToArray();
+ var attributesFromAssemblies = allAssemblies.SelectMany(assembly => assembly.GetCustomAttributes(typeof(T2), true).OfType<T2>());
+ var attributesFromMethods = tests.SelectMany(t => t.Method.GetCustomAttributes<T2>(true).Select(attribute => attribute));
+ var attributesFromTypes = tests.SelectMany(t => t.Method.TypeInfo.GetCustomAttributes<T2>(true).Select(attribute => attribute));
+
+ var result = new List<T2>();
+ result.AddRange(attributesFromAssemblies);
+ result.AddRange(attributesFromMethods);
+ result.AddRange(attributesFromTypes);
+
+ return result.Select(m_TypeSelector).Where(type => type != null);
+ }
+
+ private static IEnumerable<Type> GetTypesFromInterface(IEnumerable<ITest> selectedTests, RuntimePlatform testTargetPlatform)
+ {
+ var typesWithInterfaces = selectedTests.Where(t => typeof(T1).IsAssignableFrom(t.Method.TypeInfo.Type) && IsTestEnabledOnPlatform(t, testTargetPlatform));
+ return typesWithInterfaces.Select(t => t.Method.TypeInfo.Type);
+ }
+ }
+}