Skip to content

remove redudnant cast to IReadOnlyList and add a not null ckeck in AssemblyEnumeratorTests #5402

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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public void GetTypesShouldReturnSetOfDefinedTypes()
// Setup mocks
mockAssembly.Setup(a => a.GetTypes()).Returns(expectedTypes);

IReadOnlyList<Type> types = AssemblyEnumerator.GetTypes(mockAssembly.Object, string.Empty, _warnings);
Type[] types = AssemblyEnumerator.GetTypes(mockAssembly.Object, string.Empty, _warnings);
Verify(expectedTypes.SequenceEqual(types));
}

Expand All @@ -116,7 +116,7 @@ public void GetTypesShouldReturnReflectionTypeLoadExceptionTypesOnException()
// Setup mocks
mockAssembly.Setup(a => a.GetTypes()).Throws(new ReflectionTypeLoadException(reflectedTypes, null));

IReadOnlyList<Type> types = AssemblyEnumerator.GetTypes(mockAssembly.Object, string.Empty, _warnings);
Type[] types = AssemblyEnumerator.GetTypes(mockAssembly.Object, string.Empty, _warnings);

Verify(types is not null);
Verify(reflectedTypes.Equals(types));
Expand All @@ -131,8 +131,9 @@ public void GetTypesShouldLogWarningsWhenReflectionFailsWithLoaderExceptions()
mockAssembly.Setup(a => a.GetTypes()).Throws(new ReflectionTypeLoadException(null, exceptions));
mockAssembly.Setup(a => a.GetTypes()).Throws(new ReflectionTypeLoadException(null, exceptions));

IReadOnlyList<Type> types = AssemblyEnumerator.GetTypes(mockAssembly.Object, "DummyAssembly", _warnings);
Type[] types = AssemblyEnumerator.GetTypes(mockAssembly.Object, "DummyAssembly", _warnings);

Verify(types is not null);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assert is failing. It's because the mocking two lines above is throwing ReflectionTypeLoadException passing null array.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NOTE: The failure is .NET Framework only, due to difference in ReflectionTypeLoadException implementation between .NET Core and .NET Framework.

When the constructor in .NET Core receives null, it sets the property to empty array:

https://github.com/dotnet/runtime/blob/0e94dca5b3203cdb9077dd543d87f1a69ff7eb40/src/libraries/System.Private.CoreLib/src/System/Reflection/ReflectionTypeLoadException.cs#L23

This is not the case for .NET Framework.

Verify(_warnings.Count == 1);
Verify(_warnings.ToList().Contains(
string.Format(CultureInfo.CurrentCulture, Resource.TypeLoadFailed, "DummyAssembly", "System.Exception: DummyLoaderException\r\n")));
Expand Down
Loading