Example #1
0
        public void Properties()
        {
            const string code = @"    
            public class Class1 
            {
                public static int SomeValue
                {
                    get { return i; }
                    set { }
                }
            }";

            CSharpParser parser = new CSharpParser();

            parser.FormatSettings.InlineSingleLineGettersAndSetters = true;
            parser.FormatSettings.PutBracesOnNewLines = false;
            parser.ParseCode(code);

            ICodeRoot codeRoot = parser.CreatedCodeRoot;
            Class     clazz    = (Class)codeRoot.WalkChildren()[0];

            Property con = (Property)clazz.WalkChildren()[0];

            Assert.That(con.Name, Is.EqualTo("SomeValue"));
            Assert.That(con.DataType.ToString(), Is.EqualTo("int"));
            Assert.That(con.Modifiers, Has.Count(2));
            Assert.That(con.Modifiers[0], Is.EqualTo("public"));
            Assert.That(con.Modifiers[1], Is.EqualTo("static"));
            Assert.That(con.GetAccessor, Is.Not.Null);
            Assert.That(con.GetAccessor.AccessorType, Is.EqualTo(PropertyAccessor.AccessorTypes.Get));
            Assert.That(con.GetAccessor.BodyText, Is.EqualTo("{ return i; }"));
            Assert.That(con.SetAccessor, Is.Not.Null);
            Assert.That(con.SetAccessor.AccessorType, Is.EqualTo(PropertyAccessor.AccessorTypes.Set));
            Assert.That(con.SetAccessor.BodyText, Is.EqualTo("{ }"));
        }
        public string RunActions(string code, ICodeRoot codeRoot, bool standardiseLineBreaks)
        {
            if (standardiseLineBreaks)
                return RunActions(new StringBuilder(Helper.StandardizeLineBreaks(code, "\n")), codeRoot);

            return RunActions(new StringBuilder(code), codeRoot);
        }
Example #3
0
        public void Interface_Indexer()
        {
            const string code = @"    
            public interface Interface1 
            {
                string this[int i] { get; set; }
            }
            ";

            CSharpParser parser = new CSharpParser();

            parser.ParseCode(code);

            ICodeRoot codeRoot = parser.CreatedCodeRoot;
            Interface clazz    = (Interface)codeRoot.WalkChildren()[0];

            InterfaceIndexer inter = (InterfaceIndexer)clazz.WalkChildren()[0];

            Assert.That(inter.DataType.ToString(), Is.EqualTo("string"));
            Assert.That(inter.GetAccessor, Is.Not.Null);
            Assert.That(inter.SetAccessor, Is.Not.Null);
            Assert.That(inter.Parameters, Has.Count(1));
            Assert.That(inter.Parameters[0].Name, Is.EqualTo("i"));
            Assert.That(inter.Parameters[0].DataType, Is.EqualTo("int"));
        }
Example #4
0
        public void Methods()
        {
            const string code = @"    
            public class Class1 
            {
                public void Class1(string param1) 
                {
                    int i = 0;
                }
            }
            ";

            CSharpParser parser = new CSharpParser();

            parser.ParseCode(code);

            ICodeRoot codeRoot = parser.CreatedCodeRoot;
            Class     clazz    = (Class)codeRoot.WalkChildren()[0];

            Function con = (Function)clazz.WalkChildren()[0];

            Assert.That(con.Name, Is.EqualTo("Class1"));
            Assert.That(con.ReturnType.ToString(), Is.EqualTo("void"));
            Assert.That(con.Modifiers, Has.Count(1));
            Assert.That(con.Modifiers[0], Is.EqualTo("public"));
            Assert.That(con.Parameters, Has.Count(1));
            Assert.That(con.Parameters[0].Name, Is.EqualTo("param1"));
            Assert.That(con.Parameters[0].DataType, Is.EqualTo("string"));
            Assert.That(con.BodyText, Is.EqualTo("{\r\n\tint i = 0;\r\n}\r\n"));
        }
