How to use Instruction class of Telerik.JustMock.Core package

Best JustMockLite code snippet using Telerik.JustMock.Core.Instruction

MockingUtil.PrivateMethods.cs

Source:MockingUtil.PrivateMethods.cs Github

copy

Full Screen

...312 }313 string expectedName = String.Format("<{0}>g__{1}|", string.IsNullOrEmpty(originalMethodName) ? method.Name : originalMethodName, localMemberName);314 MethodBody body = method.GetMethodBody();315 var disasembledBody = MethodBodyDisassembler.DisassembleMethodInfo(method);316 var callInstructions = disasembledBody.Where(instr => instr.OpCode == System.Reflection.Emit.OpCodes.Call).ToArray();317 foreach (var instruction in callInstructions)318 {319 MethodBase methodBase = null;320 try321 {322 methodBase = type.Module.ResolveMethod(instruction.Operand.Int);323 }324 catch (Exception)325 {326 //The exception is captured when the metadata token refers generic method.327 //Do not handle the exception since there is no way to know in advance if the metadata token refers non-generic or generic method.328 }329 if (methodBase == null && localFunctionGenericTypes != null)330 {331 try...

Full Screen

Full Screen

MockingUtils.MethodBodyDisassembler.cs

Source:MockingUtils.MethodBodyDisassembler.cs Github

copy

Full Screen

...40 public double Double;41 [FieldOffset(0)]42 public long Long;43 }44 internal class Instruction45 {46 public readonly OpCode OpCode;47 public readonly int OperandSize;48 private readonly Operand? operand;49 public readonly Module Module;50 public Operand Operand51 {52 get { return operand.Value; }53 }54 public int Length { get { return OpCode.Size + OperandSize; } }55 internal Instruction(OpCode opCode, int operandSize, Operand? operand, Module module)56 {57 this.OpCode = opCode;58 this.OperandSize = operandSize;59 this.operand = operand;60 Module = module;61 }62 }63 internal class MethodBodyDisassembler64 {65 private static readonly HashSet<int> prefixes = new HashSet<int>66 {67 OpCodes.Prefix1.Value,68 OpCodes.Prefix2.Value,69 OpCodes.Prefix3.Value,70 OpCodes.Prefix4.Value,71 OpCodes.Prefix5.Value,72 OpCodes.Prefix6.Value,73 OpCodes.Prefix7.Value,74 };75 private static readonly Dictionary<short, OpCode> opCodeMap = new Dictionary<short, OpCode>();76 static MethodBodyDisassembler()77 {78 var opcodes = typeof(OpCodes).GetFields(BindingFlags.Public | BindingFlags.Static)79 .Where(field => field.FieldType == typeof(OpCode));80 foreach (var opcodeField in opcodes)81 {82 var opcode = (OpCode)opcodeField.GetValue(null);83 opCodeMap.Add(opcode.Value, opcode);84 }85 }86 public static IEnumerable<Instruction> DisassembleMethodInfo(MethodBase method)87 {88 var body = method.GetMethodBody();89 if (body != null)90 {91 var il = body.GetILAsByteArray();92 for (int i = 0; i < il.Length;)93 {94 int baseIdx = i;95 int code1 = il[i++];96 var code = (short)(prefixes.Contains(code1) ? (code1 << 8) | il[i++] : code1);97 var opcode = opCodeMap[code];98 int operandSize;99 bool validOperand = true;100 var operand = default(Operand);101 switch (opcode.OperandType)102 {103 case OperandType.InlineBrTarget:104 case OperandType.InlineField:105 case OperandType.InlineI:106 case OperandType.InlineMethod:107 case OperandType.InlineSig:108 case OperandType.InlineString:109 case OperandType.InlineTok:110 case OperandType.InlineType:111 case OperandType.ShortInlineR:112 operand.Int = BitConverter.ToInt32(il, i);113 operandSize = 4;114 break;115 case OperandType.InlineI8:116 case OperandType.InlineR:117 operand.Long = BitConverter.ToInt64(il, i);118 operandSize = 8;119 break;120 case OperandType.InlineNone:121 validOperand = false;122 operandSize = 0;123 break;124 case OperandType.InlineVar:125 operand.Short = BitConverter.ToInt16(il, i);126 operandSize = 2;127 break;128 case OperandType.ShortInlineBrTarget:129 case OperandType.ShortInlineI:130 case OperandType.ShortInlineVar:131 operand.Byte = il[i];132 operandSize = 1;133 break;134 case OperandType.InlineSwitch:135 var branchCount = BitConverter.ToInt32(il, i);136 operandSize = 4 + 4 * branchCount;137 validOperand = false;138 break;139 default:140 throw new ArgumentOutOfRangeException();141 }142 i += operandSize;143 var instrBuffer = il.Skip(baseIdx).Take(i - baseIdx).ToArray();144 yield return new Instruction(opcode, operandSize, validOperand ? (Operand?)operand : null, method.Module);145 }146 }147 }148 }149 }150}...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run JustMockLite automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used methods in Instruction

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful