A game about forced loneliness, made by TACStudios
1using System;
2using System.Collections;
3using System.Collections.Generic;
4using NUnit.Framework.Interfaces;
5using UnityEngine;
6using UnityEngine.TestTools.Logging;
7
8namespace UnityEditor.TestTools.TestRunner.TestRun.Tasks
9{
10 internal abstract class BuildActionTaskBase<T> : TestTaskBase
11 {
12 private string typeName;
13 internal IAttributeFinder attributeFinder;
14 internal Action<string> logAction = Debug.Log;
15 internal Func<ILogScope> logScopeProvider = () => new LogScope();
16 internal Func<Type, object> createInstance = Activator.CreateInstance;
17
18 protected BuildActionTaskBase(IAttributeFinder attributeFinder)
19 {
20 this.attributeFinder = attributeFinder;
21 typeName = typeof(T).Name;
22 }
23
24 protected abstract void Action(T target);
25
26 public override IEnumerator Execute(TestJobData testJobData)
27 {
28 if (testJobData.testTree == null)
29 {
30 throw new Exception($"Test tree is not available for {GetType().Name}.");
31 }
32
33 var enumerator = ExecuteMethods(testJobData.testTree, testJobData.testFilter, testJobData.TargetRuntimePlatform ?? Application.platform);
34
35 while (enumerator.MoveNext())
36 {
37 yield return null;
38 }
39 }
40
41 private IEnumerator ExecuteMethods(ITest testTree, ITestFilter testRunnerFilter, RuntimePlatform targetPlatform)
42 {
43 var exceptions = new List<Exception>();
44
45 foreach (var targetClassType in attributeFinder.Search(testTree, testRunnerFilter, targetPlatform))
46 {
47 try
48 {
49 var targetClass = (T)createInstance(targetClassType);
50
51 logAction($"Executing {typeName} for: {targetClassType.FullName}.");
52
53 using (var logScope = logScopeProvider())
54 {
55 Action(targetClass);
56 logScope.EvaluateLogScope(true);
57 }
58 }
59 catch (Exception ex)
60 {
61 exceptions.Add(ex);
62 }
63 }
64
65 if (exceptions.Count > 0)
66 {
67 throw new AggregateException($"One or more exceptions when executing {typeName}.", exceptions);
68 }
69 yield break;
70 }
71 }
72}