Example #5
0
        public void GenericMethod_WithConstraints()
        {
            const string code = @"    
            public class Class1 
            {
                public void Method1<T>(T param) where T : struct
				{

				}
            }
            ";

            CSharpParser parser = new CSharpParser();

            parser.ParseCode(code);

            if (parser.ErrorOccurred)
            {
                Assert.Fail(parser.GetFormattedErrors());
            }

            ICodeRoot codeRoot = parser.CreatedCodeRoot;
            Class     clazz    = (Class)codeRoot.WalkChildren()[0];

            Function function = (Function)clazz.WalkChildren()[0];

            Assert.That(function.Name, Is.EqualTo("Method1"));
            Assert.That(function.GenericParameters.Count, Is.EqualTo(1));
            Assert.That(function.GenericParameters[0], Is.EqualTo("T"));
            Assert.That(function.GenericConstraintClause, Is.EqualTo("where T : struct"));
            Assert.That(function.ReturnType.ToString(), Is.EqualTo("void"));
            Assert.That(function.Parameters, Has.Count(1));
            Assert.That(function.Parameters[0].Name, Is.EqualTo("param"));
            Assert.That(function.Parameters[0].DataType, Is.EqualTo("T"));
        }
Example #6
0
        public void Operators()
        {
            const string code = @"    
            public class Class1 
            {
                public static int operator +(Class1 self, string param1)
                {
                    int i = 0;
                    return i;
                }
            }";

            CSharpParser parser = new CSharpParser();

            parser.ParseCode(code);

            ICodeRoot codeRoot = parser.CreatedCodeRoot;
            Class     clazz    = (Class)codeRoot.WalkChildren()[0];

            Operator con = (Operator)clazz.WalkChildren()[0];

            Assert.That(con.Name, Is.EqualTo("+"));
            Assert.That(con.DataType.ToString(), Is.EqualTo("int"));
            Assert.That(con.Modifiers, Has.Count(2));
            Assert.That(con.Modifiers[0], Is.EqualTo("public"));
            Assert.That(con.Modifiers[1], Is.EqualTo("static"));
            Assert.That(con.Parameters, Has.Count(2));
            Assert.That(con.Parameters[0].Name, Is.EqualTo("self"));
            Assert.That(con.Parameters[0].DataType, Is.EqualTo("Class1"));
            Assert.That(con.Parameters[1].Name, Is.EqualTo("param1"));
            Assert.That(con.Parameters[1].DataType, Is.EqualTo("string"));
            Assert.That(con.BodyText, Is.EqualTo("{\r\n\tint i = 0;\r\n\treturn i;\r\n}\r\n"));
        }
        public void Matching_Successful()
        {
            CodeRootMap map = new CodeRootMap();

            CodeRoot userroot = CreateBasicRoot();

            map.AddCodeRoot(userroot, Version.User);

            CodeRoot prevroot = CreateBasicRoot();

            map.AddCodeRoot(prevroot, Version.PrevGen);

            CodeRoot templateroot = CreateBasicRoot();

            map.AddCodeRoot(templateroot, Version.NewGen);

            string result = map.GetMergedCodeRoot().ToString();

            Assertions.StringContains(result, "class Class1", 1);
            Assertions.StringContains(result, "namespace ArchAngel.Tests", 1);

            map.MatchConstructs(map.GetExactNode(userroot.Namespaces[0]), userroot.Namespaces[0].Classes[0], null, prevroot.Namespaces[0].Classes[0]);
            map.Diff();

            Assert.That(map.ChildNodes[0].ChildNodes, Has.Count(2));
            Assert.That(map.ChildNodes[0].ChildNodes[0].DetermineMissingConstructs(), Is.EqualTo(MissingObject.User | MissingObject.PrevGen));
            Assert.That(map.ChildNodes[0].ChildNodes[1].DetermineMissingConstructs(), Is.EqualTo(MissingObject.NewGen));

            ICodeRoot merged = map.GetMergedCodeRoot();

            result = merged.ToString();
            Assertions.StringContains(result, "class Class1", 2);
            Assertions.StringContains(result, "namespace ArchAngel.Tests", 1);
        }
Example #8
0
 /// <summary>
 /// Copies top level items from this ICodeRoot into the given ICodeRoot
 /// </summary>
 /// <param name="codeRoot">The ICodeRoot to copy items into.</param>
 public void ShallowCloneInto(ICodeRoot codeRoot)
 {
     if ((codeRoot is CodeRoot) == false)
     {
         return;
     }
 }
