File size: 5,148 Bytes
18a519f | 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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | using System;
using System.Collections;
using System.Linq;
using NUnit.Framework;
using NUnit.Framework.Interfaces;
using NUnit.Framework.Internal;
using NUnit.Framework.Internal.Commands;
using UnityEngine.TestTools;
namespace UnityEngine.TestRunner.NUnitExtensions.Runner
{
internal static class TestCommandBuilder
{
public static TestCommand BuildTestCommand(TestMethod test, ITestFilter filter)
{
if (test.RunState != RunState.Runnable &&
!(test.RunState == RunState.Explicit && filter.IsExplicitMatch(test)))
{
return new SkipCommand(test);
}
var testReturnsIEnumerator = test.Method.ReturnType.Type == typeof(IEnumerator);
TestCommand command;
if (!testReturnsIEnumerator)
{
command = new UnityTestMethodCommand(test);
}
else
{
command = new EnumerableTestMethodCommand(test);
}
command = new UnityLogCheckDelegatingCommand(command);
foreach (var wrapper in test.Method.GetCustomAttributes<IWrapTestMethod>(true))
{
command = wrapper.Wrap(command);
if (command == null)
{
var message = String.Format("IWrapTestMethod implementation '{0}' returned null as command.",
wrapper.GetType().FullName);
return new FailCommand(test, ResultState.Failure, message);
}
if (testReturnsIEnumerator && !(command is IEnumerableTestMethodCommand))
{
command = TryReplaceWithEnumerableCommand(command);
if (command != null)
{
continue;
}
var message = String.Format("'{0}' is not supported on {1} as it does not handle returning IEnumerator.",
wrapper.GetType().FullName,
GetTestBuilderName(test));
return new FailCommand(test, ResultState.Failure, message);
}
}
command = new UnityEngine.TestTools.TestActionCommand(command);
command = new UnityEngine.TestTools.SetUpTearDownCommand(command);
if (!testReturnsIEnumerator)
{
command = new ImmediateEnumerableCommand(command);
}
foreach (var wrapper in test.Method.GetCustomAttributes<IWrapSetUpTearDown>(true))
{
command = wrapper.Wrap(command);
if (command == null)
{
var message = String.Format("IWrapSetUpTearDown implementation '{0}' returned null as command.",
wrapper.GetType().FullName);
return new FailCommand(test, ResultState.Failure, message);
}
if (testReturnsIEnumerator && !(command is IEnumerableTestMethodCommand))
{
command = TryReplaceWithEnumerableCommand(command);
if (command != null)
{
continue;
}
var message = String.Format("'{0}' is not supported on {1} as it does not handle returning IEnumerator.",
wrapper.GetType().FullName,
GetTestBuilderName(test));
return new FailCommand(test, ResultState.Failure, message);
}
}
command = new EnumerableSetUpTearDownCommand(command);
command = new OuterUnityTestActionCommand(command);
IApplyToContext[] changes = test.Method.GetCustomAttributes<IApplyToContext>(true);
if (changes.Length > 0)
{
command = new EnumerableApplyChangesToContextCommand(command, changes);
}
return command;
}
private static string GetTestBuilderName(TestMethod testMethod)
{
return new[]
{
testMethod.Method.GetCustomAttributes<ITestBuilder>(true).Select(attribute => attribute.GetType().Name),
testMethod.Method.GetCustomAttributes<ISimpleTestBuilder>(true).Select(attribute => attribute.GetType().Name)
}.SelectMany(v => v).FirstOrDefault();
}
private static TestCommand TryReplaceWithEnumerableCommand(TestCommand command)
{
switch (command.GetType().Name)
{
case nameof(RepeatAttribute.RepeatedTestCommand):
return new EnumerableRepeatedTestCommand(command as RepeatAttribute.RepeatedTestCommand);
case nameof(RetryAttribute.RetryCommand):
return new EnumerableRetryTestCommand(command as RetryAttribute.RetryCommand);
default:
return null;
}
}
}
} |