No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TestCommandBuilder.cs 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using System;
  2. using System.Collections;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using NUnit.Framework;
  6. using NUnit.Framework.Interfaces;
  7. using NUnit.Framework.Internal;
  8. using NUnit.Framework.Internal.Commands;
  9. using UnityEngine.TestTools;
  10. using SetUpTearDownCommand = UnityEngine.TestTools.SetUpTearDownCommand;
  11. using TestActionCommand = UnityEngine.TestTools.TestActionCommand;
  12. namespace UnityEngine.TestRunner.NUnitExtensions.Runner
  13. {
  14. internal static class TestCommandBuilder
  15. {
  16. public static TestCommand BuildTestCommand(TestMethod test, ITestFilter filter)
  17. {
  18. if (test.RunState != RunState.Runnable &&
  19. !(test.RunState == RunState.Explicit && filter.IsExplicitMatch(test)))
  20. {
  21. return new SkipCommand(test);
  22. }
  23. var testReturnsIEnumerator = test.Method.ReturnType.Type == typeof(IEnumerator);
  24. var testReturnsTask = test.Method.ReturnType.Type == typeof(Task);
  25. TestCommand command;
  26. if (testReturnsTask)
  27. {
  28. command = new TaskTestMethodCommand(test);
  29. }
  30. else if (testReturnsIEnumerator)
  31. {
  32. command = new EnumerableTestMethodCommand(test);
  33. }
  34. else
  35. {
  36. command = new UnityTestMethodCommand(test);
  37. }
  38. command = new UnityLogCheckDelegatingCommand(command);
  39. foreach (var wrapper in test.Method.GetCustomAttributes<IWrapTestMethod>(true))
  40. {
  41. command = wrapper.Wrap(command);
  42. if (command == null)
  43. {
  44. var message = String.Format("IWrapTestMethod implementation '{0}' returned null as command.",
  45. wrapper.GetType().FullName);
  46. return new FailCommand(test, ResultState.Failure, message);
  47. }
  48. if (testReturnsIEnumerator && !(command is IEnumerableTestMethodCommand))
  49. {
  50. command = TryReplaceWithEnumerableCommand(command);
  51. if (command != null)
  52. {
  53. continue;
  54. }
  55. var message = String.Format("'{0}' is not supported on {1} as it does not handle returning IEnumerator.",
  56. wrapper.GetType().FullName,
  57. GetTestBuilderName(test));
  58. return new FailCommand(test, ResultState.Failure, message);
  59. }
  60. }
  61. command = new TestActionCommand(command);
  62. if (!testReturnsIEnumerator && !testReturnsTask)
  63. {
  64. command = new ImmediateEnumerableCommand(command);
  65. }
  66. command = new SetUpTearDownCommand(command);
  67. foreach (var wrapper in test.Method.GetCustomAttributes<IWrapSetUpTearDown>(true))
  68. {
  69. if (command is SetUpTearDownCommand && !testReturnsIEnumerator && !testReturnsTask)
  70. {
  71. // Ensure that we can use the immediate execute on the setup/teardown
  72. command = new ImmediateEnumerableCommand(command);
  73. }
  74. command = wrapper.Wrap(command);
  75. if (command == null)
  76. {
  77. var message = String.Format("IWrapSetUpTearDown implementation '{0}' returned null as command.",
  78. wrapper.GetType().FullName);
  79. return new FailCommand(test, ResultState.Failure, message);
  80. }
  81. if (testReturnsIEnumerator && !(command is IEnumerableTestMethodCommand))
  82. {
  83. command = TryReplaceWithEnumerableCommand(command);
  84. if (command != null)
  85. {
  86. continue;
  87. }
  88. var message = String.Format("'{0}' is not supported on {1} as it does not handle returning IEnumerator.",
  89. wrapper.GetType().FullName,
  90. GetTestBuilderName(test));
  91. return new FailCommand(test, ResultState.Failure, message);
  92. }
  93. }
  94. command = new EnumerableSetUpTearDownCommand(command);
  95. command = new OuterUnityTestActionCommand(command);
  96. command = new RetryCommand(command);
  97. command = new RepeatCommand(command);
  98. IApplyToContext[] changes = test.Method.GetCustomAttributes<IApplyToContext>(true);
  99. if (changes.Length > 0)
  100. {
  101. command = new EnumerableApplyChangesToContextCommand(command, changes);
  102. }
  103. command = new TimeoutCommand(command);
  104. command = new IgnoreTestCommand(command, test);
  105. command = new StrictCheckCommand(command);
  106. return command;
  107. }
  108. private static string GetTestBuilderName(TestMethod testMethod)
  109. {
  110. return new[]
  111. {
  112. testMethod.Method.GetCustomAttributes<ITestBuilder>(true).Select(attribute => attribute.GetType().Name),
  113. testMethod.Method.GetCustomAttributes<ISimpleTestBuilder>(true).Select(attribute => attribute.GetType().Name)
  114. }.SelectMany(v => v).FirstOrDefault();
  115. }
  116. private static TestCommand TryReplaceWithEnumerableCommand(TestCommand command)
  117. {
  118. switch (command.GetType().Name)
  119. {
  120. case nameof(RepeatAttribute.RepeatedTestCommand):
  121. return new EnumerableRepeatedTestCommand(command as RepeatAttribute.RepeatedTestCommand);
  122. case nameof(RetryAttribute.RetryCommand):
  123. return new EnumerableRetryTestCommand(command as RetryAttribute.RetryCommand);
  124. default:
  125. return null;
  126. }
  127. }
  128. }
  129. }