Example #9
0
        public void Namespaces()
        {
            string code = @"namespace n1 { }";

            CSharpParser parser = new CSharpParser();

            parser.ParseCode(code);

            ICodeRoot codeRoot = parser.CreatedCodeRoot;
            Namespace n1       = (Namespace)codeRoot.WalkChildren()[0];

            Assert.That(n1.Name, Is.EqualTo("n1"));

            code = @"namespace n1 { namespace n2 { } }";

            parser = new CSharpParser();
            parser.ParseCode(code);

            codeRoot = parser.CreatedCodeRoot;
            n1       = (Namespace)codeRoot.WalkChildren()[0];
            Namespace n2 = n1.InnerNamespaces[0];

            Assert.That(n2.Name, Is.EqualTo("n2"));
            Assert.That(n2.FullyQualifiedName, Is.EqualTo("n1.n2"));
        }
Example #10
0
        public void Constructors()
        {
            const string code = @"    
            public class Class1 
            {
                public Class1(string param1) 
                {
                }
            }
            ";

            CSharpParser parser = new CSharpParser();

            parser.ParseCode(code);

            ICodeRoot codeRoot = parser.CreatedCodeRoot;
            Class     clazz    = (Class)codeRoot.WalkChildren()[0];

            Constructor con = (Constructor)clazz.WalkChildren()[0];

            Assert.That(con.Name, Is.EqualTo("Class1"));
            Assert.That(con.Modifiers, Has.Count(1));
            Assert.That(con.Modifiers[0], Is.EqualTo("public"));
            Assert.That(con.Parameters, Has.Count(1));
            Assert.That(con.Parameters[0].Name, Is.EqualTo("param1"));
            Assert.That(con.Parameters[0].DataType, Is.EqualTo("string"));
            Assert.That(con.BodyText, Is.EqualTo("{\r\n}\r\n"));
        }
Example #11
0
        public void Indexers()
        {
            const string code = @"    
            public class Class1 
            {
                public static int this[int i]
                {
                    get { return i; }
                    set { }
                }
            }";

            CSharpParser parser = new CSharpParser();

            parser.ParseCode(code);

            ICodeRoot codeRoot = parser.CreatedCodeRoot;
            Class     clazz    = (Class)codeRoot.WalkChildren()[0];

            Indexer con = (Indexer)clazz.WalkChildren()[0];

            Assert.That(con.Name, Is.Empty);
            Assert.That(con.DataType.ToString(), Is.EqualTo("int"));
            Assert.That(con.Parameters, Has.Count(1));
            Assert.That(con.Parameters[0].Name, Is.EqualTo("i"));
            Assert.That(con.Parameters[0].DataType, Is.EqualTo("int"));
            Assert.That(con.GetAccessor, Is.Not.Null);
            Assert.That(con.GetAccessor.AccessorType, Is.EqualTo(PropertyAccessor.AccessorTypes.Get));
            Assert.That(con.GetAccessor.BodyText, Is.EqualTo("{ return i; }"));
            Assert.That(con.SetAccessor, Is.Not.Null);
            Assert.That(con.SetAccessor.AccessorType, Is.EqualTo(PropertyAccessor.AccessorTypes.Set));
            Assert.That(con.SetAccessor.BodyText, Is.EqualTo("{ }"));
        }
        public void Map_Contains_CodeRoot()
        {
            CodeRootMap    map      = new CodeRootMap();
            ICodeRoot      coderoot = mocks.StrictMock <ICodeRoot>();
            IBaseConstruct bc1      = mocks.DynamicMock <IBaseConstruct>();
            IBaseConstruct bc2      = mocks.DynamicMock <IBaseConstruct>();

            Utility.SetupBaseConstructs(mocks, bc1, bc2, coderoot);

            using (mocks.Playback())
            {
                map.AddCodeRoot(coderoot, Version.User);
            }

            Assert.That(map.ChildNodes, Has.Count(1));
            Assert.That(map.AllNodes, Has.Count(2));
            Assert.That(map.ChildNodes[0], Is.Not.Null);
            Assert.That(map.ChildNodes[0].IsTreeRoot, Is.False);
            Assert.That(map.ChildNodes[0].UserObj, Is.SameAs(bc1));
            Assert.That(map.ChildNodes[0].Omit, Is.False);
            Assert.That(map.ChildNodes[0].ParentNode, Is.SameAs(map));
            Assert.That(map.ChildNodes[0].ParentTree, Is.SameAs(map));

            CodeRootMapNode node = map.ChildNodes[0].ChildNodes[0];

            Assert.That(node, Is.Not.Null);
            Assert.That(node.IsTreeRoot, Is.False);
            Assert.That(node.UserObj, Is.SameAs(bc2));
            Assert.That(node.Omit, Is.False);
            Assert.That(node.ParentNode, Is.SameAs(map.ChildNodes[0]));
            Assert.That(node.ParentTree, Is.SameAs(map));
        }
