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

Issue/17 #18

Merged
merged 8 commits into from
Nov 16, 2021
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
24 changes: 16 additions & 8 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,18 @@ jobs:
steps:
- uses: actions/checkout@v2

- name: Setup .NET Core 3.1
uses: actions/[email protected]
with:
dotnet-version: 3.1.x
- name: Setup .NET 5
uses: actions/[email protected]
with:
dotnet-version: 5.0.x
- name: Setup .NET 6
uses: actions/[email protected]
with:
dotnet-version: 6.0.x

- name: Restore dependencies
run: dotnet restore
Expand Down Expand Up @@ -45,15 +53,15 @@ jobs:
# files: coverage.cobertura.xml
# verbose: true

- name: Run Stryker.NET
run: cd ./Shimterface.Tests/; dotnet stryker --reporters "['cleartext','html','dashboard']" --dashboard-api-key ${{ secrets.STRYKER_DASHBOARD }} --dashboard-project github.com/IFYates/Shimterface --dashboard-version $GITHUB_REF
- name: Upload mutation report
uses: actions/[email protected]
with:
name: mutation-report.html
path: ./**/mutation-report.html
# - name: Run Stryker.NET
# run: cd ./Shimterface.Tests/; dotnet stryker --project Shimterface.Standard.Tests.csproj --reporters "['cleartext','html','dashboard']" --dashboard-api-key ${{ secrets.STRYKER_DASHBOARD }} --dashboard-project github.com/IFYates/Shimterface --dashboard-version $GITHUB_REF
# - name: Upload mutation report
# uses: actions/[email protected]
# with:
# name: mutation-report.html
# path: ./**/mutation-report.html

