|
| 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 | +} |
0 commit comments