Skip to content

Commit 31c854f

Browse files
Move fakes to main repo (#536)
* Move fakes to main repo * target frameworks
1 parent f93c224 commit 31c854f

23 files changed

+914
-0
lines changed

Machine.Specifications.sln

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Machine.Specifications.Anal
2525
EndProject
2626
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}"
2727
EndProject
28+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Machine.Specifications.Fakes", "src\Machine.Specifications.Fakes\Machine.Specifications.Fakes.csproj", "{2DBF8D62-8A2F-4BE1-B5F3-82910AD277F7}"
29+
EndProject
30+
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}"
31+
EndProject
2832
Global
2933
GlobalSection(SolutionConfigurationPlatforms) = preSolution
3034
Debug|Any CPU = Debug|Any CPU
@@ -75,6 +79,14 @@ Global
7579
{402EF260-6140-4633-880B-18BEBF7B9DA2}.Debug|Any CPU.Build.0 = Debug|Any CPU
7680
{402EF260-6140-4633-880B-18BEBF7B9DA2}.Release|Any CPU.ActiveCfg = Release|Any CPU
7781
{402EF260-6140-4633-880B-18BEBF7B9DA2}.Release|Any CPU.Build.0 = Release|Any CPU
82+
{2DBF8D62-8A2F-4BE1-B5F3-82910AD277F7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
83+
{2DBF8D62-8A2F-4BE1-B5F3-82910AD277F7}.Debug|Any CPU.Build.0 = Debug|Any CPU
84+
{2DBF8D62-8A2F-4BE1-B5F3-82910AD277F7}.Release|Any CPU.ActiveCfg = Release|Any CPU
85+
{2DBF8D62-8A2F-4BE1-B5F3-82910AD277F7}.Release|Any CPU.Build.0 = Release|Any CPU
86+
{B897126F-BF01-4A97-965D-C2DE1BC63460}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
87+
{B897126F-BF01-4A97-965D-C2DE1BC63460}.Debug|Any CPU.Build.0 = Debug|Any CPU
88+
{B897126F-BF01-4A97-965D-C2DE1BC63460}.Release|Any CPU.ActiveCfg = Release|Any CPU
89+
{B897126F-BF01-4A97-965D-C2DE1BC63460}.Release|Any CPU.Build.0 = Release|Any CPU
7890
EndGlobalSection
7991
GlobalSection(SolutionProperties) = preSolution
8092
HideSolutionNode = FALSE
Lines changed: 273 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,273 @@
1+
using System;
2+
using System.Reflection;
3+
using Machine.Specifications.Fakes.Proxy;
4+
5+
namespace Machine.Specifications.Fakes.Specs
6+
{
7+
public interface IMyInterface
8+
{
9+
bool ReadProp { get; }
10+
11+
bool WriteProp { set; }
12+
13+
bool ReadWriteProp { get; set; }
14+
15+
event EventHandler Event;
16+
17+
bool Method(int arg1, ref int arg2, out int arg3);
18+
}
19+
20+
public class MyClass
21+
{
22+
public MyClass(int arg1, int arg2)
23+
{
24+
}
25+
26+
public virtual bool ReadProp { get; }
27+
28+
public virtual bool WriteProp
29+
{
30+
set
31+
{
32+
}
33+
}
34+
35+
public virtual bool ReadWriteProp { get; set; }
36+
37+
public virtual event EventHandler Event;
38+
39+
public virtual bool Method(int arg1, ref int arg2, out int arg3)
40+
{
41+
arg3 = default;
42+
43+
return default;
44+
}
45+
}
46+
47+
public delegate bool MyDelegate(int arg1, ref int arg2, out int arg3);
48+
49+
public class MyInterfaceFake : IMyInterface
50+
{
51+
private static readonly MethodInfo MethodHandle = typeof(IMyInterface).GetMethod("Method");
52+
private static readonly MethodInfo ReadPropGetHandle = typeof(IMyInterface).GetMethod("get_ReadProp");
53+
private static readonly MethodInfo WritePropSetHandle = typeof(IMyInterface).GetMethod("set_WriteProp");
54+
private static readonly MethodInfo ReadWritePropGetHandle = typeof(IMyInterface).GetMethod("get_ReadWriteProp");
55+
private static readonly MethodInfo ReadWritePropSetHandle = typeof(IMyInterface).GetMethod("set_ReadWriteProp");
56+
private static readonly MethodInfo EventAddHandle = typeof(IMyInterface).GetMethod("add_Event");
57+
private static readonly MethodInfo EventRemoveHandle = typeof(IMyInterface).GetMethod("remove_Event");
58+
59+
private readonly IInterceptor interceptor;
60+
61+
public MyInterfaceFake(IInterceptor interceptor)
62+
{
63+
this.interceptor = interceptor;
64+
}
65+
66+
public bool ReadProp
67+
{
68+
get
69+
{
70+
var arguments = new object[0];
71+
var invocation = new Invocation(ReadPropGetHandle, arguments);
72+
73+
interceptor.Intercept(invocation);
74+
75+
return (bool) invocation.ReturnValue;
76+
}
77+
}
78+
79+
public bool WriteProp
80+
{
81+
set
82+
{
83+
var arguments = new object[] {value};
84+
var invocation = new Invocation(WritePropSetHandle, arguments);
85+
86+
interceptor.Intercept(invocation);
87+
}
88+
}
89+
90+
public bool ReadWriteProp
91+
{
92+
get
93+
{
94+
var arguments = new object[0];
95+
var invocation = new Invocation(ReadWritePropGetHandle, arguments);
96+
97+
interceptor.Intercept(invocation);
98+
99+
return (bool) invocation.ReturnValue;
100+
}
101+
set
102+
{
103+
var arguments = new object[] {value};
104+
var invocation = new Invocation(ReadWritePropSetHandle, arguments);
105+
106+
interceptor.Intercept(invocation);
107+
}
108+
}
109+
110+
public event EventHandler Event
111+
{
112+
add
113+
{
114+
var arguments = new object[] {value};
115+
var invocation = new Invocation(EventAddHandle, arguments);
116+
117+
interceptor.Intercept(invocation);
118+
}
119+
remove
120+
{
121+
var arguments = new object[] {value};
122+
var invocation = new Invocation(EventRemoveHandle, arguments);
123+
124+
interceptor.Intercept(invocation);
125+
}
126+
}
127+
128+
public bool Method(int arg1, ref int arg2, out int arg3)
129+
{
130+
arg3 = default;
131+
132+
var arguments = new object[] {arg1, arg2, arg3};
133+
var invocation = new Invocation(MethodHandle, arguments);
134+
135+
interceptor.Intercept(invocation);
136+
137+
arg2 = (int) arguments[1];
138+
arg3 = (int) arguments[2];
139+
140+
return (bool) invocation.ReturnValue;
141+
}
142+
}
143+
144+
public class MyClassFake : MyClass
145+
{
146+
private static readonly MethodInfo MethodHandle = typeof(MyClass).GetMethod("Method");
147+
private static readonly MethodInfo ReadPropGetHandle = typeof(MyClass).GetMethod("get_ReadProp");
148+
private static readonly MethodInfo WritePropSetHandle = typeof(MyClass).GetMethod("set_WriteProp");
149+
private static readonly MethodInfo ReadWritePropGetHandle = typeof(MyClass).GetMethod("get_ReadWriteProp");
150+
private static readonly MethodInfo ReadWritePropSetHandle = typeof(MyClass).GetMethod("set_ReadWriteProp");
151+
private static readonly MethodInfo EventAddHandle = typeof(MyClass).GetMethod("add_Event");
152+
private static readonly MethodInfo EventRemoveHandle = typeof(MyClass).GetMethod("remove_Event");
153+
154+
private readonly IInterceptor interceptor;
155+
156+
public MyClassFake(IInterceptor interceptor, int arg1, int arg2)
157+
: base(arg1, arg2)
158+
{
159+
this.interceptor = interceptor;
160+
}
161+
162+
public override bool ReadProp
163+
{
164+
get
165+
{
166+
var arguments = new object[0];
167+
var invocation = new Invocation(ReadPropGetHandle, arguments);
168+
169+
interceptor.Intercept(invocation);
170+
171+
return (bool) invocation.ReturnValue;
172+
}
173+
}
174+
175+
public override bool WriteProp
176+
{
177+
set
178+
{
179+
var arguments = new object[] {value};
180+
var invocation = new Invocation(WritePropSetHandle, arguments);
181+
182+
interceptor.Intercept(invocation);
183+
}
184+
}
185+
186+
public override bool ReadWriteProp
187+
{
188+
get
189+
{
190+
var arguments = new object[0];
191+
var invocation = new Invocation(ReadWritePropGetHandle, arguments);
192+
193+
interceptor.Intercept(invocation);
194+
195+
return (bool) invocation.ReturnValue;
196+
}
197+
set
198+
{
199+
var arguments = new object[] {value};
200+
var invocation = new Invocation(ReadWritePropSetHandle, arguments);
201+
202+
interceptor.Intercept(invocation);
203+
}
204+
}
205+
206+
public override event EventHandler Event
207+
{
208+
add
209+
{
210+
var arguments = new object[] {value};
211+
var invocation = new Invocation(EventAddHandle, arguments);
212+
213+
interceptor.Intercept(invocation);
214+
}
215+
remove
216+
{
217+
var arguments = new object[] {value};
218+
var invocation = new Invocation(EventRemoveHandle, arguments);
219+
220+
interceptor.Intercept(invocation);
221+
}
222+
}
223+
224+
public override bool Method(int arg1, ref int arg2, out int arg3)
225+
{
226+
arg3 = default;
227+
228+
var arguments = new object[] {arg1, arg2, arg3};
229+
var invocation = new Invocation(MethodHandle, arguments);
230+
231+
interceptor.Intercept(invocation);
232+
233+
arg2 = (int) arguments[1];
234+
arg3 = (int) arguments[2];
235+
236+
return (bool) invocation.ReturnValue;
237+
}
238+
}
239+
240+
public class MyDelegateFake
241+
{
242+
private static readonly MethodInfo MethodHandle = typeof(MyDelegate).GetMethod("Invoke");
243+
244+
private readonly IInterceptor interceptor;
245+
246+
public MyDelegateFake(IInterceptor interceptor)
247+
{
248+
this.interceptor = interceptor;
249+
}
250+
251+
public bool Invoke(int arg1, ref int arg2, out int arg3)
252+
{
253+
arg3 = default;
254+
255+
var arguments = new object[] {arg1, arg2, arg3};
256+
var invocation = new Invocation(MethodHandle, arguments);
257+
258+
interceptor.Intercept(invocation);
259+
260+
arg2 = (int) arguments[1];
261+
arg3 = (int) arguments[2];
262+
263+
return (bool) invocation.ReturnValue;
264+
}
265+
266+
public static MyDelegate Create(IInterceptor interceptor)
267+
{
268+
var target = new MyDelegateFake(interceptor);
269+
270+
return (MyDelegate) Delegate.CreateDelegate(typeof(MyDelegate), target, "Invoke");
271+
}
272+
}
273+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
6+
<IsPackable>false</IsPackable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Machine.Specifications" Version="1.0.0" />
11+
<PackageReference Include="Machine.Specifications.Runner.VisualStudio" Version="2.9.0" />
12+
<PackageReference Include="Machine.Specifications.Should" Version="1.0.0" />
13+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" />
14+
</ItemGroup>
15+
16+
<ItemGroup>
17+
<ProjectReference Include="..\Machine.Specifications.Fakes\Machine.Specifications.Fakes.csproj" />
18+
</ItemGroup>
19+
20+
</Project>
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using System;
2+
using System.Linq;
3+
using System.Reflection;
4+
using Machine.Specifications.Fakes.Proxy;
5+
using Machine.Specifications.Fakes.Proxy.Reflection;
6+
7+
namespace Machine.Specifications.Fakes.Specs
8+
{
9+
public interface IRob
10+
{
11+
object Execute(int arg1);
12+
}
13+
14+
public class Interceptor : IInterceptor
15+
{
16+
public void Intercept(IInvocation invocation)
17+
{
18+
invocation.ReturnValue = null;
19+
}
20+
}
21+
22+
class TestSpec
23+
{
24+
static TypeEmitterFactory factory;
25+
26+
static MethodInfo execute_method;
27+
28+
static Type type;
29+
30+
static Interceptor interceptor;
31+
32+
Establish context = () =>
33+
{
34+
factory = new TypeEmitterFactory();
35+
interceptor = new Interceptor();
36+
execute_method = typeof(IRob).GetMethod("Execute", BindingFlags.Instance | BindingFlags.Public);
37+
};
38+
39+
Because of = () =>
40+
{
41+
var emitter = factory.DefineType(typeof(object));
42+
43+
emitter.EmitInterface(typeof(IRob));
44+
emitter.EmitConstructor(typeof(object).GetConstructors().First());
45+
emitter.EmitMethod(execute_method);
46+
47+
type = emitter.CreateType();
48+
};
49+
50+
It should = () =>
51+
{
52+
var value = Activator.CreateInstance(type, interceptor);
53+
54+
var ret = execute_method.Invoke(value, new object[] {4});
55+
56+
int r = 1;
57+
};
58+
}
59+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFrameworks>net472;netstandard2.0</TargetFrameworks>
5+
<IsPackable>false</IsPackable>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="System.Reflection.Emit" Version="4.3.0" />
10+
</ItemGroup>
11+
12+
</Project>

0 commit comments

Comments
 (0)