Example #13
0
        /// <summary>
        /// Generates the Merged CodeRoot object.
        /// </summary>
        /// <exception cref="InvalidOperationException">If no code roots have been added, or there is a conflict.</exception>
        /// <returns></returns>
        public ICodeRoot GetMergedCodeRoot()
        {
            ICodeRoot originalCodeRoot = coderoots.GetFirstNonNullObject();

            if (originalCodeRoot == null)
            {
                throw new InvalidOperationException("Cannot merge code roots when none have been added");
            }
            ICodeRoot newCodeRoot = originalCodeRoot.NewInstance();

            originalCodeRoot.ShallowCloneInto(newCodeRoot);

            foreach (CodeRootMapNode child in children)
            {
                MissingObject missingObjects = child.DetermineMissingConstructs();
                if ((missingObjects & MissingObject.User) != 0 &&
                    (missingObjects & MissingObject.NewGen) != 0)
                {
                    // Only PrevGen here. Ignore it.
                    continue;
                }

                newCodeRoot.AddChild(child.GetMergedBaseConstruct());
            }

            return(newCodeRoot);
        }
Example #14
0
        public void Delegates()
        {
            const string code = @"    
            public class Class1 
            {
                public delegate int Delegate1(string param1, Class1 p2);
            }
            ";

            CSharpParser parser = new CSharpParser();

            parser.ParseCode(code);

            ICodeRoot codeRoot = parser.CreatedCodeRoot;
            Class     clazz    = (Class)codeRoot.WalkChildren()[0];

            Delegate con = (Delegate)clazz.WalkChildren()[0];

            Assert.That(con.Name, Is.EqualTo("Delegate1"));
            Assert.That(con.Modifiers, Has.Count(1));
            Assert.That(con.Modifiers[0], Is.EqualTo("public"));
            Assert.That(con.ReturnType.Name, Is.EqualTo("int"));
            Assert.That(con.Parameters, Has.Count(2));
            Assert.That(con.Parameters[0].Name, Is.EqualTo("param1"));
            Assert.That(con.Parameters[0].DataType, Is.EqualTo("string"));
            Assert.That(con.Parameters[1].Name, Is.EqualTo("p2"));
            Assert.That(con.Parameters[1].DataType, Is.EqualTo("Class1"));
        }
Example #15
0
        private void FixTextRanges(ICodeRoot root, ActionResult result)
        {
            if (!result.ChangeApplied ||
                result.NumCharactersInserted == 0)
            {
                return;
            }
            var constructs = root.WalkTree().ToList();

            foreach (var bc in constructs)
            {
                if (bc.TextRange.StartOffset > result.ChangeStartIndex)
                {
                    bc.TextRange.StartOffset += result.NumCharactersInserted;
                    bc.TextRange.EndOffset   += result.NumCharactersInserted;
                }
                else if (bc.TextRange.StartOffset < result.ChangeStartIndex &&
                         bc.TextRange.EndOffset > result.ChangeStartIndex)
                {
                    bc.TextRange.EndOffset += result.NumCharactersInserted;
                }
                //else if (bc.TextRange.StartOffset == result.ChangeStartIndex)
                //    bc.TextRange.EndOffset += result.NumCharactersInserted; // GFH
            }
        }
Example #16
0
        public void Interface_Methods()
        {
            const string code = @"    
            public interface Interface1 
            {
                string Method1(int param, Interface1 i);
            }
            ";

            CSharpParser parser = new CSharpParser();

            parser.ParseCode(code);

            ICodeRoot codeRoot = parser.CreatedCodeRoot;
            Interface clazz    = (Interface)codeRoot.WalkChildren()[0];

            InterfaceMethod inter = (InterfaceMethod)clazz.WalkChildren()[0];

            Assert.That(inter.Name, Is.EqualTo("Method1"));
            Assert.That(inter.ReturnType.ToString(), Is.EqualTo("string"));
            Assert.That(inter.Parameters, Has.Count(2));
            Assert.That(inter.Parameters[0].Name, Is.EqualTo("param"));
            Assert.That(inter.Parameters[0].DataType, Is.EqualTo("int"));
            Assert.That(inter.Parameters[1].Name, Is.EqualTo("i"));
            Assert.That(inter.Parameters[1].DataType, Is.EqualTo("Interface1"));
        }
