-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Move fakes to main repo * target frameworks
- Loading branch information
1 parent
f93c224
commit 31c854f
Showing
23 changed files
with
914 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/Machine.Specifications.Fakes.Specs/Machine.Specifications.Fakes.Specs.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/Machine.Specifications.Fakes/Machine.Specifications.Fakes.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
Oops, something went wrong.