Keine Beschreibung
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

OrderedTestSuiteModifier.cs 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using NUnit.Framework.Interfaces;
  5. using NUnit.Framework.Internal;
  6. namespace UnityEngine.TestRunner.NUnitExtensions
  7. {
  8. internal class OrderedTestSuiteModifier : ITestSuiteModifier
  9. {
  10. internal const string suiteIsReorderedProperty = "suiteIsReordered";
  11. private string[] m_OrderedTestNames;
  12. public OrderedTestSuiteModifier(string[] orderedTestNames)
  13. {
  14. m_OrderedTestNames = orderedTestNames;
  15. }
  16. public TestSuite ModifySuite(TestSuite root)
  17. {
  18. var suite = new TestSuite(root.Name);
  19. suite.Properties.Set(suiteIsReorderedProperty, true);
  20. var workingStack = new List<ITest> { suite };
  21. foreach (var fullName in m_OrderedTestNames)
  22. {
  23. var test = FindTest(root, fullName);
  24. if (test == null)
  25. {
  26. continue;
  27. }
  28. var ancestorList = GetAncestorList(test);
  29. for (int i = 0; i < ancestorList.Count; i++)
  30. {
  31. if (i >= workingStack.Count || ancestorList[i].Name != workingStack[i].Name || !ancestorList[i].HasChildren)
  32. {
  33. // The ancestor list diverges from the current working set. We need to insert a new element
  34. var commonParent = workingStack[i - 1];
  35. var nodeToClone = ancestorList[i];
  36. var newNode = CloneNode(nodeToClone);
  37. CloneProperties(newNode, nodeToClone);
  38. newNode.Properties.Set(suiteIsReorderedProperty, true);
  39. (commonParent as TestSuite).Add(newNode);
  40. if (i < workingStack.Count)
  41. {
  42. workingStack = workingStack.Take(i).ToList();
  43. }
  44. workingStack.Add(newNode);
  45. }
  46. }
  47. }
  48. return suite;
  49. }
  50. private static void CloneProperties(ITest target, ITest source)
  51. {
  52. if (target == source)
  53. {
  54. return;
  55. }
  56. foreach (var key in source.Properties.Keys)
  57. {
  58. foreach (var value in source.Properties[key])
  59. {
  60. target.Properties.Set(key, value);
  61. }
  62. }
  63. }
  64. private static Test CloneNode(ITest test)
  65. {
  66. var type = test.GetType();
  67. if (type == typeof(TestSuite))
  68. {
  69. return new TestSuite(test.Name);
  70. }
  71. if (type == typeof(TestAssembly))
  72. {
  73. var testAssembly = test as TestAssembly;
  74. return new TestAssembly(testAssembly.Assembly, testAssembly.Name);
  75. }
  76. if (type == typeof(TestFixture))
  77. {
  78. return new TestFixture(test.TypeInfo);
  79. }
  80. if (type == typeof(TestMethod))
  81. {
  82. return test as Test;
  83. }
  84. if (type == typeof(ParameterizedMethodSuite))
  85. {
  86. return new ParameterizedMethodSuite(test.Method);
  87. }
  88. throw new NotImplementedException(type.FullName);
  89. }
  90. private static List<ITest> GetAncestorList(ITest test)
  91. {
  92. var list = new List<ITest>();
  93. while (test != null)
  94. {
  95. list.Insert(0, test);
  96. test = test.Parent;
  97. }
  98. return list;
  99. }
  100. private static ITest FindTest(ITest node, string fullName)
  101. {
  102. if (node.HasChildren)
  103. {
  104. return node.Tests
  105. .Select(test => FindTest(test, fullName))
  106. .FirstOrDefault(match => match != null);
  107. }
  108. return TestExtensions.GetFullName(node.FullName, node.HasChildIndex() ? node.GetChildIndex() : -1) == fullName ? node : null;
  109. }
  110. }
  111. }