Example #17
0
        public void Regions()
        {
            const string code = @"
            namespace N1
            {
                #region Region Name
                public class Class1 
                {
                }
                #endregion
            }";

            CSharpParser parser = new CSharpParser();

            parser.ParseCode(code);

            ICodeRoot codeRoot = parser.CreatedCodeRoot;
            Namespace ns       = (Namespace)codeRoot.WalkChildren()[0];

            Assert.That(ns.WalkChildren(), Has.Count(1));

            Region region = ns.SortedConstructs[0] as Region;

            Assert.IsNotNull(region);
            Assert.That(region.Name, Is.EqualTo("Region Name"));

            Assert.That(region.WalkChildren(), Has.Count(1));
            Assert.That(region.SortedConstructs[0], Is.InstanceOfType(typeof(Class)));
        }
Example #18
0
        public void GenericInterfaceMethod_WithConstraints()
        {
            const string code = @"    
            public interface Interface1 
            {
                string Method1<T>(int param, T i) where T : class;
            }
            ";

            CSharpParser parser = new CSharpParser();

            parser.ParseCode(code);

            if (parser.ErrorOccurred)
            {
                Assert.Fail(parser.GetFormattedErrors());
            }

            ICodeRoot codeRoot = parser.CreatedCodeRoot;
            Interface clazz    = (Interface)codeRoot.WalkChildren()[0];

            InterfaceMethod inter = (InterfaceMethod)clazz.WalkChildren()[0];

            Assert.That(inter.Name, Is.EqualTo("Method1"));
            Assert.That(inter.GenericParameters.Count, Is.EqualTo(1));
            Assert.That(inter.GenericParameters[0], Is.EqualTo("T"));
            Assert.That(inter.GenericConstraintClause, Is.EqualTo("where T : class"));
            Assert.That(inter.ReturnType.ToString(), Is.EqualTo("string"));
            Assert.That(inter.Parameters, Has.Count(2));
            Assert.That(inter.Parameters[0].Name, Is.EqualTo("param"));
            Assert.That(inter.Parameters[0].DataType, Is.EqualTo("int"));
            Assert.That(inter.Parameters[1].Name, Is.EqualTo("i"));
            Assert.That(inter.Parameters[1].DataType, Is.EqualTo("T"));
        }
 public void SetUp()
 {
     var controller = new CSharpController();
     codeRoot = new CodeRoot(controller);
     var ns = new Namespace(controller, "Namespace1") { Index = 0 };
     codeRoot.AddChild(ns);
     ns.AddChild(new Class(controller, "Class1") { Index = 1 });
 }
Example #20
0
        public string RunActions(string code, ICodeRoot codeRoot, bool standardiseLineBreaks)
        {
            if (standardiseLineBreaks)
            {
                return(RunActions(new StringBuilder(Helper.StandardizeLineBreaks(code, "\n")), codeRoot));
            }

            return(RunActions(new StringBuilder(code), codeRoot));
        }
Example #21
0
        /// <summary>
        /// Adds a CodeRoot to the tree.
        /// </summary>
        /// <param name="codeRoot">The CodeRoot to add.</param>
        /// <param name="type">The Version of the CodeRoot to add.</param>
        public void AddCodeRoot(ICodeRoot codeRoot, Version type)
        {
            coderoots.SetObject(codeRoot, type);

            foreach (IBaseConstruct bc in codeRoot.WalkChildren())
            {
                AddBaseConstructAsChild(bc, type);
            }
        }
Example #22
0
        private string RunActions(StringBuilder builder, ICodeRoot codeRoot)
        {
            foreach (var action in ActionsToPerform)
            {
                var result = action.ApplyActionTo(builder);
                FixTextRanges(codeRoot, result);
            }

            return(builder.ToString());
        }
        public void Diff_Result_Is_Change_Constructor()
        {
            string original = Path.Combine(ResourcePath, "Class1.cs");
            string changed  = Path.Combine(ResourcePath, "Class1_WithConstructor.cs");

            ICodeRoot originalCR = GetCodeRoot(original);

            AssertBasicConstructsExist(originalCR);

            TestDiff(original, changed, true);
        }
        public void User_File_Doesnt_Exist_Diff_Result_Is_Exact_Copy()
        {
            string original = Path.Combine(ResourcePath, "Class1.cs");

            CodeRootMap codeRootMap = new CodeRootMap();

            ICodeRoot codeRoot = Diffing_Two_Different_Basic_CSharp_Files.GetCodeRoot(original);

            codeRootMap.AddCodeRoot(codeRoot, Version.NewGen);

            Assert.That(codeRootMap.Diff(), Is.EqualTo(TypeOfDiff.ExactCopy));
        }
