Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#endif

using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices;
using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Helpers;
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using Microsoft.VisualStudio.TestTools.UnitTesting;

Expand Down Expand Up @@ -145,41 +146,7 @@ internal static bool MatchReturnType(MethodInfo method, Type returnType)
/// <param name="owningType">The reflected type that owns <paramref name="categoryAttributeProvider"/>.</param>
/// <returns>Categories defined.</returns>
internal static string[] GetTestCategories(MemberInfo categoryAttributeProvider, Type owningType)
{
Attribute[] methodAttributes = GetCustomAttributesCached(categoryAttributeProvider);
Attribute[] typeAttributes = GetCustomAttributesCached(owningType);
Attribute[] assemblyAttributes = GetCustomAttributesCached(owningType.Assembly);

// Avoid LINQ iterator allocations by iterating the cached attribute arrays directly.
// This follows the same allocation-free pattern used by GetTestPropertiesAsTraits.
List<string>? categories = null;

foreach (Attribute attribute in methodAttributes)
{
if (attribute is TestCategoryBaseAttribute categoryAttr)
{
(categories ??= []).AddRange(categoryAttr.TestCategories);
}
}

foreach (Attribute attribute in typeAttributes)
{
if (attribute is TestCategoryBaseAttribute categoryAttr)
{
(categories ??= []).AddRange(categoryAttr.TestCategories);
}
}

foreach (Attribute attribute in assemblyAttributes)
{
if (attribute is TestCategoryBaseAttribute categoryAttr)
{
(categories ??= []).AddRange(categoryAttr.TestCategories);
}
}

return categories is null ? [] : [.. categories];
}
=> PlatformServiceProvider.Instance.ReflectionOperations.GetTestCategories(categoryAttributeProvider, owningType);

/// <summary>
/// Gets the parallelization level set on an assembly.
Expand Down Expand Up @@ -232,52 +199,7 @@ internal static bool IsDoNotParallelizeSet(Assembly assembly)
/// <param name="testPropertyProvider">The member to inspect.</param>
/// <returns>List of traits.</returns>
internal static Trait[] GetTestPropertiesAsTraits(MethodInfo testPropertyProvider)
{
Attribute[] attributesFromMethod = GetCustomAttributesCached(testPropertyProvider);
Attribute[] attributesFromClass = testPropertyProvider.ReflectedType is { } testClass ? GetCustomAttributesCached(testClass) : [];
int countTestPropertyAttribute = 0;
foreach (Attribute attribute in attributesFromMethod)
{
if (attribute is TestPropertyAttribute)
{
countTestPropertyAttribute++;
}
}

foreach (Attribute attribute in attributesFromClass)
{
if (attribute is TestPropertyAttribute)
{
countTestPropertyAttribute++;
}
}

if (countTestPropertyAttribute == 0)
{
// This is the common case that we optimize for. This method used to be an iterator (uses yield return) which is allocating unnecessarily in common cases.
return [];
}

var traits = new Trait[countTestPropertyAttribute];
int index = 0;
foreach (Attribute attribute in attributesFromMethod)
{
if (attribute is TestPropertyAttribute testProperty)
{
traits[index++] = new Trait(testProperty.Name, testProperty.Value);
}
}

foreach (Attribute attribute in attributesFromClass)
{
if (attribute is TestPropertyAttribute testProperty)
{
traits[index++] = new Trait(testProperty.Name, testProperty.Value);
}
}

return traits;
}
=> PlatformServiceProvider.Instance.ReflectionOperations.GetTestPropertiesAsTraits(testPropertyProvider);

/// <summary>
/// Get attribute defined on a method which is of given type of subtype of given type.
Expand Down
Loading