Skip to content

Commit ffcfeec

Browse files
committed
add tests
1 parent 384a75e commit ffcfeec

File tree

4 files changed

+89
-2
lines changed

4 files changed

+89
-2
lines changed

tests/Smdn.Reflection.ReverseGenerating.ListApi.Core/Smdn.Reflection.ReverseGenerating.ListApi.Core.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ SPDX-License-Identifier: MIT
2525
<TestProjects Include="$(TestAssemblyRootDirectory)\LibA\LibA.csproj" />
2626
<TestProjects Include="$(TestAssemblyRootDirectory)\LibB\LibB.csproj" />
2727
<TestProjects Include="$(TestAssemblyRootDirectory)\LibReferencedAssemblies1\LibReferencedAssemblies1.csproj" />
28+
<TestProjects Include="$(TestAssemblyRootDirectory)\LibPackageReferences1\LibPackageReferences1.csproj" />
2829
</ItemGroup>
2930

3031
<!--

tests/Smdn.Reflection.ReverseGenerating.ListApi.Core/Smdn.Reflection.ReverseGenerating.ListApi/AssemblyLoader.cs

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System;
44
using System.Linq;
55
using System.IO;
6+
using System.Reflection;
67
using Microsoft.Extensions.DependencyInjection;
78
using Microsoft.Extensions.Logging;
89
using Microsoft.Extensions.Logging.Console;
@@ -175,7 +176,7 @@ public void UsingAssembly_FromStream(bool loadIntoReflectionOnlyContext, string
175176
[TestCase(true, "net7.0")]
176177
[TestCase(false, "net7.0")]
177178
#endif
178-
public void UsingAssembly_ResolveDependency(bool loadIntoReflectionOnlyContext, string targetFrameworkMoniker)
179+
public void UsingAssembly_ResolveDependency_ProjectReference(bool loadIntoReflectionOnlyContext, string targetFrameworkMoniker)
179180
{
180181
var assemblyFile = new FileInfo(
181182
TestAssemblyInfo.TestAssemblyPaths.First(f => f.Contains(targetFrameworkMoniker) && f.Contains("LibB.dll"))
@@ -236,7 +237,7 @@ public void UsingAssembly_ResolveDependency(bool loadIntoReflectionOnlyContext,
236237
[TestCase(true, "net7.0")]
237238
[TestCase(false, "net7.0")]
238239
#endif
239-
public void UsingAssembly_FromStream_ResolveDependency(bool loadIntoReflectionOnlyContext, string targetFrameworkMoniker)
240+
public void UsingAssembly_FromStream_ResolveDependency_ProjectReference(bool loadIntoReflectionOnlyContext, string targetFrameworkMoniker)
240241
{
241242
var assemblyFile = new FileInfo(
242243
TestAssemblyInfo.TestAssemblyPaths.First(f => f.Contains(targetFrameworkMoniker) && f.Contains("LibB.dll"))
@@ -286,6 +287,73 @@ public void UsingAssembly_FromStream_ResolveDependency(bool loadIntoReflectionOn
286287
GC.WaitForPendingFinalizers();
287288
}
288289

290+
Assert.IsTrue(unloaded, nameof(unloaded));
291+
}
292+
293+
#if NETCOREAPP3_1_OR_GREATER || NET6_0_OR_GREATER
294+
[TestCase(true, "netstandard2.1")]
295+
[TestCase(false, "netstandard2.1")]
296+
#endif
297+
#if NET6_0_OR_GREATER
298+
[TestCase(true, "net6.0")]
299+
[TestCase(false, "net6.0")]
300+
#endif
301+
#if NET7_0_OR_GREATER
302+
[TestCase(true, "net7.0")]
303+
[TestCase(false, "net7.0")]
304+
#endif
305+
public void UsingAssembly_ResolveDependency_PackageReference(bool loadIntoReflectionOnlyContext, string targetFrameworkMoniker)
306+
{
307+
var assemblyFile = new FileInfo(
308+
TestAssemblyInfo.TestAssemblyPaths.First(f => f.Contains(targetFrameworkMoniker) && f.Contains("LibPackageReferences1.dll"))
309+
);
310+
311+
var result = AssemblyLoader.UsingAssembly(
312+
assemblyFile,
313+
loadIntoReflectionOnlyContext: loadIntoReflectionOnlyContext,
314+
arg: assemblyFile,
315+
(assm, arg) => {
316+
Assert.AreSame(arg, assemblyFile, nameof(arg));
317+
318+
Assert.IsNotNull(assm, nameof(assm));
319+
Assert.AreEqual(arg.FullName, assm.Location, nameof(assm.Location));
320+
321+
Assert.DoesNotThrow(() => assm.GetExportedTypes(), nameof(assm.GetExportedTypes));
322+
323+
return assm
324+
.GetType("C")
325+
?.GetMethod("M", BindingFlags.Public | BindingFlags.Static)
326+
?.GetParameters()
327+
?[0]
328+
?.ParameterType
329+
?.FullName;
330+
},
331+
context: out var context,
332+
logger: logger
333+
);
334+
335+
Assert.IsNotNull(result, nameof(result));
336+
Assert.AreEqual(result, "Microsoft.Extensions.Logging.ILogger", nameof(result));
337+
338+
if (loadIntoReflectionOnlyContext) {
339+
Assert.IsNull(context, nameof(context));
340+
return;
341+
}
342+
343+
Assert.IsNotNull(context, nameof(context));
344+
345+
var unloaded = false;
346+
347+
for (var i = 0; i < 10; i++) {
348+
if (!context!.IsAlive) {
349+
unloaded = true;
350+
break;
351+
}
352+
353+
GC.Collect();
354+
GC.WaitForPendingFinalizers();
355+
}
356+
289357
Assert.IsTrue(unloaded, nameof(unloaded));
290358
}
291359
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#nullable enable
2+
3+
public static class C {
4+
public static void M(Microsoft.Extensions.Logging.ILogger? logger = null) { }
5+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<TargetFrameworks>net6.0;netstandard2.1</TargetFrameworks>
4+
<!-- do not copy assemblies from the NuGet packages to the output directory -->
5+
<CopyLocalLockFileAssemblies>false</CopyLocalLockFileAssemblies>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0" />
10+
</ItemGroup>
11+
12+
<Import Project="..\..\..\src\Sdk.net7.0.props" />
13+
</Project>

0 commit comments

Comments
 (0)