예제 #1
0
        public void TestDependencyExistingProperty()
        {
            PatchInstaller   installer = new PatchInstaller(new MockModuleSource(module));
            StandardPatch    patch     = new StandardPatch("TestPatch");
            TypeModification type      = patch.Module(module.Name, ModificationKind.FailIfMissing, true).Type(ReadOnlyNamespace, NormalClass, ModificationKind.FailIfMissing);

            type.Attributes = TypeAttributes.BeforeFieldInit;
            MethodModification propertyGet = type.Method("get_" + StringProperty, new MethodSignature(CallingConvention.HasThis, 0u, new CorLibTypeSignature(new TypeReference(patch.Module("System.Runtime", ModificationKind.FailIfMissing, true), "System", "String", new ModuleReference("System.Runtime")), ElementType.String)), ModificationKind.FailIfMissing);

            propertyGet.Attributes = MethodAttributes.HideBySig | MethodAttributes.SpecialName;
            type.Property(StringProperty, new PropertySignature(true, new CorLibTypeSignature(new TypeReference(patch.Module("System.Runtime", ModificationKind.FailIfMissing, true), "System", "String", new ModuleReference("System.Runtime")), ElementType.String)), ModificationKind.FailIfMissing).Get(propertyGet);
            installer.Add(patch);
            installer.Install();
        }
        private IEnumerable <MethodModification> GetDifferences(IEnumerable <MethodParserInfo> oldDeclaredMethods, IEnumerable <MethodParserInfo> newDeclaredMethods)
        {
            var result         = new List <MethodModification>();
            var methodsAdded   = newDeclaredMethods.Except(oldDeclaredMethods);
            var methodsRemoved = oldDeclaredMethods.Except(newDeclaredMethods);
            var methodsUpdated = from oldMethod in oldDeclaredMethods
                                 from newMethod in newDeclaredMethods
                                 where oldMethod.Equals(newMethod)
                                 select new MethodUpdateInfo(newMethod.MethodDescriptor,
                                                             oldMethod.DeclarationSyntaxNode,
                                                             newMethod.DeclarationSyntaxNode);

            foreach (var methodInfo in methodsAdded)
            {
                var modification = new MethodModification(methodInfo.MethodDescriptor, ModificationKind.MethodAdded);
                result.Add(modification);
            }

            foreach (var methodInfo in methodsRemoved)
            {
                var modification = new MethodModification(methodInfo.MethodDescriptor, ModificationKind.MethodRemoved);
                result.Add(modification);
            }

            // Compare the body of each potentially updated method to see if it was acutally updated or not
            foreach (var methodInfo in methodsUpdated)
            {
                var oldMethodBody = methodInfo.OldDeclarationNode.ToString();
                var newMethodBody = methodInfo.NewDeclarationNode.ToString();

                // TODO: Hack! We should compare the method bodies better!
                if (oldMethodBody != newMethodBody)
                {
                    var modification = new MethodModification(methodInfo.MethodDescriptor, ModificationKind.MethodUpdated);
                    result.Add(modification);
                }
            }

            return(result);
        }
예제 #3
0
        public async Task TestDocumentsDiff()
        {
            #region original source code
            var source = @"
using System;
class Program
{
    public static void MethodToRemove()
    {
		Console.WriteLine(1);
    }

    public static void MethodToUpdate()
    {
		MethodToRemove();
    }

    public static void Main()
    {
		MethodToUpdate();
    }
}";
            #endregion

            #region modified source code
            var newSource = @"
using System;
class Program
{
	public static void MethodToAdd()
    {
		Console.WriteLine(2);
    }

    public static void MethodToUpdate()
    {
		MethodToAdd();
    }

    public static void Main()
    {
		MethodToUpdate();
    }
}";
            #endregion

            Document document;
            Document newDocument;

            GetDocumentsFromSource(source, newSource, out document, out newDocument);

            var documentDiff  = new DocumentDiff();
            var modifications = await documentDiff.GetDifferencesAsync(document, newDocument);

            var remove = new MethodModification(new MethodDescriptor("Program", "MethodToRemove", true), ModificationKind.MethodRemoved);
            var update = new MethodModification(new MethodDescriptor("Program", "MethodToUpdate", true), ModificationKind.MethodUpdated);
            var add    = new MethodModification(new MethodDescriptor("Program", "MethodToAdd", true), ModificationKind.MethodAdded);

            Assert.IsTrue(modifications.Contains(remove));
            Assert.IsTrue(modifications.Contains(update));
            Assert.IsTrue(modifications.Contains(add));
        }