SW 중심대학 OSS GIT 서버 박건태, 이승준, 고기완, 이준호 새로운 배포
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.

146 lines
4.7 KiB

4 years ago
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using NUnit.Framework.Interfaces;
  4. using NUnit.Framework.Internal;
  5. using UnityEngine.TestRunner.NUnitExtensions.Filters;
  6. namespace UnityEngine.TestRunner.NUnitExtensions
  7. {
  8. internal static class TestExtensions
  9. {
  10. private static IEnumerable<string> GetTestCategories(this ITest test)
  11. {
  12. var categories = test.Properties[PropertyNames.Category].Cast<string>().ToList();
  13. if (categories.Count == 0 && test is TestMethod)
  14. {
  15. // only mark tests as Uncategorized if the test fixture doesn't have a category,
  16. // otherwise the test inherits the Fixture category
  17. var fixtureCategories = test.Parent.Properties[PropertyNames.Category].Cast<string>().ToList();
  18. if (fixtureCategories.Count == 0)
  19. categories.Add(CategoryFilterExtended.k_DefaultCategory);
  20. }
  21. return categories;
  22. }
  23. public static bool HasCategory(this ITest test, string[] categoryFilter)
  24. {
  25. var categories = test.GetAllCategoriesFromTest().Distinct();
  26. return categoryFilter.Any(c => categories.Any(r => r == c));
  27. }
  28. public static List<string> GetAllCategoriesFromTest(this ITest test)
  29. {
  30. if (test.Parent == null)
  31. return test.GetTestCategories().ToList();
  32. var categories = GetAllCategoriesFromTest(test.Parent);
  33. categories.AddRange(test.GetTestCategories());
  34. return categories;
  35. }
  36. public static void ParseForNameDuplicates(this ITest test)
  37. {
  38. var duplicates = new Dictionary<string, int>();
  39. for (var i = 0; i < test.Tests.Count; i++)
  40. {
  41. var child = test.Tests[i];
  42. int count;
  43. if (duplicates.TryGetValue(child.FullName, out count))
  44. {
  45. count++;
  46. child.Properties.Add("childIndex", count);
  47. duplicates[child.FullName] = count;
  48. }
  49. else
  50. {
  51. duplicates.Add(child.FullName, 1);
  52. }
  53. ParseForNameDuplicates(child);
  54. }
  55. }
  56. public static int GetChildIndex(this ITest test)
  57. {
  58. var index = test.Properties["childIndex"];
  59. return (int)index[0];
  60. }
  61. public static bool HasChildIndex(this ITest test)
  62. {
  63. var index = test.Properties["childIndex"];
  64. return index.Count > 0;
  65. }
  66. static string GetAncestorPath(ITest test)
  67. {
  68. var path = "";
  69. var testParent = test.Parent;
  70. while (testParent != null && testParent.Parent != null && !string.IsNullOrEmpty(testParent.Name))
  71. {
  72. path = testParent.Name + "/" + path;
  73. testParent = testParent.Parent;
  74. }
  75. return path;
  76. }
  77. public static string GetUniqueName(this ITest test)
  78. {
  79. var id = GetAncestorPath(test) + GetFullName(test);
  80. if (test.HasChildIndex())
  81. {
  82. var index = test.GetChildIndex();
  83. if (index >= 0)
  84. id += index;
  85. }
  86. if (test.IsSuite)
  87. {
  88. id += "[suite]";
  89. }
  90. return id;
  91. }
  92. public static string GetFullName(ITest test)
  93. {
  94. if (test.TypeInfo == null && (test.Parent == null || test.Parent.TypeInfo == null))
  95. {
  96. return "[" + test.FullName + "]";
  97. }
  98. var assemblyId = test.TypeInfo == null ? test.Parent.TypeInfo.Assembly.GetName().Name : test.TypeInfo.Assembly.GetName().Name;
  99. return string.Format("[{0}][{1}]", assemblyId, test.FullName);
  100. }
  101. public static string GetSkipReason(this ITest test)
  102. {
  103. if (test.Properties.ContainsKey(PropertyNames.SkipReason))
  104. return (string)test.Properties.Get(PropertyNames.SkipReason);
  105. return null;
  106. }
  107. public static string GetParentId(this ITest test)
  108. {
  109. if (test.Parent != null)
  110. return test.Parent.Id;
  111. return null;
  112. }
  113. public static string GetParentFullName(this ITest test)
  114. {
  115. if (test.Parent != null)
  116. return test.Parent.FullName;
  117. return null;
  118. }
  119. public static string GetParentUniqueName(this ITest test)
  120. {
  121. if (test.Parent != null)
  122. return GetUniqueName(test.Parent);
  123. return null;
  124. }
  125. }
  126. }