Skip to content

Commit

Permalink
Move fakes to main repo (#536)
Browse files Browse the repository at this point in the history
* Move fakes to main repo

* target frameworks
  • Loading branch information
robertcoltheart authored Dec 15, 2024
1 parent f93c224 commit 31c854f
Show file tree
Hide file tree
Showing 23 changed files with 914 additions and 0 deletions.
12 changes: 12 additions & 0 deletions Machine.Specifications.sln
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Machine.Specifications.Anal
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Machine.Specifications.Analyzers.Tests", "src\Machine.Specifications.Analyzers.Tests\Machine.Specifications.Analyzers.Tests.csproj", "{402EF260-6140-4633-880B-18BEBF7B9DA2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Machine.Specifications.Fakes", "src\Machine.Specifications.Fakes\Machine.Specifications.Fakes.csproj", "{2DBF8D62-8A2F-4BE1-B5F3-82910AD277F7}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Machine.Specifications.Fakes.Specs", "src\Machine.Specifications.Fakes.Specs\Machine.Specifications.Fakes.Specs.csproj", "{B897126F-BF01-4A97-965D-C2DE1BC63460}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -75,6 +79,14 @@ Global
{402EF260-6140-4633-880B-18BEBF7B9DA2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{402EF260-6140-4633-880B-18BEBF7B9DA2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{402EF260-6140-4633-880B-18BEBF7B9DA2}.Release|Any CPU.Build.0 = Release|Any CPU
{2DBF8D62-8A2F-4BE1-B5F3-82910AD277F7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2DBF8D62-8A2F-4BE1-B5F3-82910AD277F7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2DBF8D62-8A2F-4BE1-B5F3-82910AD277F7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2DBF8D62-8A2F-4BE1-B5F3-82910AD277F7}.Release|Any CPU.Build.0 = Release|Any CPU
{B897126F-BF01-4A97-965D-C2DE1BC63460}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B897126F-BF01-4A97-965D-C2DE1BC63460}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B897126F-BF01-4A97-965D-C2DE1BC63460}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B897126F-BF01-4A97-965D-C2DE1BC63460}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
273 changes: 273 additions & 0 deletions src/Machine.Specifications.Fakes.Specs/DeepFake.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,273 @@
using System;
using System.Reflection;
using Machine.Specifications.Fakes.Proxy;

namespace Machine.Specifications.Fakes.Specs
{
public interface IMyInterface
{
bool ReadProp { get; }

bool WriteProp { set; }

bool ReadWriteProp { get; set; }

event EventHandler Event;

bool Method(int arg1, ref int arg2, out int arg3);
}

public class MyClass
{
public MyClass(int arg1, int arg2)
{
}

public virtual bool ReadProp { get; }

public virtual bool WriteProp
{
set
{
}
}

public virtual bool ReadWriteProp { get; set; }

public virtual event EventHandler Event;

public virtual bool Method(int arg1, ref int arg2, out int arg3)
{
arg3 = default;

return default;
}
}

public delegate bool MyDelegate(int arg1, ref int arg2, out int arg3);

public class MyInterfaceFake : IMyInterface
{
private static readonly MethodInfo MethodHandle = typeof(IMyInterface).GetMethod("Method");
private static readonly MethodInfo ReadPropGetHandle = typeof(IMyInterface).GetMethod("get_ReadProp");
private static readonly MethodInfo WritePropSetHandle = typeof(IMyInterface).GetMethod("set_WriteProp");
private static readonly MethodInfo ReadWritePropGetHandle = typeof(IMyInterface).GetMethod("get_ReadWriteProp");
private static readonly MethodInfo ReadWritePropSetHandle = typeof(IMyInterface).GetMethod("set_ReadWriteProp");
private static readonly MethodInfo EventAddHandle = typeof(IMyInterface).GetMethod("add_Event");
private static readonly MethodInfo EventRemoveHandle = typeof(IMyInterface).GetMethod("remove_Event");

private readonly IInterceptor interceptor;

public MyInterfaceFake(IInterceptor interceptor)
{
this.interceptor = interceptor;
}

public bool ReadProp
{
get
{
var arguments = new object[0];
var invocation = new Invocation(ReadPropGetHandle, arguments);

interceptor.Intercept(invocation);

return (bool) invocation.ReturnValue;
}
}

public bool WriteProp
{
set
{
var arguments = new object[] {value};
var invocation = new Invocation(WritePropSetHandle, arguments);

interceptor.Intercept(invocation);
}
}

public bool ReadWriteProp
{
get
{
var arguments = new object[0];
var invocation = new Invocation(ReadWritePropGetHandle, arguments);

interceptor.Intercept(invocation);

return (bool) invocation.ReturnValue;
}
set
{
var arguments = new object[] {value};
var invocation = new Invocation(ReadWritePropSetHandle, arguments);

interceptor.Intercept(invocation);
}
}

public event EventHandler Event
{
add
{
var arguments = new object[] {value};
var invocation = new Invocation(EventAddHandle, arguments);

interceptor.Intercept(invocation);
}
remove
{
var arguments = new object[] {value};
var invocation = new Invocation(EventRemoveHandle, arguments);

interceptor.Intercept(invocation);
}
}

public bool Method(int arg1, ref int arg2, out int arg3)
{
arg3 = default;

var arguments = new object[] {arg1, arg2, arg3};
var invocation = new Invocation(MethodHandle, arguments);

interceptor.Intercept(invocation);

arg2 = (int) arguments[1];
arg3 = (int) arguments[2];

return (bool) invocation.ReturnValue;
}
}

public class MyClassFake : MyClass
{
private static readonly MethodInfo MethodHandle = typeof(MyClass).GetMethod("Method");
private static readonly MethodInfo ReadPropGetHandle = typeof(MyClass).GetMethod("get_ReadProp");
private static readonly MethodInfo WritePropSetHandle = typeof(MyClass).GetMethod("set_WriteProp");
private static readonly MethodInfo ReadWritePropGetHandle = typeof(MyClass).GetMethod("get_ReadWriteProp");
private static readonly MethodInfo ReadWritePropSetHandle = typeof(MyClass).GetMethod("set_ReadWriteProp");
private static readonly MethodInfo EventAddHandle = typeof(MyClass).GetMethod("add_Event");
private static readonly MethodInfo EventRemoveHandle = typeof(MyClass).GetMethod("remove_Event");

private readonly IInterceptor interceptor;

public MyClassFake(IInterceptor interceptor, int arg1, int arg2)
: base(arg1, arg2)
{
this.interceptor = interceptor;
}

public override bool ReadProp
{
get
{
var arguments = new object[0];
var invocation = new Invocation(ReadPropGetHandle, arguments);

interceptor.Intercept(invocation);

return (bool) invocation.ReturnValue;
}
}

public override bool WriteProp
{
set
{
var arguments = new object[] {value};
var invocation = new Invocation(WritePropSetHandle, arguments);

interceptor.Intercept(invocation);
}
}

public override bool ReadWriteProp
{
get
{
var arguments = new object[0];
var invocation = new Invocation(ReadWritePropGetHandle, arguments);

interceptor.Intercept(invocation);

return (bool) invocation.ReturnValue;
}
set
{
var arguments = new object[] {value};
var invocation = new Invocation(ReadWritePropSetHandle, arguments);

interceptor.Intercept(invocation);
}
}

public override event EventHandler Event
{
add
{
var arguments = new object[] {value};
var invocation = new Invocation(EventAddHandle, arguments);

interceptor.Intercept(invocation);
}
remove
{
var arguments = new object[] {value};
var invocation = new Invocation(EventRemoveHandle, arguments);

interceptor.Intercept(invocation);
}
}

public override bool Method(int arg1, ref int arg2, out int arg3)
{
arg3 = default;

var arguments = new object[] {arg1, arg2, arg3};
var invocation = new Invocation(MethodHandle, arguments);

interceptor.Intercept(invocation);

arg2 = (int) arguments[1];
arg3 = (int) arguments[2];

return (bool) invocation.ReturnValue;
}
}

public class MyDelegateFake
{
private static readonly MethodInfo MethodHandle = typeof(MyDelegate).GetMethod("Invoke");

private readonly IInterceptor interceptor;

public MyDelegateFake(IInterceptor interceptor)
{
this.interceptor = interceptor;
}

public bool Invoke(int arg1, ref int arg2, out int arg3)
{
arg3 = default;

var arguments = new object[] {arg1, arg2, arg3};
var invocation = new Invocation(MethodHandle, arguments);

interceptor.Intercept(invocation);

arg2 = (int) arguments[1];
arg3 = (int) arguments[2];

return (bool) invocation.ReturnValue;
}

public static MyDelegate Create(IInterceptor interceptor)
{
var target = new MyDelegateFake(interceptor);

return (MyDelegate) Delegate.CreateDelegate(typeof(MyDelegate), target, "Invoke");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Machine.Specifications" Version="1.0.0" />
<PackageReference Include="Machine.Specifications.Runner.VisualStudio" Version="2.9.0" />
<PackageReference Include="Machine.Specifications.Should" Version="1.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Machine.Specifications.Fakes\Machine.Specifications.Fakes.csproj" />
</ItemGroup>

</Project>
59 changes: 59 additions & 0 deletions src/Machine.Specifications.Fakes.Specs/TestSpec.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using System;
using System.Linq;
using System.Reflection;
using Machine.Specifications.Fakes.Proxy;
using Machine.Specifications.Fakes.Proxy.Reflection;

namespace Machine.Specifications.Fakes.Specs
{
public interface IRob
{
object Execute(int arg1);
}

public class Interceptor : IInterceptor
{
public void Intercept(IInvocation invocation)
{
invocation.ReturnValue = null;
}
}

class TestSpec
{
static TypeEmitterFactory factory;

static MethodInfo execute_method;

static Type type;

static Interceptor interceptor;

Establish context = () =>
{
factory = new TypeEmitterFactory();
interceptor = new Interceptor();
execute_method = typeof(IRob).GetMethod("Execute", BindingFlags.Instance | BindingFlags.Public);
};

Because of = () =>
{
var emitter = factory.DefineType(typeof(object));

emitter.EmitInterface(typeof(IRob));
emitter.EmitConstructor(typeof(object).GetConstructors().First());
emitter.EmitMethod(execute_method);

type = emitter.CreateType();
};

It should = () =>
{
var value = Activator.CreateInstance(type, interceptor);

var ret = execute_method.Invoke(value, new object[] {4});

int r = 1;
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net472;netstandard2.0</TargetFrameworks>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.Reflection.Emit" Version="4.3.0" />
</ItemGroup>

</Project>
Loading

0 comments on commit 31c854f

Please sign in to comment.