Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ltrzesniewski committed Mar 20, 2024
1 parent bf48fe6 commit e1eb1a5
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 14 deletions.
13 changes: 13 additions & 0 deletions src/InlineIL.Tests.InjectedAssembly/InjectedGenericType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.Diagnostics.CodeAnalysis;

namespace InlineIL.Tests.InjectedAssembly;

[SuppressMessage("ReSharper", "UnusedTypeParameter")]
public class InjectedGenericType<T>
{
}

[SuppressMessage("ReSharper", "UnusedTypeParameter")]
public class InjectedGenericType<T1, T2>
{
}
18 changes: 18 additions & 0 deletions src/InlineIL.Tests.InvalidAssemblyToProcess/TypeRefTestCases.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,22 @@ public void UseMethodsFromDifferentVersionsOfDll()

Pop();
}

public void InvalidInjectedDllFile()
{
Ldtoken(
#pragma warning disable CS0618
TypeRef.FromDllFile("InjectedDllDir/DoesNotExist.dll", "SomeType")
#pragma warning restore CS0618
);
}

public void InvalidInjectedTypeName()
{
Ldtoken(
#pragma warning disable CS0618
TypeRef.FromDllFile("InjectedDllDir/InlineIL.Tests.InjectedAssembly.Alternative.dll", "DoesNotExist")
#pragma warning restore CS0618
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,22 @@

namespace InlineIL.Tests.UnverifiableAssemblyToProcess;

#pragma warning disable CS0618

[SuppressMessage("ReSharper", "UnusedType.Global")]
[SuppressMessage("ReSharper", "UnusedMember.Global")]
public class TypeRefTestCases
{
private const string _injectedAssemblyPath = "InjectedDllDir/InlineIL.Tests.InjectedAssembly.dll";

public int UseInjectedDll()
{
Ldc_I4(40);
Ldc_I4_2();

Call(
MethodRef.Method(
#pragma warning disable CS0618
TypeRef.FromDllFile(
"InjectedDllDir/InlineIL.Tests.InjectedAssembly.dll",
"InlineIL.Tests.InjectedAssembly.InjectedType"
),
#pragma warning restore CS0618
TypeRef.FromDllFile(_injectedAssemblyPath, "InlineIL.Tests.InjectedAssembly.InjectedType"),
"AddInt32"
)
);
Expand All @@ -31,12 +30,38 @@ public int UseInjectedDll()
public RuntimeTypeHandle ReturnInjectedTypeSpec()
{
Ldtoken(
#pragma warning disable CS0618
TypeRef.FromDllFile(
"InjectedDllDir/InlineIL.Tests.InjectedAssembly.dll",
"InlineIL.Tests.InjectedAssembly.InjectedType"
).MakeArrayType()
#pragma warning restore CS0618
TypeRef.FromDllFile(_injectedAssemblyPath, "InlineIL.Tests.InjectedAssembly.InjectedType")
.MakeArrayType()
);

return IL.Return<RuntimeTypeHandle>();
}

public RuntimeTypeHandle ReturnInjectedGenericTypeSpec()
{
Ldtoken(
TypeRef.FromDllFile(_injectedAssemblyPath, "InlineIL.Tests.InjectedAssembly.InjectedGenericType`1")
);

return IL.Return<RuntimeTypeHandle>();
}

public RuntimeTypeHandle ReturnInjectedGenericTypeSpec2()
{
Ldtoken(
TypeRef.FromDllFile(_injectedAssemblyPath, "InlineIL.Tests.InjectedAssembly.InjectedGenericType`2")
);

return IL.Return<RuntimeTypeHandle>();
}

public RuntimeTypeHandle ReturnInjectedConstructedGenericTypeSpec()
{
Ldtoken(
TypeRef.FromDllFile(_injectedAssemblyPath, "InlineIL.Tests.InjectedAssembly.InjectedGenericType`1")
.MakeGenericType(
TypeRef.FromDllFile(_injectedAssemblyPath, "InlineIL.Tests.InjectedAssembly.InjectedType")
)
);

return IL.Return<RuntimeTypeHandle>();
Expand Down
6 changes: 4 additions & 2 deletions src/InlineIL.Tests/Weaving/TestModuleWeaver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ private void CopyInjectedAssemblies()
{
ProjectDirectoryPath.ShouldNotBeNull();

var sourceDir = Path.GetDirectoryName(typeof(AssemblyToProcessFixture).Assembly.Location)!;

var targetDir = Path.Combine(ProjectDirectoryPath, "InjectedDllDir");
Directory.CreateDirectory(targetDir);

Expand All @@ -25,8 +27,8 @@ private void CopyInjectedAssemblies()
void Copy(string fileName)
{
File.Copy(
Path.Combine(Path.GetDirectoryName(typeof(AssemblyToProcessFixture).Assembly.Location)!, fileName),
Path.Combine(targetDir, Path.GetFileName(fileName)),
Path.Combine(sourceDir, fileName),
Path.Combine(targetDir, fileName),
true
);
}
Expand Down
33 changes: 33 additions & 0 deletions src/InlineIL.Tests/Weaving/TypeRefTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,27 @@ public void should_return_type_spec_from_injected_type()
Type.GetTypeFromHandle(result).ShouldEqual(typeof(InjectedType[]));
}

[Fact]
public void should_return_generic_type_spec_from_injected_type()
{
var result = (RuntimeTypeHandle)GetUnverifiableInstance().ReturnInjectedGenericTypeSpec();
Type.GetTypeFromHandle(result).ShouldEqual(typeof(InjectedGenericType<>));
}

[Fact]
public void should_return_generic_type_spec_from_injected_type_2()
{
var result = (RuntimeTypeHandle)GetUnverifiableInstance().ReturnInjectedGenericTypeSpec2();
Type.GetTypeFromHandle(result).ShouldEqual(typeof(InjectedGenericType<,>));
}

[Fact]
public void should_return_constructed_generic_type_spec_from_injected_type()
{
var result = (RuntimeTypeHandle)GetUnverifiableInstance().ReturnInjectedConstructedGenericTypeSpec();
Type.GetTypeFromHandle(result).ShouldEqual(typeof(InjectedGenericType<InjectedType>));
}

[Fact]
public void should_use_methods_from_injected_type_and_referenced_type()
{
Expand All @@ -343,6 +364,18 @@ public void should_use_methods_from_injected_type_and_referenced_type()
calls[0].Operand.ShouldBe<MethodReference>().Name.ShouldEqual("AddInt32");
calls[1].Operand.ShouldBe<MethodReference>().Name.ShouldEqual("MultiplyInt32");
}

[Fact]
public void should_report_dll_file_not_found()
{
ShouldHaveError("InvalidInjectedDllFile").ShouldContain("Could not read assembly");
}

[Fact]
public void should_report_type_in_dll_file_not_found()
{
ShouldHaveError("InvalidInjectedTypeName").ShouldContain("Could not find type 'DoesNotExist'");
}
}

#if NET
Expand Down

0 comments on commit e1eb1a5

Please sign in to comment.