File size: 6,528 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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 | using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using NUnit.Framework.Interfaces;
using NUnit.Framework.Internal;
using UnityEngine.TestRunner.NUnitExtensions.Filters;
namespace UnityEngine.TestRunner.NUnitExtensions
{
internal static class TestExtensions
{
private static IEnumerable<string> GetTestCategories(this ITest test)
{
var categories = test.Properties[PropertyNames.Category].Cast<string>().ToList();
if (categories.Count == 0 && test is TestMethod)
{
// only mark tests as Uncategorized if the test fixture doesn't have a category,
// otherwise the test inherits the Fixture category
var fixtureCategories = test.Parent.Properties[PropertyNames.Category].Cast<string>().ToList();
if (fixtureCategories.Count == 0)
categories.Add(CategoryFilterExtended.k_DefaultCategory);
}
return categories;
}
public static bool HasCategory(this ITest test, string[] categoryFilter)
{
var categories = test.GetAllCategoriesFromTest().Distinct();
return categoryFilter.Any(c => categories.Any(r => r == c));
}
public static List<string> GetAllCategoriesFromTest(this ITest test)
{
if (test.Parent == null)
return test.GetTestCategories().ToList();
var categories = GetAllCategoriesFromTest(test.Parent);
categories.AddRange(test.GetTestCategories());
return categories;
}
public static void ParseForNameDuplicates(this ITest test)
{
var duplicates = new Dictionary<string, int>();
for (var i = 0; i < test.Tests.Count; i++)
{
var child = test.Tests[i];
int count;
if (duplicates.TryGetValue(child.FullName, out count))
{
count++;
child.Properties.Add("childIndex", count);
duplicates[child.FullName] = count;
}
else
{
duplicates.Add(child.FullName, 1);
}
ParseForNameDuplicates(child);
}
}
public static int GetChildIndex(this ITest test)
{
var index = test.Properties["childIndex"];
return (int)index[0];
}
public static bool HasChildIndex(this ITest test)
{
var index = test.Properties["childIndex"];
return index.Count > 0;
}
static string GetAncestorPath(ITest test)
{
var path = "";
var testParent = test.Parent;
while (testParent != null && testParent.Parent != null && !string.IsNullOrEmpty(testParent.Name))
{
path = testParent.Name + "/" + path;
testParent = testParent.Parent;
}
return path;
}
public static string GetUniqueName(this ITest test)
{
var id = GetAncestorPath(test) + GetFullName(test);
if (test.HasChildIndex())
{
var index = test.GetChildIndex();
if (index >= 0)
id += index;
}
if (test.IsSuite)
{
id += "[suite]";
}
return id;
}
public static string GetFullName(this ITest test)
{
var typeInfo = test.TypeInfo ?? test.Parent?.TypeInfo ?? test.Tests.FirstOrDefault()?.TypeInfo;
if (typeInfo == null)
{
return "[" + test.Name + "]";
}
var assemblyId = typeInfo.Assembly.GetName().Name;
if (assemblyId == test.Name)
{
return $"[{test.Name}]";
}
return string.Format("[{0}][{1}]", assemblyId, test.FullName);
}
public static string GetFullNameWithoutDllPath(this ITest test)
{
if (test.Parent == null)
{
return string.Empty;
}
var typeInfo = test.TypeInfo ?? test.Parent?.TypeInfo;
if (typeInfo == null && IsAssembly(test))
{
return test.Name;
}
return test.FullName;
}
private static bool IsAssembly(this ITest test)
{
return test.Parent.Parent == null;
}
public static string GetSkipReason(this ITest test)
{
if (test.Properties.ContainsKey(PropertyNames.SkipReason))
return (string)test.Properties.Get(PropertyNames.SkipReason);
return null;
}
public static string GetParentId(this ITest test)
{
if (test.Parent != null)
return test.Parent.Id;
return null;
}
public static string GetParentFullName(this ITest test)
{
if (test.Parent != null)
return test.Parent.FullName;
return null;
}
public static string GetParentUniqueName(this ITest test)
{
if (test.Parent != null)
return GetUniqueName(test.Parent);
return null;
}
internal static string GetFullName(string testFullName, int childIndex)
{
return childIndex != -1 ? GetIndexedTestCaseName(testFullName, childIndex) : testFullName;
}
private static string GetIndexedTestCaseName(string fullName, int index)
{
var generatedTestSuffix = " GeneratedTestCase" + index;
if (fullName.EndsWith(")"))
{
// Test names from generated TestCaseSource look like Test(TestCaseSourceType)
// This inserts a unique test case index in the name, so that it becomes Test(TestCaseSourceType GeneratedTestCase0)
return fullName.Substring(0, fullName.Length - 1) + generatedTestSuffix + fullName[fullName.Length - 1];
}
// In some cases there can be tests with duplicate names generated in other ways and they won't have () in their name
// We just append a suffix at the end of the name in that case
return fullName + generatedTestSuffix;
}
}
}
|