private static void EmitILForConstructor(ILGenerator ilGenerator, OperandInstruction <MethodBase> instruction, ConstructorInfo constructorInfo, int contextParamIndex) { /*if (PoseContext.StubCache.TryGetValue(constructorInfo, out DynamicMethod stub)) * { * ilGenerator.Emit(OpCodes.Ldtoken, constructorInfo); * ilGenerator.Emit(OpCodes.Ldtoken, constructorInfo.DeclaringType); * ilGenerator.Emit(OpCodes.Call, stub); * return; * }*/ var methodBody = constructorInfo.GetMethodBody(); if (methodBody == null) { ilGenerator.Emit(instruction.OpCode, constructorInfo); return; } if (instruction.OpCode != OpCodes.Newobj && instruction.OpCode != OpCodes.Call) { ilGenerator.Emit(instruction.OpCode, constructorInfo); return; } var stub = StubGenerator.GenerateStub(constructorInfo, instruction.OpCode == OpCodes.Newobj); ilGenerator.Emit(OpCodes.Ldarg, contextParamIndex); ilGenerator.Emit(OpCodes.Call, stub); //PoseContext.StubCache.TryAdd(constructorInfo, stub); }
public void Sets_up_story_handler_to_be_generator() { Factory.ParseArguments(new[] { "stub" }); StubGenerator generator = GetGenerator(); generator.ShouldNotBeNull(); }
public void GenerateStubTestConstructor() { var info = typeof(ExampleClass).GetConstructor(new [] { typeof(int) }); var stub = StubGenerator.GenerateStub(info); var result = (ExampleClass)stub.Invoke(null, new object[] { 3, new ShimContext() }); Assert.NotNull(result); Assert.Equal(3, result.Factor); info = typeof(List <string>).GetConstructor(Type.EmptyTypes); stub = StubGenerator.GenerateStub(info); var resultList = (List <string>)stub.Invoke(null, new object[] { new ShimContext() }); Assert.NotNull(resultList); Assert.Empty(resultList); info = typeof(ExampleStruct).GetConstructor(new[] { typeof(int) }); stub = StubGenerator.GenerateStub(info); var resultStruct = (ExampleStruct)stub.Invoke(null, new object[] { 3, new ShimContext() }); Assert.Equal(3, resultStruct.Factor); }
public void GenerateStubTestVirtualMethod() { var methodInfo = typeof(ExampleClass).GetMethod(nameof(ExampleClass.VirtualTestMethod), BindingFlags.Public | BindingFlags.Instance); var stub = StubGenerator.GenerateStubForVirtualMethod(methodInfo); var result = (int)stub.Invoke(null, new object[] { new ExampleClassChild(2), 3, new ShimContext() }); Assert.Equal(12, result); }
public void GenerateStubTestStaticMethod() { var methodInfo = typeof(ExampleClass).GetMethod(nameof(ExampleClass.StaticTestMethod), BindingFlags.Public | BindingFlags.Static); var stub = StubGenerator.GenerateStubForMethod(methodInfo); var result = (int)stub.Invoke(null, new object[] { 3, new ShimContext() }); Assert.Equal(6, result); }
public void Rewriter(ILProcessor processor) { var constraineds = processor.Instructions .OfType <OperandInstruction <Type> >() .Where(i => i.OpCode == OpCodes.Constrained) .ToList(); foreach (var constrained in constraineds) { var constrainedType = constrained.Operand; var methodCall = (OperandInstruction <MethodBase>)constrained.Next; //Remove the Constrained Instruction processor.Remove(constrained); //Check for a quick fix if (constrainedType.IsValueType) { //Check if the value type directly implements the method or not var realMethod = StubGenerator.GetRuntimeMethodForVirtual(constrainedType, (MethodInfo)methodCall.Operand); if (realMethod.DeclaringType == constrainedType) { //the method is directly implemented, just replace the CallVirt with a normal Call processor.Replace(methodCall, OperandInstruction.Create <MethodBase>(OpCodes.Call, realMethod)); //For this case we are done! continue; } } //Find were the this pointer is pushed on the stack var thisInstruction = FindThisInstruction(methodCall); if (constrainedType.IsValueType) //the method is not directly implemented { //ldobj struct //box struct //call virt processor.InsertAfter(thisInstruction, new NoneInstruction(OpCodes.Box)); processor.InsertAfter(thisInstruction, new NoneInstruction(OpCodes.Ldobj)); } else //this is a reference type { //ldind_ref //call virt processor.InsertAfter(thisInstruction, new NoneInstruction(OpCodes.Ldind_Ref)); } } }
public void GenerateStubTestInstanceMethod() { var methodInfo = typeof(ExampleClass).GetMethod(nameof(ExampleClass.InstanceTestMethod), BindingFlags.Public | BindingFlags.Instance); var stub = StubGenerator.GenerateStubForMethod(methodInfo); //This is not possible with the current generator //var result = (int)stub.Invoke(new TestClass(2), new object[] { 3, new ShimContext() }); var result = (int)stub.Invoke(null, new object[] { new ExampleClass(2), 3, new ShimContext() }); Assert.Equal(6, result); }
public void GenerateStubTestLocalMethod() { int TestMethod(int a) { return(a * 2); } Func <int, int> func = TestMethod; var stub = StubGenerator.GenerateStubForMethod(func.Method); var result = (int)stub.Invoke(null, new[] { func.Target, 3, new ShimContext() }); Assert.Equal(6, result); }
private static void EmitILForMethod(ILGenerator ilGenerator, OperandInstruction <MethodBase> instruction, MethodInfo methodInfo, ShimContext context, int contextParamIndex) { /*if (context.StubCache.TryGetValue(methodInfo, out DynamicMethod stub)) * { * ilGenerator.Emit(OpCodes.Ldtoken, methodInfo); * ilGenerator.Emit(OpCodes.Ldtoken, methodInfo.DeclaringType); * ilGenerator.Emit(OpCodes.Call, stub); * return; * }*/ var methodBody = methodInfo.GetMethodBody(); if (methodBody == null) { ilGenerator.Emit(instruction.OpCode, methodInfo); return; } MethodInfo stub; if (instruction.OpCode == OpCodes.Call) { stub = StubGenerator.GenerateStubForMethod(methodInfo); } else if (instruction.OpCode == OpCodes.Callvirt) { stub = StubGenerator.GenerateStubForVirtualMethod(methodInfo); } /*else if (instruction.OpCode == OpCodes.Ldftn) * { * stub = StubGenerator.GenerateStubForMethodPointer(methodInfo); * } * else if (instruction.OpCode == OpCodes.Ldvirtftn) * { * stub = StubGenerator.GenerateStubForMethodPointer(methodInfo); * }*/ else { ilGenerator.Emit(instruction.OpCode, methodInfo); return; } ilGenerator.Emit(OpCodes.Ldarg, contextParamIndex); ilGenerator.Emit(OpCodes.Call, stub); //PoseContext.StubCache.TryAdd(methodInfo, stub); }
public ProductServiceTest() { this._productListStub = StubGenerator.GetProductsListStub(); this._productRepositoryMock = new Mock <IProductRepository>(); this._productRepositoryMock .Setup(p => p.GetProducts()) .Returns(this._productListStub); foreach (Product product in this._productListStub) { this._productRepositoryMock .Setup(p => p.GetProductById(product.Id)) .Returns(product); } this._service = new ProductService(this._productRepositoryMock.Object); }
public DelegateInfo GenerateDelegateStub() { PerfTrack.NoteEvent(PerfTrack.Categories.DelegateCreate, ToString()); Type[] delegateParams = new Type[_parameters.Length]; for (int i = 0; i < _parameters.Length; i++) { delegateParams[i] = _parameters[i].ParameterType; } AssemblyGen snippets = ScriptDomainManager.CurrentManager.Snippets.Assembly; // Create new constant pool ConstantPool constants = new ConstantPool(); // Create the method CodeGen cg = snippets.DefineMethod(ToString(), _returnType, delegateParams, constants); cg.Binder = _binder; // Add the space for the delegate target and save the index at which it was placed, // most likely zero. int targetIndex = constants.Count; #if DEBUG Slot target = constants.AddData(TargetPlaceHolder); #else Slot target = constants.AddData(null); #endif // Add the CodeContext into the constant pool Slot context = cg.ConstantPool.AddData(_binder.Context); Debug.Assert(typeof(CodeContext).IsAssignableFrom(context.Type)); cg.ContextSlot = context; // Emit the stub StubGenerator.EmitClrCallStub(cg, target, 0, StubGenerator.CallType.None); // Finish the method MethodInfo method = cg.CreateDelegateMethodInfo(); // Save the constants in the delegate info class return(new DelegateInfo(method, constants.Data, targetIndex)); }
public void SetupContext() { FakeWriter = MockRepository.GenerateMock <ITextWriter>(); FakeResolver = MockRepository.GenerateMock <IAmbiguousMatchResolver>(); var scenarioInterpreter = new ScenarioInterpreter(new InterpreterForTypeFactory(new AssemblyRegistry()), FakeResolver, new DefaultLanguageService()); Generator = new StubGenerator(scenarioInterpreter, new ImplementationHelper(), FakeWriter, new FakeSessionContext()); TestStory = new Story("foo", "", new[] { TestHelper.BuildScenario("", new[] { "first line", "second line" }), TestHelper.BuildScenario("", new[] { "first line", "second line", "third line" }), TestHelper.BuildScenario("", new[] { "this line's weird, punctuation! should be: handled" }) }); Generator.HandleStories(new[] { TestStory }); Suggestions = (string)FakeWriter.GetArgumentsForCallsMadeOn(x => x.Write(Arg <string> .Is.Anything))[0][0]; }
public OrderServiceTest() { this._ordersInDateRangeStub = StubGenerator.GetOrdersListStub(); this._productListStub = StubGenerator.GetProductsListStub(); this._orderRespositoryMock = new Mock <IOrderRepository>(); this._orderRespositoryMock .Setup(o => o.GetOrdersInDateRange(It.IsAny <DateRange>())) .Returns(this._ordersInDateRangeStub); this._productRepositoryMock = new Mock <IProductRepository>(); this._productRepositoryMock .Setup(p => p.GetProducts()) .Returns(this._productListStub); for (int i = 0; i < this._productListStub.Count; i++) { this._productRepositoryMock .Setup(p => p.GetProductById(this._productListStub[i].Id)) .Returns(this._productListStub[i]); } this._service = new OrderService(this._orderRespositoryMock.Object, this._productRepositoryMock.Object); }