public void GetParameters_InstructionsThatReadParameters_ReturnsParameters() { MethodReference method = CecilUtility.Import(() => string.Format("Test", 12)); var instructions = new[] { Instruction.Create(OpCodes.Ldarg_1), Instruction.Create(OpCodes.Ldarg_0) }; Parameter parameter = new Parameter(CecilUtility.Import(typeof(string)), 0); _instructionHelperMock .Setup(helper => helper.ReadsParameter(method, instructions[0], out parameter)) .Returns(true); Parameter parameter2 = new Parameter(CecilUtility.Import(typeof(int)), 1); _instructionHelperMock .Setup(helper => helper.ReadsParameter(method, instructions[1], out parameter2)) .Returns(true); var subject = new ParameterDeducer(_instructionHelperMock.Object); TypeReference[] result = subject.GetParameters(method, instructions); Assert.AreEqual(2, result.Length); Assert.AreEqual(parameter.ParameterType, result[0]); Assert.AreEqual(parameter2.ParameterType, result[1]); }
public void Accepts_ReturnsTrue() { var subject = new AllowAllModuleFilter(); Assert.IsTrue(subject.Accepts(null)); Assert.IsTrue(subject.Accepts(CecilUtility.Import(typeof(object)).Resolve().Module)); }
public void Constructor_ModuleComparerNull_ThrowsArgumentNullException() { var module = CecilUtility.Import(typeof(object)).Resolve().Module; var exception = Assert.Throws <ArgumentNullException>( () => new DependencyGraphNode(module, null)); Assert.AreEqual("moduleComparer", exception.ParamName); }
public void GetParameters_EmptyInstructions_EmptyResult() { MethodReference method = CecilUtility.Import(() => Console.WriteLine()); var subject = new ParameterDeducer(_instructionHelperMock.Object); var result = subject.GetParameters(method, Enumerable.Empty <Instruction>()); Assert.AreEqual(0, result.Length); }
public void Constructor_SetsProperties() { const int index = 42; var type = CecilUtility.Import(typeof(string)); var subject = new Parameter(type, index); Assert.AreSame(type, subject.ParameterType); Assert.AreEqual(index, subject.Index); }
public void Accept_InlineFieldTokenOperand_InvokesVisitor() { FieldReference field = CecilUtility.Import(ReflectionUtility.GetField(() => OpCodes.Nop)); var instruction = Instruction.Create(OpCodes.Ldtoken, field); _visitorMock .Setup(visitor => visitor.VisitInlineTok(instruction, field)) .Returns(Unit.Value) .Verifiable(); instruction.Accept(_visitorMock.Object); _visitorMock.Verify(); }
public void Accept_ShortInlineVarOperand_InvokesVisitor() { var variable = new VariableDefinition(CecilUtility.Import(typeof(object))); var instruction = Instruction.Create(OpCodes.Ldloc_S, variable); _visitorMock .Setup(visitor => visitor.VisitInlineVar(instruction, variable)) .Returns(Unit.Value) .Verifiable(); instruction.Accept(_visitorMock.Object); _visitorMock.Verify(); }
public void Accept_InlineTypeTokenOperand_InvokesVisitor() { TypeReference type = CecilUtility.Import(typeof(object)); var instruction = Instruction.Create(OpCodes.Ldtoken, type); _visitorMock .Setup(visitor => visitor.VisitInlineTok(instruction, type)) .Returns(Unit.Value) .Verifiable(); instruction.Accept(_visitorMock.Object); _visitorMock.Verify(); }
public void GetMatchingTargets_ReturnsOnlySetupsThatTargetQueriedMethod(int indexToTest) { ReadOnlyCollection <IRewriteTarget> setups = new List <IRewriteTarget> { TestDataFactory.CreateSetupTarget(() => Console.WriteLine()), TestDataFactory.CreateSetupTarget(() => Console.ReadLine()) }.AsReadOnly(); var subject = new RewriteTargetMatcher(_methodImporterMock.Object, setups); var result = subject.GetMatchingTargets(CecilUtility.Import(setups[indexToTest].Methods[0])); Assert.AreEqual(setups[indexToTest], result.Single()); }
public void Accept_InlineMethodTokenOperand_InvokesVisitor() { var method = CecilUtility.Import(ReflectionUtility.GetMethod(() => Console.WriteLine())); var instruction = Instruction.Create(OpCodes.Ldtoken, method); _visitorMock .Setup(visitor => visitor.VisitInlineTok(instruction, method)) .Returns(Unit.Value) .Verifiable(); instruction.Accept(_visitorMock.Object); _visitorMock.Verify(); }
public void Accept_InlineSigOperand_InvokesVisitor() { var callsite = new CallSite(CecilUtility.Import(typeof(object))); var instruction = Instruction.Create(OpCodes.Calli, callsite); _visitorMock .Setup(visitor => visitor.VisitInlineSig(instruction, callsite)) .Returns(Unit.Value) .Verifiable(); instruction.Accept(_visitorMock.Object); _visitorMock.Verify(); }
public void GetMatchingTargets_ReturnsAllSetupsThatTargetQueriedMethod() { ReadOnlyCollection <IRewriteTarget> setups = new List <IRewriteTarget> { TestDataFactory.CreateSetupTarget(() => Console.WriteLine()), TestDataFactory.CreateSetupTarget(() => Console.ReadLine()), TestDataFactory.CreateSetupTarget(() => Console.WriteLine()) }.AsReadOnly(); var subject = new RewriteTargetMatcher(_methodImporterMock.Object, setups); var result = subject.GetMatchingTargets(CecilUtility.Import(setups[0].Methods[0])); Assert.AreEqual(2, result.Count()); Assert.IsTrue(result.All(setup => setup.Methods[0] == setups[0].Methods[0])); }
public void VisitInlineArg_ByteArgument_EmitsInstruction() { var method = CecilUtility.Import(ReflectionUtility.GetMethod(() => int.Parse(string.Empty))).Resolve(); ParameterDefinition parameter = method.Parameters[0]; _generatorMock .Setup(generator => generator.Emit(System.Reflection.Emit.OpCodes.Ldarg_S, (byte)parameter.Index)) .Verifiable(); var subject = new ILGeneratorInstructionVisitor(_generatorMock.Object); var instruction = Instruction.Create(OpCodes.Ldarg_S, parameter); subject.VisitInlineArg(instruction, parameter); _generatorMock.Verify(); }
public void VisitInlineMethod_ConstructorOperand_EmitsInstruction() { var subject = new ILGeneratorInstructionVisitor(_generatorMock.Object); _generatorMock .Setup(generator => generator.Emit( System.Reflection.Emit.OpCodes.Newobj, It.Is <ConstructorInfo>(constructor => constructor.DeclaringType.Name == "MemoryStream"))) .Verifiable(); var method = ReflectionUtility.GetMethod(() => new MemoryStream()); var instruction = Instruction.Create(OpCodes.Newobj, CecilUtility.Import(method)); subject.VisitInlineMethod(instruction, (MethodReference)instruction.Operand); _generatorMock.Verify(); }
public void GetMatcher_ReturnsMatcherForProvidedTargets() { var targets = new[] { TestDataFactory.CreateSetupTarget(() => Console.WriteLine()), TestDataFactory.CreateSetupTarget(() => Console.ReadLine()) }; var subject = new RewriteTargetCollection(targets); var matcher = subject.GetMatcher(TestDataFactory.CreateModuleDefinition()); var result0 = matcher.GetMatchingTargets(CecilUtility.Import(targets[0].Methods[0])); var result1 = matcher.GetMatchingTargets(CecilUtility.Import(targets[1].Methods[0])); Assert.AreEqual(targets[0], result0.Single()); Assert.AreEqual(targets[1], result1.Single()); }
public void VisitInlineField_EmitsInstruction() { TypeDefinition type = CecilUtility.Import(GetType()).Resolve(); FieldDefinition fieldDefinition = type.Fields.First(f => f.Name == "TestField"); var subject = new ILGeneratorInstructionVisitor(_generatorMock.Object); _generatorMock .Setup(generator => generator.Emit( System.Reflection.Emit.OpCodes.Ldfld, It.Is <FieldInfo>(field => field.Name == fieldDefinition.Name))) .Verifiable(); var instruction = Instruction.Create(OpCodes.Ldfld, fieldDefinition); subject.VisitInlineField(instruction, fieldDefinition); _generatorMock.Verify(); }
public void Accepts_ModuleUnequalButSomeChildIsEqual_ReturnsTrue() { var query = CecilUtility.Import(typeof(object)).Resolve().Module; var child = CecilUtility.Import(GetType()).Resolve().Module; _moduleComparerMock.Setup(comparer => comparer.Equals(child, It.IsAny <ModuleReference>())).Returns(false); _moduleComparerMock.Setup(comparer => comparer.Equals( It.Is <ModuleReference>(module => IsAny(module, query, child)), It.Is <ModuleReference>(module => IsAny(module, query, child)))) .Returns(true); HashSet <DependencyGraphNode> nodes = new HashSet <DependencyGraphNode>(); nodes.Add(new DependencyGraphNode(child, _moduleComparerMock.Object)); _graphMock.SetupGet(graph => graph.Nodes).Returns(nodes); var subject = new DirectReferencesModuleFilter(_graphMock.Object, _moduleComparerMock.Object); bool result = subject.Accepts(query); Assert.IsTrue(result); }