Example #25
0
        public void Comments()
        {
            const string code   = @"
            /// <summary>
            /// Xml Comment line\r\n
			/// break.
            ///</summary>
            public class Class1 // Class Trailing Comment
            {
                // Comment1-1
                // Comment1-2
                public int i = 0; // Trailing Comment
                // Comment2-1
                public int j = 0;
            }";
            CSharpParser parser = new CSharpParser();

            parser.ParseCode(code);

            ICodeRoot codeRoot = parser.CreatedCodeRoot;

            Assert.That(codeRoot, Is.InstanceOfType(typeof(CodeRoot)));
            Assert.That(codeRoot.WalkChildren(), Has.Count(1));

            Class clazz = (Class)codeRoot.WalkChildren()[0];

            Assert.That(clazz, Is.Not.Null);

            Assert.That(clazz.Name, Is.EqualTo("Class1"));
            Assert.That(clazz.XmlComments, Has.Count(4));
            Assert.That(clazz.XmlComments[0].Trim(), Is.EqualTo(@"<summary>"));
            Assert.That(clazz.XmlComments[1].Trim(), Is.EqualTo(@"Xml Comment line\r\n"));
            Assert.That(clazz.XmlComments[2].Trim(), Is.EqualTo(@"break."));
            Assert.That(clazz.XmlComments[3].Trim(), Is.EqualTo(@"</summary>"));

            Assert.That(clazz.Comments.TrailingComment, Is.EqualTo("// Class Trailing Comment"));

            Assert.That(clazz.WalkChildren(), Has.Count(2));
            Field field = (Field)clazz.WalkChildren()[0];

            Assert.That(field.Name, Is.EqualTo("i"));
            Assert.That(field.Comments.PreceedingComments, Has.Count(2));
            Assert.That(field.Comments.PreceedingComments[0], Is.EqualTo("// Comment1-1"));
            Assert.That(field.Comments.PreceedingComments[1], Is.EqualTo("// Comment1-2"));
            Assert.That(field.Comments.TrailingComment, Is.EqualTo("// Trailing Comment"));

            field = (Field)clazz.WalkChildren()[1];
            Assert.That(field.Name, Is.EqualTo("j"));
            Assert.That(field.Comments.PreceedingComments, Has.Count(1));
            Assert.That(field.Comments.PreceedingComments[0], Is.EqualTo("// Comment2-1"));
        }
        internal static void TestFiles_UserAndTemplate(string original, string changed, TypeOfDiff diffType)
        {
            CodeRootMap codeRootMap = new CodeRootMap();

            ICodeRoot codeRoot = GetCodeRoot(original);

            codeRootMap.AddCodeRoot(codeRoot, Version.PrevGen);

            codeRoot = GetCodeRoot(changed);
            codeRootMap.AddCodeRoot(codeRoot, Version.User);
            codeRootMap.AddCodeRoot(codeRoot, Version.NewGen);

            Assert.That(codeRootMap.Diff(), Is.EqualTo(diffType));
        }
        public void SetUp()
        {
            var controller = new CSharpController();

            codeRoot = new CodeRoot(controller);
            var ns = new Namespace(controller, "Namespace1")
            {
                Index = 0
            };

            codeRoot.AddChild(ns);
            ns.AddChild(new Class(controller, "Class1")
            {
                Index = 1
            });
        }
        public void Diff_Result_Is_Exact_Copy()
        {
            string original = Path.Combine(ResourcePath, "Class1.cs");

            CodeRootMap codeRootMap = new CodeRootMap();

            CSharpParser formatter = new CSharpParser();

            formatter.ParseCode(File.ReadAllText(original));
            ICodeRoot codeRoot = formatter.CreatedCodeRoot;

            codeRootMap.AddCodeRoot(codeRoot, Version.User);
            codeRootMap.AddCodeRoot(codeRoot, Version.NewGen);
            codeRootMap.AddCodeRoot(codeRoot, Version.PrevGen);

            Assert.That(codeRootMap.Diff(), Is.EqualTo(TypeOfDiff.ExactCopy));
        }
