TestEnumerator.cs
2.6 KB
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
using System;
using System.Collections;
using NUnit.Framework;
using NUnit.Framework.Interfaces;
using NUnit.Framework.Internal;
using UnityEngine.TestRunner.NUnitExtensions;
namespace UnityEngine.TestTools
{
internal class TestEnumerator
{
private readonly ITestExecutionContext m_Context;
private static IEnumerator m_TestEnumerator;
public static IEnumerator Enumerator { get { return m_TestEnumerator; } }
public static void Reset()
{
m_TestEnumerator = null;
}
public TestEnumerator(ITestExecutionContext context, IEnumerator testEnumerator)
{
m_Context = context;
m_TestEnumerator = testEnumerator;
}
public IEnumerator Execute()
{
m_Context.CurrentResult.SetResult(ResultState.Success);
return Execute(m_TestEnumerator, new EnumeratorContext(m_Context));
}
private IEnumerator Execute(IEnumerator enumerator, EnumeratorContext context)
{
while (true)
{
if (context.ExceptionWasRecorded)
{
break;
}
try
{
if (!enumerator.MoveNext())
{
break;
}
}
catch (Exception ex)
{
context.RecordExceptionWithHint(ex);
break;
}
if (enumerator.Current is IEnumerator nestedEnumerator)
{
yield return Execute(nestedEnumerator, context);
}
else
{
yield return enumerator.Current;
}
}
}
private class EnumeratorContext
{
private readonly ITestExecutionContext m_Context;
public EnumeratorContext(ITestExecutionContext context)
{
m_Context = context;
}
public bool ExceptionWasRecorded
{
get;
private set;
}
public void RecordExceptionWithHint(Exception ex)
{
if (ExceptionWasRecorded)
{
return;
}
m_Context.CurrentResult.RecordException(ex);
ExceptionWasRecorded = true;
}
}
}
}