A game about forced loneliness, made by TACStudios
1using System; 2using System.Collections.Generic; 3using System.Linq; 4using NUnit.Framework.Interfaces; 5using UnityEngine; 6using UnityEngine.TestTools; 7 8namespace UnityEditor.TestTools.TestRunner 9{ 10 internal abstract class AttributeFinderBase : IAttributeFinder 11 { 12 public abstract IEnumerable<Type> Search(ITest tests, ITestFilter filter, RuntimePlatform testTargetPlatform); 13 } 14 15 internal interface IAttributeFinder 16 { 17 IEnumerable<Type> Search(ITest tests, ITestFilter filter, RuntimePlatform testTargetPlatform); 18 } 19 20 internal abstract class AttributeFinderBase<T1, T2> : AttributeFinderBase where T2 : Attribute 21 { 22 private readonly Func<T2, Type> m_TypeSelector; 23 protected AttributeFinderBase(Func<T2, Type> typeSelector) 24 { 25 m_TypeSelector = typeSelector; 26 } 27 28 public override IEnumerable<Type> Search(ITest tests, ITestFilter filter, RuntimePlatform testTargetPlatform) 29 { 30 var selectedTests = new List<ITest>(); 31 GetMatchingTests(tests, filter, ref selectedTests, testTargetPlatform); 32 33 var result = new List<Type>(); 34 result.AddRange(GetTypesFromPrebuildAttributes(selectedTests)); 35 result.AddRange(GetTypesFromInterface(selectedTests, testTargetPlatform)); 36 37 return result.Distinct(); 38 } 39 40 private static void GetMatchingTests(ITest tests, ITestFilter filter, ref List<ITest> resultList, RuntimePlatform testTargetPlatform) 41 { 42 foreach (var test in tests.Tests) 43 { 44 if (IsTestEnabledOnPlatform(test, testTargetPlatform)) 45 { 46 if (test.IsSuite) 47 { 48 GetMatchingTests(test, filter, ref resultList, testTargetPlatform); 49 } 50 else 51 { 52 if (filter.Pass(test)) 53 resultList.Add(test); 54 } 55 } 56 } 57 } 58 59 private static bool IsTestEnabledOnPlatform(ITest test, RuntimePlatform testTargetPlatform) 60 { 61 if (test.Method == null) 62 { 63 return true; 64 } 65 66 var attributesFromMethods = test.Method.GetCustomAttributes<UnityPlatformAttribute>(true); 67 var attributesFromTypes = test.Method.TypeInfo.GetCustomAttributes<UnityPlatformAttribute>(true); 68 69 if (attributesFromMethods.Length == 0 && attributesFromTypes.Length == 0) 70 { 71 return true; 72 } 73 74 if (!attributesFromMethods.All(a => a.IsPlatformSupported(testTargetPlatform))) 75 { 76 return false; 77 } 78 79 if (!attributesFromTypes.All(a => a.IsPlatformSupported(testTargetPlatform))) 80 { 81 return false; 82 } 83 84 return true; 85 } 86 87 private IEnumerable<Type> GetTypesFromPrebuildAttributes(IEnumerable<ITest> tests) 88 { 89 var allAssemblies = AppDomain.CurrentDomain.GetAssemblies(); 90 allAssemblies = allAssemblies.Where(x => x.GetReferencedAssemblies().Any(z => z.Name == "UnityEditor.TestRunner")).ToArray(); 91 var attributesFromAssemblies = allAssemblies.SelectMany(assembly => assembly.GetCustomAttributes(typeof(T2), true).OfType<T2>()); 92 var attributesFromMethods = tests.SelectMany(t => t.Method.GetCustomAttributes<T2>(true).Select(attribute => attribute)); 93 var attributesFromTypes = tests.SelectMany(t => t.Method.TypeInfo.GetCustomAttributes<T2>(true).Select(attribute => attribute)); 94 95 var result = new List<T2>(); 96 result.AddRange(attributesFromAssemblies); 97 result.AddRange(attributesFromMethods); 98 result.AddRange(attributesFromTypes); 99 100 return result.Select(m_TypeSelector).Where(type => type != null); 101 } 102 103 private static IEnumerable<Type> GetTypesFromInterface(IEnumerable<ITest> selectedTests, RuntimePlatform testTargetPlatform) 104 { 105 var typesWithInterfaces = selectedTests.Where(t => typeof(T1).IsAssignableFrom(t.Method.TypeInfo.Type) && IsTestEnabledOnPlatform(t, testTargetPlatform)); 106 return typesWithInterfaces.Select(t => t.Method.TypeInfo.Type); 107 } 108 } 109}