Example #29
0
        public void GenericClasses()
        {
            const string code =
                @"    
            public class Class1<T> : IComparer<T>
            {
            }
            ";

            CSharpParser parser = new CSharpParser();

            parser.ParseCode(code);

            ICodeRoot codeRoot = parser.CreatedCodeRoot;
            Class     clazz    = (Class)codeRoot.WalkChildren()[0];

            Assert.That(clazz.Name, Is.EqualTo("Class1<T>"));
            Assert.That(clazz.BaseNames[0], Is.EqualTo("IComparer<T>"));
        }
Example #30
0
        public void MultiLineComments()
        {
            const string code   = @"
            /* adfjlaskdjflkasdjflkjdf */
            public class Class1 
            {
                /* Comment1-1
                 * Comment1-2 */
                public int i = 0; /* Trailing Comment
                * Comment2-1 */
                public int j = 0;
            }";
            CSharpParser parser = new CSharpParser();

            parser.ParseCode(code);

            ICodeRoot codeRoot = parser.CreatedCodeRoot;

            Assert.That(codeRoot, Is.InstanceOfType(typeof(CodeRoot)));
            Assert.That(codeRoot.WalkChildren(), Has.Count(1));

            Class clazz = (Class)codeRoot.WalkChildren()[0];

            Assert.That(clazz, Is.Not.Null);

            Assert.That(clazz.Name, Is.EqualTo("Class1"));
            Assert.That(clazz.Comments.PreceedingComments, Has.Count(1));
            Assert.That(clazz.Comments.PreceedingComments[0].Trim(), Is.EqualTo("/* adfjlaskdjflkasdjflkjdf */"));

            Assert.That(clazz.WalkChildren(), Has.Count(2));
            Field field = (Field)clazz.WalkChildren()[0];

            Assert.That(field.Name, Is.EqualTo("i"));
            Assert.That(field.Comments.PreceedingComments, Has.Count(1));
            Assert.That(field.Comments.PreceedingComments[0], Is.EqualTo(@"/* Comment1-1
                 * Comment1-2 */"));
            Assert.That(field.Comments.TrailingComment, Is.EqualTo(@"/* Trailing Comment
                * Comment2-1 */"));

            field = (Field)clazz.WalkChildren()[1];
            Assert.That(field.Name, Is.EqualTo("j"));
            Assert.That(field.Comments.PreceedingComments, Has.Count(0));
        }
Example #31
0
        public void UsingStatements()
        {
            string code = @"using System;
namespace n1 { }";

            CSharpParser parser = new CSharpParser();

            parser.FormatSettings.ReorderBaseConstructs = false;
            parser.ParseCode(code);

            ICodeRoot codeRoot = parser.CreatedCodeRoot;

            Assert.That(((CodeRoot)codeRoot).UsingStatements[0].ToString(), Is.EqualTo("using System;"));
            Namespace n1 = (Namespace)codeRoot.WalkChildren()[0];

            Assert.That(n1.Name, Is.EqualTo("n1"));
            Assert.That(n1.UsingStatements, Is.Empty);

            code = @"using System;
namespace n1 
{ 
    using System.Web;
    namespace n2 
    {
        using Slyce.Common;
    }
}";

            parser = new CSharpParser();
            parser.ParseCode(code);

            codeRoot = parser.CreatedCodeRoot;
            Assert.That(((CodeRoot)codeRoot).UsingStatements[0].ToString(), Is.EqualTo("using System;"));

            n1 = (Namespace)codeRoot.WalkChildren()[0];
            Assert.That(n1.UsingStatements[0].ToString(), Is.EqualTo("using System.Web;"));

            Namespace n2 = n1.InnerNamespaces[0];

            Assert.That(n2.Name, Is.EqualTo("n2"));
            Assert.That(n2.FullyQualifiedName, Is.EqualTo("n1.n2"));
            Assert.That(n2.UsingStatements[0].ToString(), Is.EqualTo("using Slyce.Common;"));
        }
Example #32
0
        public void Interface_Events()
        {
            const string code = @"    
            public interface Interface1 
            {
                event Delegate1 Event1;
            }
            ";

            CSharpParser parser = new CSharpParser();

            parser.ParseCode(code);

            ICodeRoot codeRoot = parser.CreatedCodeRoot;
            Interface clazz    = (Interface)codeRoot.WalkChildren()[0];

            InterfaceEvent enu = (InterfaceEvent)clazz.WalkChildren()[0];

            Assert.That(enu.Name, Is.EqualTo("Event1"));
            Assert.That(enu.DataType.ToString(), Is.EqualTo("Delegate1"));
        }
        private void FixTextRanges(ICodeRoot root, ActionResult result)
        {
            if (!result.ChangeApplied ||
                result.NumCharactersInserted == 0)
            {
                return;
            }
            var constructs = root.WalkTree().ToList();

            foreach (var bc in constructs)
            {
                if (bc.TextRange.StartOffset > result.ChangeStartIndex)
                {
                    bc.TextRange.StartOffset += result.NumCharactersInserted;
                    bc.TextRange.EndOffset += result.NumCharactersInserted;
                }
                else if (bc.TextRange.StartOffset < result.ChangeStartIndex &&
                    bc.TextRange.EndOffset > result.ChangeStartIndex)
                    bc.TextRange.EndOffset += result.NumCharactersInserted;
                //else if (bc.TextRange.StartOffset == result.ChangeStartIndex)
                //    bc.TextRange.EndOffset += result.NumCharactersInserted; // GFH
            }
        }
        private string RunActions(StringBuilder builder, ICodeRoot codeRoot)
        {
            foreach (var action in ActionsToPerform)
            {
                var result = action.ApplyActionTo(builder);
                FixTextRanges(codeRoot, result);
            }

            return builder.ToString();
        }
        public static void SetupBaseConstructs(MockRepository mocks, IBaseConstruct bc1, IBaseConstruct bc2, ICodeRoot coderoot)
        {
            using(mocks.Record())
            {
                Expect.Call(bc1.ShortName).Repeat.Any().Return("bc1");
                Expect.Call(bc2.ShortName).Repeat.Any().Return("bc2");

                Expect.Call(bc1.IsTheSame(bc1)).Repeat.Any().Return(true);
                Expect.Call(bc2.IsTheSame(bc2)).Repeat.Any().Return(true);

                Expect.Call(bc2.WalkChildren()).Repeat.AtLeastOnce().Return(new List<IBaseConstruct>().AsReadOnly());

                List<IBaseConstruct> bc1Children = new List<IBaseConstruct>();
                bc1Children.Add(bc2);
                Expect.Call(bc1.WalkChildren()).Repeat.AtLeastOnce().Return(bc1Children.AsReadOnly());

                List<IBaseConstruct> rootChildren = new List<IBaseConstruct>();
                rootChildren.Add(bc1);
                Expect.On(coderoot).Call(coderoot.WalkChildren()).Repeat.AtLeastOnce().Return(rootChildren.AsReadOnly());
            }
        }
        /// <summary>
        /// Returns the IBaseConstruct for Class1
        /// </summary>
        /// <param name="cr"></param>
        /// <returns></returns>
        private static void AssertBasicConstructsExist(ICodeRoot cr)
        {
            Assert.That(cr.WalkChildren(), Has.Count(1));

            IBaseConstruct namespaceBC = cr.WalkChildren()[0];
            Assert.That(namespaceBC.FullyQualifiedIdentifer, Is.EqualTo("Slyce.IntelliMerge.UnitTesting.Resources.CSharp"));

            Assert.That(namespaceBC.WalkChildren(), Has.Count(1));
            IBaseConstruct classBC = namespaceBC.WalkChildren()[0];
            Assert.That(classBC.FullyQualifiedIdentifer,
                        Is.EqualTo(string.Format("Slyce.IntelliMerge.UnitTesting.Resources.CSharp{0}Class1", BaseConstructConstants.FullyQualifiedIdentifierSeparator)));

            Assert.That(classBC.WalkChildren(), Has.Count(1));
            IBaseConstruct methodBC = classBC.WalkChildren()[0];
            Assert.That(methodBC.FullyQualifiedIdentifer,
                        Is.EqualTo(string.Format("Slyce.IntelliMerge.UnitTesting.Resources.CSharp{0}Class1{0}Method ()", BaseConstructConstants.FullyQualifiedIdentifierSeparator)));

            return;
        }