UnityLogCheckDelegatingCommand.cs
3.27 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
95
96
97
98
99
100
101
102
103
104
105
106
using System;
using System.Collections;
using System.Linq;
using NUnit.Framework.Internal;
using NUnit.Framework.Internal.Commands;
using UnityEngine.TestTools.Logging;
using UnityEngine.TestTools.TestRunner;
namespace UnityEngine.TestRunner.NUnitExtensions.Runner
{
internal class UnityLogCheckDelegatingCommand : DelegatingTestCommand, IEnumerableTestMethodCommand
{
public UnityLogCheckDelegatingCommand(TestCommand innerCommand)
: base(innerCommand) {}
public override TestResult Execute(ITestExecutionContext context)
{
var logCollector = new LogScope();
try
{
innerCommand.Execute(context);
if (logCollector.AnyFailingLogs())
{
var failingLog = logCollector.FailingLogs.First();
throw new UnhandledLogMessageException(failingLog);
}
if (logCollector.ExpectedLogs.Any())
{
throw new UnexpectedLogMessageException(logCollector.ExpectedLogs.Peek());
}
}
catch (Exception exception)
{
context.CurrentResult.RecordException(exception);
}
logCollector.Dispose();
return context.CurrentResult;
}
public IEnumerable ExecuteEnumerable(ITestExecutionContext context)
{
var logCollector = new LogScope();
if (!(innerCommand is IEnumerableTestMethodCommand))
{
Execute(context);
yield break;
}
var enumerableTestMethodCommand = (IEnumerableTestMethodCommand)innerCommand;
IEnumerable executeEnumerable;
try
{
executeEnumerable = enumerableTestMethodCommand.ExecuteEnumerable(context);
}
catch (Exception e)
{
context.CurrentResult.RecordException(e);
yield break;
}
foreach (var step in executeEnumerable)
{
try
{
if (logCollector.AnyFailingLogs())
{
var failingLog = logCollector.FailingLogs.First();
throw new UnhandledLogMessageException(failingLog);
}
}
catch (Exception e)
{
context.CurrentResult.RecordException(e);
break;
}
yield return step;
}
try
{
if (logCollector.AnyFailingLogs())
{
var failingLog = logCollector.FailingLogs.First();
throw new UnhandledLogMessageException(failingLog);
}
logCollector.ProcessExpectedLogs();
if (logCollector.ExpectedLogs.Any())
{
throw new UnexpectedLogMessageException(LogScope.Current.ExpectedLogs.Peek());
}
}
catch (Exception exception)
{
context.CurrentResult.RecordException(exception);
}
logCollector.Dispose();
}
}
}