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

Use reflection provider for class constructors #4079

Merged
merged 3 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
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
41 changes: 30 additions & 11 deletions src/Adapter/MSTest.TestAdapter/Execution/TypeCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -260,24 +260,43 @@ private static bool TryGetUnescapedManagedTypeName(TestMethod testMethod, [NotNu
private TestClassInfo CreateClassInfo(Type classType, TestMethod testMethod)
{
IEnumerable<ConstructorInfo> constructors = PlatformServiceProvider.Instance.ReflectionOperations.GetDeclaredConstructors(classType);
ConstructorInfo? constructor = constructors.FirstOrDefault(ctor => ctor.GetParameters().Length == 0 && ctor.IsPublic);
bool isParameterLessConstructor;
(ConstructorInfo CtorInfo, bool IsParameterless)? selectedConstructor = null;

if (classType.GetConstructor([typeof(TestContext)]) is { } testContextCtor)
foreach (ConstructorInfo ctor in constructors)
{
constructor = testContextCtor;
isParameterLessConstructor = false;
}
else if (classType.GetConstructor([]) is { } parameterLessCtor)
{
constructor = parameterLessCtor;
isParameterLessConstructor = true;
if (!ctor.IsPublic)
{
continue;
}

ParameterInfo[] parameters = ctor.GetParameters();

// There are just 2 ctor shapes that we know, so the code is quite simple,
// but if we add more, add a priority to the search, and short-circuit this search so we only iterate
// through the collection once, to avoid re-allocating GetParameters multiple times.
if (parameters.Length == 1 && parameters[0].ParameterType == typeof(TestContext))
{
selectedConstructor = (ctor, IsParameterless: false);

// This is the preferred constructor, no point in searching for more.
break;
}

if (parameters.Length == 0)
{
// Otherwise take the first parameterless constructor we can find.
selectedConstructor ??= (ctor, IsParameterless: true);
}
}
else

if (selectedConstructor is null)
{
throw new TypeInspectionException(string.Format(CultureInfo.CurrentCulture, Resource.UTA_NoValidConstructor, testMethod.FullClassName));
}

ConstructorInfo constructor = selectedConstructor.Value.CtorInfo;
bool isParameterLessConstructor = selectedConstructor.Value.IsParameterless;

PropertyInfo? testContextProperty = ResolveTestContext(classType);

TestAssemblyInfo assemblyInfo = GetAssemblyInfo(classType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,9 @@ public void RunTestsForTestShouldNotRunTestsInParallelWhenDisabledFromRunsetting
var originalReflectionOperation = new ReflectionOperations2();
var originalFileOperation = new FileOperations();

testablePlatformService.MockReflectionOperations.Setup(ro => ro.GetDeclaredConstructors(It.IsAny<Type>()))
.Returns((Type classType) => originalReflectionOperation.GetDeclaredConstructors(classType));

testablePlatformService.MockReflectionOperations.Setup(
ro => ro.GetCustomAttributes(It.IsAny<Assembly>(), It.IsAny<Type>())).
Returns((Assembly asm, Type type) => type.FullName.Equals(typeof(ParallelizeAttribute).FullName, StringComparison.Ordinal)
Expand Down Expand Up @@ -632,6 +635,9 @@ public void RunTestsForTestShouldNotRunTestsInParallelWhenDisabledFromSource()
var originalReflectionOperation = new ReflectionOperations2();
var originalFileOperation = new FileOperations();

testablePlatformService.MockReflectionOperations.Setup(ro => ro.GetDeclaredConstructors(It.IsAny<Type>()))
.Returns((Type classType) => originalReflectionOperation.GetDeclaredConstructors(classType));

testablePlatformService.MockReflectionOperations.Setup(
ro => ro.GetCustomAttributes(It.IsAny<Assembly>(), It.IsAny<Type>())).
Returns((Assembly asm, Type type) => type.FullName.Equals(typeof(DoNotParallelizeAttribute).FullName, StringComparison.Ordinal)
Expand Down Expand Up @@ -730,6 +736,9 @@ public void RunTestsForTestShouldPreferParallelSettingsFromRunSettingsOverAssemb
var originalReflectionOperation = new ReflectionOperations2();
var originalFileOperation = new FileOperations();

testablePlatformService.MockReflectionOperations.Setup(ro => ro.GetDeclaredConstructors(It.IsAny<Type>()))
.Returns((Type classType) => originalReflectionOperation.GetDeclaredConstructors(classType));

testablePlatformService.MockReflectionOperations.Setup(
ro => ro.GetCustomAttributes(It.IsAny<Assembly>(), It.IsAny<Type>())).
Returns((Assembly asm, Type type) => type.FullName.Equals(typeof(ParallelizeAttribute).FullName, StringComparison.Ordinal)
Expand Down
Loading