コード例 #1
0
        public void Accepts_ReturnsTrue()
        {
            var subject = new AllowAllModuleFilter();

            Assert.IsTrue(subject.Accepts(null));
            Assert.IsTrue(subject.Accepts(CecilUtility.Import(typeof(object)).Resolve().Module));
        }
コード例 #2
0
        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]);
        }
コード例 #3
0
        static int Run(Options options)
        {
            var module1 = CecilUtility.ReadModule(options.File1);
            var module2 = CecilUtility.ReadModule(options.File2);

            FacadeModuleProcessor.MakePublicFacade(module1, options.IncludeInternals);
            FacadeModuleProcessor.MakePublicFacade(module2, options.IncludeInternals);

            var changes = ApiDiff.FindChanges(module1, module2);

            if (changes.Count == 0)
            {
                Console.WriteLine("No changes");
            }
            foreach (var changeGroup in changes.GroupBy(x => x.IsBreaking))
            {
                Console.WriteLine("{0} changes:", changeGroup.Key ? "Breaking" : "Non-breaking");
                foreach (var change in changeGroup)
                {
                    Console.WriteLine(change.Message);
                }
                Console.WriteLine();
            }

            return(0);
        }
コード例 #4
0
        public void TestFindChanges()
        {
            var directory = Path.GetDirectoryName(typeof(ApiDiffTest).Assembly.Location);

            var module1 = CecilUtility.ReadModule(Path.Combine(directory, "TestLibrary.V1.dll"));
            var module2 = CecilUtility.ReadModule(Path.Combine(directory, "TestLibrary.V2.dll"));

            FacadeModuleProcessor.MakePublicFacade(module1, false);
            FacadeModuleProcessor.MakePublicFacade(module2, false);

            var changes = ApiDiff.FindChanges(module1, module2);

            var diff         = NormalizeDiff(changes.Select(change => $"{(change.IsBreaking ? "B" : "N")} {change.Message}"));
            var expectedDiff = NormalizeDiff(File.ReadAllLines(Path.Join(directory, "expected-diff.txt")));

            var falseNegatives = diff.Except(expectedDiff).ToList();

            if (falseNegatives.Count != 0)
            {
                Console.WriteLine("false positives:");
                Console.Write(string.Join(Environment.NewLine, falseNegatives));
                Console.WriteLine();
            }
            var falsePositives = expectedDiff.Except(diff).ToList();

            if (falsePositives.Count != 0)
            {
                Console.WriteLine("false negatives:");
                Console.Write(string.Join(Environment.NewLine, falsePositives));
                Console.WriteLine();
            }

            CollectionAssert.AreEqual(expectedDiff, diff);
        }
コード例 #5
0
        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);
        }
コード例 #6
0
        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);
        }
コード例 #7
0
        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);
        }
コード例 #8
0
        static IReadOnlyList <TypeChanges> FindChanges(Stream stream1, Stream stream2)
        {
            var module1 = CecilUtility.ReadModule(stream1);
            var module2 = CecilUtility.ReadModule(stream2);

            FacadeModuleProcessor.MakePublicFacade(module1, keepInternalTypes: false);
            FacadeModuleProcessor.MakePublicFacade(module2, keepInternalTypes: false);

            return(ApiDiff.FindTypeChanges(module1, module2));
        }
コード例 #9
0
        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();
        }
コード例 #10
0
        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();
        }
コード例 #11
0
        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();
        }
コード例 #12
0
        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());
        }
コード例 #13
0
        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();
        }
コード例 #14
0
        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();
        }
コード例 #15
0
        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]));
        }
コード例 #16
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();
        }
コード例 #17
0
        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();
        }
コード例 #18
0
        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());
        }
コード例 #19
0
        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();
        }
コード例 #20
0
        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);
        }