summaryrefslogtreecommitdiff
path: root/Library/PackageCache/com.unity.test-framework@1.1.11/UnityEngine.TestRunner/NUnitExtensions/Runner/DefaultTestWorkItem.cs
blob: 5acf8bce3a22396d7ed9af9178ce2bc41f112f2b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using System;
using System.Collections;
using System.Linq;
using NUnit.Framework.Interfaces;
using NUnit.Framework.Internal;
using NUnit.Framework.Internal.Commands;
using NUnit.Framework.Internal.Execution;
using UnityEngine.TestTools;
using SetUpTearDownCommand = NUnit.Framework.Internal.Commands.SetUpTearDownCommand;
using TestActionCommand = NUnit.Framework.Internal.Commands.TestActionCommand;

namespace UnityEngine.TestRunner.NUnitExtensions.Runner
{
    internal class EditModeTestCallbacks
    {
        public static Action RestoringTestContext { get; set; }
    }

    internal class DefaultTestWorkItem : UnityWorkItem
    {
        private TestCommand _command;

        public DefaultTestWorkItem(TestMethod test, ITestFilter filter)
            : base(test, null)
        {
            _command = TestCommandBuilder.BuildTestCommand(test, filter);
        }

        protected override IEnumerable PerformWork()
        {
            if (m_DontRunRestoringResult && EditModeTestCallbacks.RestoringTestContext != null)
            {
                EditModeTestCallbacks.RestoringTestContext();
                Result = Context.CurrentResult;
                yield break;
            }

            try
            {
                if (_command is SkipCommand || _command is FailCommand)
                {
                    Result = _command.Execute(Context);
                    yield break;
                }

                if (!(_command is IEnumerableTestMethodCommand))
                {
                    Debug.LogError("Cannot perform work on " + _command.GetType().Name);
                    yield break;
                }

                foreach (var workItemStep in ((IEnumerableTestMethodCommand)_command).ExecuteEnumerable(Context))
                {
                    ResultedInDomainReload = false;

                    if (workItemStep is IEditModeTestYieldInstruction)
                    {
                        var editModeTestYieldInstruction = (IEditModeTestYieldInstruction)workItemStep;
                        yield return editModeTestYieldInstruction;
                        var enumerator = editModeTestYieldInstruction.Perform();
                        while (true)
                        {
                            bool moveNext;
                            try
                            {
                                moveNext = enumerator.MoveNext();
                            }
                            catch (Exception e)
                            {
                                Context.CurrentResult.RecordException(e);
                                break;
                            }

                            if (!moveNext)
                            {
                                break;
                            }

                            yield return null;
                        }
                    }
                    else
                    {
                        yield return workItemStep;
                    }
                }

                Result = Context.CurrentResult;
            }
            finally
            {
                WorkItemComplete();
            }
        }
    }
}