- name: Publish package
uses: actions/upload-artifact@v2
with:
path: ./**/Shimterface.Standard.*.nupkg
path: ./**/Shimterface.Standard.*.nupkg
4 changes: 2 additions & 2 deletions Shimterface.Standard.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29326.143
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{D260D65D-0F00-4D2C-9E72-38EC9D2E3F41}"
ProjectSection(SolutionItems) = preProject
Expand Down
17 changes: 17 additions & 0 deletions Shimterface.Tests/GenericConstructorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public interface IInstanceInterface<T>
}
public interface IFactoryInterface
{
[ConstructorShim(typeof(TestClass<>))]
IInstanceInterface<T> Create<T>();
[ConstructorShim(typeof(TestClass<>))]
IInstanceInterface<T> Create<T>(T value);
[ConstructorShim(typeof(TestClass<>))]
Expand All @@ -31,6 +33,10 @@ public class TestClass<T>
public string Text { get; set; }
public int Count { get; set; }

public TestClass()
{
Count = 0;
}
public TestClass(T value)
{
Value = value;
Expand All @@ -53,6 +59,17 @@ public interface ITest
object Exec();
}

[TestMethod]
public void Can_shim_to_constructor_without_args()
{
var shim = ShimBuilder.Create<IFactoryInterface>();

var inst = shim.Create<string>();

Assert.AreEqual(0, inst.Count);
Assert.IsInstanceOfType(((IShim)inst).Unshim(), typeof(TestClass<string>));
}

[TestMethod]
public void Can_shim_to_constructor()
{
Expand Down
22 changes: 0 additions & 22 deletions Shimterface.Tests/Internal/ILBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,6 @@ namespace Shimterface.Internal.Tests
[TestClass]
public class ILBuilderTests
{
[TestMethod]
[DataRow((byte)0, 1)]
[DataRow((byte)1, 1)]
[DataRow((byte)2, 1)]
[DataRow((byte)3, 1)]
[DataRow((byte)4, 2)]
[DataRow((byte)5, 2)]
public void Ldarg__Provides_efficient_bytecode(byte input, int expOffset)
{
// Arrange
var asm = AssemblyBuilder.DefineDynamicAssembly(new AssemblyName("Shimterface.Tests.dynamic"), AssemblyBuilderAccess.Run);
var mod = asm.DefineDynamicModule("Shimterface.Tests.dynamic");
var tb = mod.DefineType($"TestClass", TypeAttributes.Public | TypeAttributes.Class | TypeAttributes.AutoClass | TypeAttributes.AnsiClass | TypeAttributes.BeforeFieldInit | TypeAttributes.AutoLayout, null, null);

// Act
var impl = tb.DefinePublicMethod("TestMethod", typeof(bool), new List<Type> { typeof(string), typeof(int) });
ILBuilder.Ldarg(impl, input);

// Assert
Assert.AreEqual(expOffset, impl.ILOffset);
}

[TestMethod]
public void DefinePublicMethod__With_paramTypes__Creates_method()
{
Expand Down
106 changes: 106 additions & 0 deletions Shimterface.Tests/Internal/ILGeneratorExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Reflection.Emit;

namespace Shimterface.Internal.Tests
{
[TestClass]
public class ILGeneratorExtensionsTests
{
private static ILGenerator getGenerator()
{
var asm = AssemblyBuilder.DefineDynamicAssembly(new AssemblyName("Shimterface.Tests.dynamic"), AssemblyBuilderAccess.Run);
var mod = asm.DefineDynamicModule("Shimterface.Tests.dynamic");
var tb = mod.DefineType($"TestClass", TypeAttributes.Public | TypeAttributes.Class | TypeAttributes.AutoClass | TypeAttributes.AnsiClass | TypeAttributes.BeforeFieldInit | TypeAttributes.AutoLayout, null, null);
return tb.DefinePublicMethod("TestMethod", typeof(bool), new List<Type> { typeof(string), typeof(int) });
}

[TestMethod]
[DataRow((byte)0, 1)]
[DataRow((byte)1, 1)]
[DataRow((byte)2, 1)]
[DataRow((byte)3, 1)]
[DataRow((byte)4, 2)]
[DataRow((byte)5, 2)]
public void Ldarg__Provides_efficient_bytecode(byte input, int expOffset)
{
// Arrange
var impl = getGenerator();

// Act
ILGeneratorExtensions.Ldarg(impl, input);

// Assert
Assert.AreEqual(expOffset, impl.ILOffset);
}

[TestMethod]
[DataRow((byte)0, 1)]
[DataRow((byte)1, 1)]
[DataRow((byte)2, 1)]
[DataRow((byte)3, 1)]
[DataRow((byte)4, 1)]
[DataRow((byte)5, 1)]
[DataRow((byte)6, 1)]
[DataRow((byte)7, 1)]
[DataRow((byte)8, 1)]
#if NETCOREAPP3_1
[DataRow((byte)9, 5)]
[DataRow((byte)10, 5)]
#else
[DataRow((byte)9, 2)]
[DataRow((byte)10, 2)]
#endif
public void Ldc_I4__Provides_efficient_bytecode(byte input, int expOffset)
{
// Arrange
var impl = getGenerator();

// Act
ILGeneratorExtensions.Ldc_I4(impl, input);

// Assert
Assert.AreEqual(expOffset, impl.ILOffset);
}

[TestMethod]
[DataRow((byte)0, 1)]
[DataRow((byte)1, 1)]
[DataRow((byte)2, 1)]
[DataRow((byte)3, 1)]
[DataRow((byte)4, 6)]
[DataRow((byte)5, 6)]
public void Ldloc__Provides_efficient_bytecode(byte input, int expOffset)
{
// Arrange
var impl = getGenerator();

// Act
ILGeneratorExtensions.Ldloc(impl, input);

// Assert
Assert.AreEqual(expOffset, impl.ILOffset);
}

[TestMethod]
[DataRow((byte)0, 1)]
[DataRow((byte)1, 1)]
[DataRow((byte)2, 1)]
[DataRow((byte)3, 1)]
[DataRow((byte)4, 6)]
[DataRow((byte)5, 6)]
public void Stloc__Provides_efficient_bytecode(byte input, int expOffset)
{
// Arrange
var impl = getGenerator();

// Act
ILGeneratorExtensions.Stloc(impl, input);

// Assert
Assert.AreEqual(expOffset, impl.ILOffset);
}
}
}
16 changes: 16 additions & 0 deletions Shimterface.Tests/Shimterface - Backup.Standard.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net5.0;net6.0</TargetFrameworks>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<RootNamespace>Shimterface.Tests</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.7" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.7" />
<PackageReference Include="Moq" Version="4.16.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Shimterface\Shimterface.Standard.csproj" />
</ItemGroup>
</Project>
4 changes: 2 additions & 2 deletions Shimterface.Tests/Shimterface.Standard.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<TargetFrameworks>netcoreapp3.1;net5.0;net6.0</TargetFrameworks>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<RootNamespace>Shimterface.Tests</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.7" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.7" />
<PackageReference Include="Moq" Version="4.16.1" />
Expand Down
40 changes: 10 additions & 30 deletions Shimterface/Internal/ILBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,6 @@ namespace Shimterface.Internal
{
internal static class ILBuilder
{
public static void Ldarg(this ILGenerator impl, byte num)
{
switch (num)
{
case 0:
impl.Emit(OpCodes.Ldarg_0);
break;
case 1:
impl.Emit(OpCodes.Ldarg_1);
break;
case 2:
impl.Emit(OpCodes.Ldarg_2);
break;
case 3:
impl.Emit(OpCodes.Ldarg_3);
break;
default:
impl.Emit(OpCodes.Ldarg_S, num);
break;
}
}

private static bool resolveIfInstance(bool isStatic, ILGenerator impl, FieldInfo? instField)
{
if (isStatic || instField == null)
Expand Down Expand Up @@ -175,7 +153,7 @@ public static void EmitTypeUnshim(this ILGenerator impl, Type shimType, Type rea

public static void WrapConstructor(this TypeBuilder tb, ShimBinding binding, ConstructorInfo constrInfo)
{
var factory = tb.DefinePublicMethod(binding.InterfaceMethod, out var args);
var factory = tb.DefinePublicMethod(binding.InterfaceMethod, out var argTypes);
var impl = factory.GetILGenerator();
var genericParams = factory.GetGenericArguments();

Expand All @@ -184,21 +162,23 @@ public static void WrapConstructor(this TypeBuilder tb, ShimBinding binding, Con
// Build args array
var pars = binding.InterfaceMethod.GetParameters();
var argsArr = impl.DeclareLocal(typeof(object[]));
impl.Emit(OpCodes.Ldc_I4, args.Length);
impl.Ldc_I4(argTypes.Length);
impl.Emit(OpCodes.Newarr, typeof(object));
for (var i = 0; i < args.Length; ++i)
for (var i = 0; i < argTypes.Length; ++i)
{
impl.Emit(OpCodes.Dup);
impl.Emit(OpCodes.Ldc_I4, i);
impl.Ldc_I4(i);
impl.Ldarg((byte)(i + 1));
impl.Emit(OpCodes.Box, args[i]);
impl.Emit(OpCodes.Box, argTypes[i]);
impl.Emit(OpCodes.Stelem_Ref);
}
impl.Emit(OpCodes.Stloc, argsArr.LocalIndex);
impl.Stloc(argsArr.LocalIndex);

// Build target type
impl.Emit(OpCodes.Ldtoken, constrInfo.DeclaringType.RebuildGenericType(genericParams));
impl.Emit(OpCodes.Ldloc, argsArr.LocalIndex);
var resultType = constrInfo.DeclaringType.RebuildGenericType(genericParams);
impl.Emit(OpCodes.Ldtoken, resultType);
impl.Emit(OpCodes.Call, typeof(Type).GetMethod(nameof(Type.GetTypeFromHandle), new[] { typeof(RuntimeTypeHandle) }));
impl.Ldloc(argsArr.LocalIndex);
impl.Emit(OpCodes.Call, typeof(Activator).GetMethod(nameof(Activator.CreateInstance), new[] { typeof(Type), typeof(object[]) }));
}
else
Expand Down
Loading