Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor GetTestFromMethod #4279

Merged
merged 6 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
38 changes: 24 additions & 14 deletions src/Adapter/MSTest.TestAdapter/Discovery/TypeEnumerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,29 +201,39 @@ internal UnitTestElement GetTestFromMethod(MethodInfo method, bool isDeclaredInT

testElement.Traits = traits.ToArray();

if (_reflectHelper.GetFirstDerivedAttributeOrDefault<CssIterationAttribute>(method, inherit: true) is CssIterationAttribute cssIteration)
{
testElement.CssIteration = cssIteration.CssIteration;
}
Attribute[] attributes = _reflectHelper.GetCustomAttributesCached(method, inherit: true);
Evangelink marked this conversation as resolved.
Show resolved Hide resolved
TestMethodAttribute? testMethodAttribute = null;

if (_reflectHelper.GetFirstDerivedAttributeOrDefault<CssProjectStructureAttribute>(method, inherit: true) is CssProjectStructureAttribute cssProjectStructure)
// Backward looping for backcompat. This used to be calls to _reflectHelper.GetFirstDerivedAttributeOrDefault
// So, to make sure the first attribute always wins, we loop from end to start.
for (int i = attributes.Length - 1; i >= 0; i--)
{
testElement.CssProjectStructure = cssProjectStructure.CssProjectStructure;
}

if (_reflectHelper.GetFirstDerivedAttributeOrDefault<DescriptionAttribute>(method, inherit: true) is DescriptionAttribute descriptionAttribute)
{
testElement.Description = descriptionAttribute.Description;
if (attributes[i] is TestMethodAttribute tma)
{
testMethodAttribute = tma;
}
else if (attributes[i] is CssIterationAttribute cssIteration)
{
testElement.CssIteration = cssIteration.CssIteration;
}
else if (attributes[i] is CssProjectStructureAttribute cssProjectStructure)
{
testElement.CssProjectStructure = cssProjectStructure.CssProjectStructure;
}
else if (attributes[i] is DescriptionAttribute descriptionAttribute)
{
testElement.Description = descriptionAttribute.Description;
}
}

WorkItemAttribute[] workItemAttributes = _reflectHelper.GetDerivedAttributes<WorkItemAttribute>(method, inherit: true).ToArray();
if (workItemAttributes.Length != 0)
IEnumerable<WorkItemAttribute> workItemAttributes = attributes.OfType<WorkItemAttribute>();
if (workItemAttributes.Any())
{
testElement.WorkItemIds = workItemAttributes.Select(x => x.Id.ToString(CultureInfo.InvariantCulture)).ToArray();
}

// get DisplayName from TestMethodAttribute (or any inherited attribute)
TestMethodAttribute? testMethodAttribute = _reflectHelper.GetFirstDerivedAttributeOrDefault<TestMethodAttribute>(method, inherit: true);
DebugEx.Assert(testMethodAttribute is not null, "Expected to find a 'TestMethod' attribute.");
testElement.DisplayName = testMethodAttribute?.DisplayName ?? method.Name;

return testElement;
Expand Down
2 changes: 1 addition & 1 deletion src/Adapter/MSTest.TestAdapter/Helpers/ReflectHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ internal virtual IEnumerable<Trait> GetTestPropertiesAsTraits(MemberInfo testPro
/// <param name="attributeProvider">The member to inspect.</param>
/// <param name="inherit">Look at inheritance chain.</param>
/// <returns>attributes defined.</returns>
private Attribute[] GetCustomAttributesCached(ICustomAttributeProvider attributeProvider, bool inherit)
internal Attribute[] GetCustomAttributesCached(ICustomAttributeProvider attributeProvider, bool inherit)
{
// If the information is cached, then use it otherwise populate the cache using
// the reflection APIs.
Expand Down
Loading