Sorted inserter.
Inheritance: IElementInserter
Exemplo n.º 1
0
        /// <summary>
        /// Creates an element inserter.
        /// </summary>
        /// <param name="elementType">Type of the element.</param>
        /// <param name="sortBy">The sort by.</param>
        /// <param name="groupBy">The group by.</param>
        /// <param name="parentConfiguration">The parent configuration.</param>
        /// <returns>An appropriate inserter.</returns>
        private static IElementInserter CreateElementInserter(
            ElementType elementType,
            SortBy sortBy,
            GroupBy groupBy,
            ConfigurationElement parentConfiguration)
        {
            IElementInserter inserter = null;

            if (sortBy != null)
            {
                inserter = new SortedInserter(elementType, sortBy);
            }

            if (groupBy != null && groupBy.InnerGroupBy != null)
            {
                inserter = new GroupedInserter(groupBy.InnerGroupBy, inserter);
            }

            if (groupBy != null)
            {
                inserter = new GroupedInserter(groupBy, inserter);
            }

            if (inserter == null)
            {
                inserter = new DefaultElementInserter();
            }

            return(inserter);
        }
Exemplo n.º 2
0
        public void InsertNullTest()
        {
            SortBy sortBy = new SortBy();
            sortBy.By = ElementAttributeType.Name;
            sortBy.Direction = SortDirection.Ascending;

            SortedInserter sortedInserter = new SortedInserter(ElementType.Field, sortBy);

            //
            // Create a parent element
            //
            RegionElement regionElement = new RegionElement();
            Assert.AreEqual(0, regionElement.Children.Count, "Parent element should not have any children.");

            //
            // Insert a non-null element
            //
            FieldElement field1 = new FieldElement();
            field1.Name = "newField";
            sortedInserter.InsertElement(regionElement, field1);
            Assert.AreEqual(1, regionElement.Children.Count, "Element was not inserted into the parent.");
            Assert.AreEqual(0, regionElement.Children.IndexOf(field1), "Element was not inserted at the correct index.");

            //
            // Insert a null element
            //
            FieldElement field2 = null;
            sortedInserter.InsertElement(regionElement, field2);
            Assert.AreEqual(1, regionElement.Children.Count, "Element should not have been inserted into the parent.");
            Assert.AreEqual(0, regionElement.Children.IndexOf(field1), "Element is not at the correct index.");
        }
Exemplo n.º 3
0
        /// <summary>
        /// Creates an element inserter.
        /// </summary>
        /// <param name="elementType">Type of the element.</param>
        /// <param name="sortBy">The sort by.</param>
        /// <param name="groupBy">The group by.</param>
        /// <param name="parentConfiguration">The parent configuration.</param>
        /// <returns>An appropriate inserter.</returns>
        private static IElementInserter CreateElementInserter(
			ElementType elementType,
			SortBy sortBy,
			GroupBy groupBy,
			ConfigurationElement parentConfiguration)
        {
            IElementInserter inserter = null;

            if (sortBy != null)
            {
                inserter = new SortedInserter(elementType, sortBy);
            }

            if (groupBy != null && groupBy.InnerGroupBy != null)
            {
                inserter = new GroupedInserter(groupBy.InnerGroupBy, inserter);
            }

            if (groupBy != null)
            {
                inserter = new GroupedInserter(groupBy, inserter);
            }

            if (inserter == null)
            {
                inserter = new DefaultElementInserter();
            }

            return inserter;
        }
Exemplo n.º 4
0
        public void InsertByTypeElementTypeDescendingTest()
        {
            SortBy sortBy = new SortBy();
            sortBy.By = ElementAttributeType.Type;
            sortBy.Direction = SortDirection.Descending;

            SortedInserter sortedInserter = new SortedInserter(ElementType.Type, sortBy);

            //
            // Create a parent element
            //
            RegionElement regionElement = new RegionElement();
            Assert.AreEqual(0, regionElement.Children.Count, "Parent element should not have any children.");

            //
            // Insert an element with a mid value.
            //
            TypeElement type1 = new TypeElement();
            type1.Name = "Type1";
            type1.Type = TypeElementType.Structure;
            sortedInserter.InsertElement(regionElement, type1);

            //
            // Insert an element that should be sorted toward the end
            //
            TypeElement type2 = new TypeElement();
            type2.Name = "Type2";
            type2.Type = TypeElementType.Class;
            sortedInserter.InsertElement(regionElement, type2);

            //
            // Insert an element that should be sorted toward the middle
            //
            TypeElement type3 = new TypeElement();
            type3.Name = "Type3";
            type3.Type = TypeElementType.Interface;
            sortedInserter.InsertElement(regionElement, type3);

            //
            // Insert an element that should be sorted toward the beginning
            //
            TypeElement type4 = new TypeElement();
            type4.Name = "Type4";
            type4.Type = TypeElementType.Enum;
            sortedInserter.InsertElement(regionElement, type4);

            Assert.AreEqual(4, regionElement.Children.Count, "Element was not inserted into the parent.");
            Assert.AreEqual(0, regionElement.Children.IndexOf(type4), "Element is not at the correct index.");
            Assert.AreEqual(1, regionElement.Children.IndexOf(type3), "Element is not at the correct index.");
            Assert.AreEqual(2, regionElement.Children.IndexOf(type1), "Element is not at the correct index.");
            Assert.AreEqual(3, regionElement.Children.IndexOf(type2), "Element is not at the correct index.");
        }
Exemplo n.º 5
0
        public void InsertByTypeTest()
        {
            SortBy sortBy = new SortBy();
            sortBy.By = ElementAttributeType.Type;
            sortBy.Direction = SortDirection.Ascending;

            SortedInserter sortedInserter = new SortedInserter(ElementType.Method, sortBy);

            //
            // Create a parent element
            //
            RegionElement regionElement = new RegionElement();
            Assert.AreEqual(0, regionElement.Children.Count, "Parent element should not have any children.");

            //
            // Insert an element with a mid alphabet return type.
            //
            MethodElement method1 = new MethodElement();
            method1.Name = "DoSomething";
            method1.Type = "Nullable<DateTime>";
            sortedInserter.InsertElement(regionElement, method1);
            Assert.AreEqual(1, regionElement.Children.Count, "Element was not inserted into the parent.");
            Assert.AreEqual(0, regionElement.Children.IndexOf(method1), "Element was not inserted at the correct index.");

            //
            // Insert an element that should be sorted toward the end
            //
            MethodElement method2 = new MethodElement();
            method2.Name = "DoSomething";
            method2.Type = "Type";
            sortedInserter.InsertElement(regionElement, method2);
            Assert.AreEqual(2, regionElement.Children.Count, "Element was not inserted into the parent.");
            Assert.AreEqual(0, regionElement.Children.IndexOf(method1), "Element is not at the correct index.");
            Assert.AreEqual(1, regionElement.Children.IndexOf(method2), "Element is not at the correct index.");

            //
            // Insert an element that should be sorted toward the beginning
            //
            MethodElement method3 = new MethodElement();
            method3.Name = "DoSomething";
            method3.Type = "IEnumerable";
            sortedInserter.InsertElement(regionElement, method3);
            Assert.AreEqual(3, regionElement.Children.Count, "Element was not inserted into the parent.");
            Assert.AreEqual(0, regionElement.Children.IndexOf(method3), "Element is not at the correct index.");
            Assert.AreEqual(1, regionElement.Children.IndexOf(method1), "Element is not at the correct index.");
            Assert.AreEqual(2, regionElement.Children.IndexOf(method2), "Element is not at the correct index.");
        }
Exemplo n.º 6
0
        public void InsertByNameTest()
        {
            SortBy sortBy = new SortBy();
            sortBy.By = ElementAttributeType.Name;
            sortBy.Direction = SortDirection.Ascending;

            SortedInserter sortedInserter = new SortedInserter(ElementType.Field, sortBy);

            //
            // Create a parent element
            //
            RegionElement regionElement = new RegionElement();
            Assert.AreEqual(0, regionElement.Children.Count, "Parent element should not have any children.");

            //
            // Insert an element with a mid alphabet name.
            //
            FieldElement field1 = new FieldElement();
            field1.Name = "newField";
            sortedInserter.InsertElement(regionElement, field1);
            Assert.AreEqual(1, regionElement.Children.Count, "Element was not inserted into the parent.");
            Assert.AreEqual(0, regionElement.Children.IndexOf(field1), "Element was not inserted at the correct index.");

            //
            // Insert an element that should be sorted toward the end
            //
            FieldElement field2 = new FieldElement();
            field2.Name = "zooField";
            sortedInserter.InsertElement(regionElement, field2);
            Assert.AreEqual(2, regionElement.Children.Count, "Element was not inserted into the parent.");
            Assert.AreEqual(0, regionElement.Children.IndexOf(field1), "Element is not at the correct index.");
            Assert.AreEqual(1, regionElement.Children.IndexOf(field2), "Element is not at the correct index.");

            //
            // Insert an element that should be sorted toward the beginning
            //
            FieldElement field3 = new FieldElement();
            field3.Name = "booField";
            sortedInserter.InsertElement(regionElement, field3);
            Assert.AreEqual(3, regionElement.Children.Count, "Element was not inserted into the parent.");
            Assert.AreEqual(0, regionElement.Children.IndexOf(field3), "Element is not at the correct index.");
            Assert.AreEqual(1, regionElement.Children.IndexOf(field1), "Element is not at the correct index.");
            Assert.AreEqual(2, regionElement.Children.IndexOf(field2), "Element is not at the correct index.");
        }
Exemplo n.º 7
0
        public void InsertByNoneTest()
        {
            SortBy sortBy = new SortBy();
            sortBy.By = ElementAttributeType.None;
            sortBy.Direction = SortDirection.Ascending;

            SortedInserter sortedInserter = new SortedInserter(ElementType.Field, sortBy);

            //
            // Create a parent element
            //
            RegionElement regionElement = new RegionElement();
            Assert.AreEqual(0, regionElement.Children.Count, "Parent element should not have any children.");

            //
            // With no criteria specified, elements should just be inserted
            // at the end of the collection.
            //
            FieldElement field1 = new FieldElement();
            field1.Name = "zooField";
            sortedInserter.InsertElement(regionElement, field1);
            Assert.AreEqual(1, regionElement.Children.Count, "Element was not inserted into the parent.");
            Assert.AreEqual(0, regionElement.Children.IndexOf(field1), "Element was not inserted at the correct index.");

            FieldElement field2 = new FieldElement();
            field1.Name = "newField";
            sortedInserter.InsertElement(regionElement, field2);
            Assert.AreEqual(2, regionElement.Children.Count, "Element was not inserted into the parent.");
            Assert.AreEqual(0, regionElement.Children.IndexOf(field1), "Element is not at the correct index.");
            Assert.AreEqual(1, regionElement.Children.IndexOf(field2), "Element is not at the correct index.");

            FieldElement field3 = new FieldElement();
            field1.Name = "booField";
            sortedInserter.InsertElement(regionElement, field3);
            Assert.AreEqual(3, regionElement.Children.Count, "Element was not inserted into the parent.");
            Assert.AreEqual(0, regionElement.Children.IndexOf(field1), "Element is not at the correct index.");
            Assert.AreEqual(1, regionElement.Children.IndexOf(field2), "Element is not at the correct index.");
            Assert.AreEqual(2, regionElement.Children.IndexOf(field3), "Element is not at the correct index.");
        }
Exemplo n.º 8
0
 public void CreateWithNullTest()
 {
     SortedInserter sortedInserter = new SortedInserter(ElementType.NotSpecified, null);
 }
Exemplo n.º 9
0
        public void InsertByAccessAndNameTest()
        {
            SortBy sortBy = new SortBy();
            sortBy.By = ElementAttributeType.Access;
            sortBy.Direction = SortDirection.Ascending;

            SortBy innerSortBy = new SortBy();
            innerSortBy.By = ElementAttributeType.Name;
            innerSortBy.Direction = SortDirection.Ascending;

            sortBy.InnerSortBy = innerSortBy;

            SortedInserter sortedInserter = new SortedInserter(ElementType.Field, sortBy);

            //
            // Create a parent element
            //
            RegionElement regionElement = new RegionElement();
            Assert.AreEqual(0, regionElement.Children.Count, "Parent element should not have any children.");

            //
            // Insert elements with middle access.
            //
            FieldElement field1 = new FieldElement();
            field1.Access = CodeAccess.Protected | CodeAccess.Internal;
            field1.Name = "newField";
            sortedInserter.InsertElement(regionElement, field1);
            Assert.AreEqual(1, regionElement.Children.Count, "Element was not inserted into the parent.");
            Assert.AreEqual(0, regionElement.Children.IndexOf(field1), "Element was not inserted at the correct index.");

            FieldElement field2 = new FieldElement();
            field2.Access = CodeAccess.Protected | CodeAccess.Internal;
            field2.Name = "gooField";
            sortedInserter.InsertElement(regionElement, field2);
            Assert.AreEqual(2, regionElement.Children.Count, "Element was not inserted into the parent.");
            Assert.AreEqual(0, regionElement.Children.IndexOf(field2), "Element was not inserted at the correct index.");
            Assert.AreEqual(1, regionElement.Children.IndexOf(field1), "Element was not inserted at the correct index.");

            //
            // Insert an element that should be sorted toward the end
            //
            FieldElement field3 = new FieldElement();
            field3.Access = CodeAccess.Public;
            field3.Name = "zooField";
            sortedInserter.InsertElement(regionElement, field3);
            Assert.AreEqual(3, regionElement.Children.Count, "Element was not inserted into the parent.");
            Assert.AreEqual(0, regionElement.Children.IndexOf(field2), "Element was not inserted at the correct index.");
            Assert.AreEqual(1, regionElement.Children.IndexOf(field1), "Element was not inserted at the correct index.");
            Assert.AreEqual(2, regionElement.Children.IndexOf(field3), "Element is not at the correct index.");

            FieldElement field4 = new FieldElement();
            field4.Access = CodeAccess.Public;
            field4.Name = "tooField";
            sortedInserter.InsertElement(regionElement, field4);
            Assert.AreEqual(4, regionElement.Children.Count, "Element was not inserted into the parent.");
            Assert.AreEqual(0, regionElement.Children.IndexOf(field2), "Element was not inserted at the correct index.");
            Assert.AreEqual(1, regionElement.Children.IndexOf(field1), "Element was not inserted at the correct index.");
            Assert.AreEqual(2, regionElement.Children.IndexOf(field4), "Element is not at the correct index.");
            Assert.AreEqual(3, regionElement.Children.IndexOf(field3), "Element is not at the correct index.");

            //
            // Insert an element that should be sorted toward the beginning
            //
            FieldElement field5 = new FieldElement();
            field5.Access = CodeAccess.Private;
            field5.Name = "booField";
            sortedInserter.InsertElement(regionElement, field5);
            Assert.AreEqual(5, regionElement.Children.Count, "Element was not inserted into the parent.");
            Assert.AreEqual(0, regionElement.Children.IndexOf(field5), "Element was not inserted at the correct index.");
            Assert.AreEqual(1, regionElement.Children.IndexOf(field2), "Element was not inserted at the correct index.");
            Assert.AreEqual(2, regionElement.Children.IndexOf(field1), "Element was not inserted at the correct index.");
            Assert.AreEqual(3, regionElement.Children.IndexOf(field4), "Element is not at the correct index.");
            Assert.AreEqual(4, regionElement.Children.IndexOf(field3), "Element is not at the correct index.");

            FieldElement field6 = new FieldElement();
            field6.Access = CodeAccess.Private;
            field6.Name = "fooField";
            sortedInserter.InsertElement(regionElement, field6);
            Assert.AreEqual(6, regionElement.Children.Count, "Element was not inserted into the parent.");
            Assert.AreEqual(0, regionElement.Children.IndexOf(field5), "Element was not inserted at the correct index.");
            Assert.AreEqual(1, regionElement.Children.IndexOf(field6), "Element was not inserted at the correct index.");
            Assert.AreEqual(2, regionElement.Children.IndexOf(field2), "Element was not inserted at the correct index.");
            Assert.AreEqual(3, regionElement.Children.IndexOf(field1), "Element was not inserted at the correct index.");
            Assert.AreEqual(4, regionElement.Children.IndexOf(field4), "Element is not at the correct index.");
            Assert.AreEqual(5, regionElement.Children.IndexOf(field3), "Element is not at the correct index.");
        }
Exemplo n.º 10
0
        public void InsertByElementTypeTest()
        {
            SortBy sortBy = new SortBy();
            sortBy.By = ElementAttributeType.ElementType;
            sortBy.Direction = SortDirection.Ascending;

            SortedInserter sortedInserter = new SortedInserter(ElementType.NotSpecified, sortBy);

            //
            // Create a parent element
            //
            RegionElement regionElement = new RegionElement();
            Assert.AreEqual(0, regionElement.Children.Count, "Parent element should not have any children.");

            //
            // Insert an element with a middle access.
            //
            ConstructorElement constructor = new ConstructorElement();
            constructor.Name = "SomeClass";
            constructor.Access = CodeAccess.Public;
            sortedInserter.InsertElement(regionElement, constructor);
            Assert.AreEqual(1, regionElement.Children.Count, "Element was not inserted into the parent.");
            Assert.AreEqual(0, regionElement.Children.IndexOf(constructor), "Element was not inserted at the correct index.");

            //
            // Insert an element that should be sorted toward the end
            //
            MethodElement methodElement = new MethodElement();
            methodElement.Name = "SomeMethod";
            methodElement.Access = CodeAccess.Public;
            sortedInserter.InsertElement(regionElement, methodElement);
            Assert.AreEqual(2, regionElement.Children.Count, "Element was not inserted into the parent.");
            Assert.AreEqual(0, regionElement.Children.IndexOf(constructor), "Element is not at the correct index.");
            Assert.AreEqual(1, regionElement.Children.IndexOf(methodElement), "Element is not at the correct index.");

            //
            // Insert an element that should be sorted toward the beginning
            //
            FieldElement fieldElement = new FieldElement();
            fieldElement.Name = "someField";
            fieldElement.Access = CodeAccess.Private;
            sortedInserter.InsertElement(regionElement, fieldElement);
            Assert.AreEqual(3, regionElement.Children.Count, "Element was not inserted into the parent.");
            Assert.AreEqual(0, regionElement.Children.IndexOf(fieldElement), "Element is not at the correct index.");
            Assert.AreEqual(1, regionElement.Children.IndexOf(constructor), "Element is not at the correct index.");
            Assert.AreEqual(2, regionElement.Children.IndexOf(methodElement), "Element is not at the correct index.");
        }
Exemplo n.º 11
0
        public void CreateTest()
        {
            SortBy sortBy = new SortBy();

            SortedInserter sortedInserter = new SortedInserter(ElementType.NotSpecified, sortBy);
        }
Exemplo n.º 12
0
        public void InsertByAccessTest()
        {
            SortBy sortBy = new SortBy();
            sortBy.By = ElementAttributeType.Access;
            sortBy.Direction = SortDirection.Ascending;

            SortedInserter sortedInserter = new SortedInserter(ElementType.Field, sortBy);

            //
            // Create a parent element
            //
            RegionElement regionElement = new RegionElement();
            Assert.AreEqual(0, regionElement.Children.Count, "Parent element should not have any children.");

            //
            // Insert an element with a middle access.
            //
            FieldElement field1 = new FieldElement();
            field1.Access = CodeAccess.Protected | CodeAccess.Internal;
            sortedInserter.InsertElement(regionElement, field1);
            Assert.AreEqual(1, regionElement.Children.Count, "Element was not inserted into the parent.");
            Assert.AreEqual(0, regionElement.Children.IndexOf(field1), "Element was not inserted at the correct index.");

            //
            // Insert an element that should be sorted toward the end
            //
            FieldElement field2 = new FieldElement();
            field2.Access = CodeAccess.Public;
            sortedInserter.InsertElement(regionElement, field2);
            Assert.AreEqual(2, regionElement.Children.Count, "Element was not inserted into the parent.");
            Assert.AreEqual(0, regionElement.Children.IndexOf(field1), "Element is not at the correct index.");
            Assert.AreEqual(1, regionElement.Children.IndexOf(field2), "Element is not at the correct index.");

            //
            // Insert an element that should be sorted toward the beginning
            //
            FieldElement field3 = new FieldElement();
            field3.Access = CodeAccess.Private;
            sortedInserter.InsertElement(regionElement, field3);
            Assert.AreEqual(3, regionElement.Children.Count, "Element was not inserted into the parent.");
            Assert.AreEqual(0, regionElement.Children.IndexOf(field3), "Element is not at the correct index.");
            Assert.AreEqual(1, regionElement.Children.IndexOf(field1), "Element is not at the correct index.");
            Assert.AreEqual(2, regionElement.Children.IndexOf(field2), "Element is not at the correct index.");
        }
Exemplo n.º 13
0
        public void NestedGroupSortTest()
        {
            GroupBy groupBy = new GroupBy();
            groupBy.By = ElementAttributeType.Type;
            groupBy.Direction = SortDirection.Ascending;

            GroupBy innerGroupBy = new GroupBy();
            innerGroupBy.By = ElementAttributeType.Name;
            innerGroupBy.AttributeCapture = "^(.*?)(\\.|$)";
            innerGroupBy.Direction = SortDirection.Ascending;

            groupBy.InnerGroupBy = innerGroupBy;

            SortBy sortBy = new SortBy();
            sortBy.By = ElementAttributeType.Name;
            SortedInserter sortedInserter = new SortedInserter(ElementType.NotSpecified, sortBy);
            GroupedInserter groupedInserter = new GroupedInserter(
                groupBy,
                new GroupedInserter(groupBy.InnerGroupBy, sortedInserter));

            //
            // Create a parent element
            //
            GroupElement groupElement = new GroupElement();
            Assert.AreEqual(0, groupElement.Children.Count, "Parent element should not have any children.");

            // Insert elements
            groupedInserter.InsertElement(groupElement, new UsingElement("NUnit.Framework"));
            groupedInserter.InsertElement(groupElement, new UsingElement("NArrange.Core"));
            groupedInserter.InsertElement(groupElement, new UsingElement("NArrange.CSharp"));
            groupedInserter.InsertElement(groupElement, new UsingElement("NArrange.Core.Configuration"));
            groupedInserter.InsertElement(groupElement, new UsingElement("System"));
            groupedInserter.InsertElement(groupElement, new UsingElement("System.IO"));
            groupedInserter.InsertElement(groupElement, new UsingElement("MyClass2", "NArrange.Core.CodeArranger"));
            groupedInserter.InsertElement(groupElement, new UsingElement("MyClass1", "NArrange.Core.ElementFilter"));

            Assert.AreEqual(2, groupElement.Children.Count, "Unexpected number of child groups.");

            GroupElement childGroup;
            GroupElement grandchildGroup;

            // Namespace usings should come before alias usings
            childGroup = groupElement.Children[0] as GroupElement;
            Assert.IsNotNull(childGroup, "Expected a child group.");
            Assert.AreEqual(3, childGroup.Children.Count, "Unexpected number of group children.");

            // System usings should always come first
            grandchildGroup = childGroup.Children[0] as GroupElement;
            Assert.IsNotNull(grandchildGroup, "Expected a child group.");
            Assert.AreEqual(2, grandchildGroup.Children.Count, "Unexpected number of group children.");
            Assert.AreEqual("System", grandchildGroup.Children[0].Name);
            Assert.AreEqual("System.IO", grandchildGroup.Children[1].Name);

            grandchildGroup = childGroup.Children[1] as GroupElement;
            Assert.IsNotNull(grandchildGroup, "Expected a child group.");
            Assert.AreEqual(3, grandchildGroup.Children.Count, "Unexpected number of group children.");
            Assert.AreEqual("NArrange.Core", grandchildGroup.Children[0].Name);
            Assert.AreEqual("NArrange.Core.Configuration", grandchildGroup.Children[1].Name);
            Assert.AreEqual("NArrange.CSharp", grandchildGroup.Children[2].Name);

            grandchildGroup = childGroup.Children[2] as GroupElement;
            Assert.IsNotNull(grandchildGroup, "Expected a child group.");
            Assert.AreEqual(1, grandchildGroup.Children.Count, "Unexpected number of group children.");
            Assert.AreEqual("NUnit.Framework", grandchildGroup.Children[0].Name);

            // Alias using directives
            childGroup = groupElement.Children[1] as GroupElement;
            Assert.IsNotNull(childGroup, "Expected a child group.");
            Assert.AreEqual(2, childGroup.Children.Count, "Unexpected number of group children.");
            grandchildGroup = childGroup.Children[0] as GroupElement;
            Assert.AreEqual(1, grandchildGroup.Children.Count, "Unexpected number of group children.");
            Assert.AreEqual("MyClass1", grandchildGroup.Children[0].Name);
            grandchildGroup = childGroup.Children[1] as GroupElement;
            Assert.AreEqual(1, grandchildGroup.Children.Count, "Unexpected number of group children.");
            Assert.AreEqual("MyClass2", grandchildGroup.Children[0].Name);
        }
Exemplo n.º 14
0
        public void InsertSortedTest()
        {
            GroupBy groupBy = new GroupBy();
            groupBy.By = ElementAttributeType.Name;
            groupBy.AttributeCapture = "^(.*?)(\\.|$)";

            SortBy sortBy = new SortBy();
            sortBy.By = ElementAttributeType.Name;
            SortedInserter sortedInserter = new SortedInserter(ElementType.Using, sortBy);

            GroupedInserter groupedInserter = new GroupedInserter(groupBy, sortedInserter);

            //
            // Create a parent element
            //
            GroupElement groupElement = new GroupElement();
            Assert.AreEqual(0, groupElement.Children.Count, "Parent element should not have any children.");

            //
            // With no criteria specified, elements should just be inserted
            // at the end of the collection.
            //
            UsingElement using1 = new UsingElement();
            using1.Name = "System.IO";
            groupedInserter.InsertElement(groupElement, using1);
            Assert.AreEqual(1, groupElement.Children.Count, "Group element was not inserted into the parent.");
            Assert.IsTrue(groupElement.Children[0] is GroupElement, "Group element was not inserted into the parent.");
            Assert.AreEqual(1, groupElement.Children[0].Children.Count, "Element was not inserted into the parent.");
            Assert.AreEqual(0, groupElement.Children[0].Children.IndexOf(using1), "Element was not inserted at the correct index.");

            UsingElement using2 = new UsingElement();
            using2.Name = "System";
            groupedInserter.InsertElement(groupElement, using2);
            Assert.AreEqual(1, groupElement.Children.Count, "Group element was not inserted into the parent.");
            Assert.IsTrue(groupElement.Children[0] is GroupElement, "Group element was not inserted into the parent.");
            Assert.AreEqual(2, groupElement.Children[0].Children.Count, "Element was not inserted into the parent.");
            Assert.AreEqual(0, groupElement.Children[0].Children.IndexOf(using2), "Element is not at the correct index.");
            Assert.AreEqual(1, groupElement.Children[0].Children.IndexOf(using1), "Element is not at the correct index.");

            UsingElement using3 = new UsingElement();
            using3.Name = "System.Text";
            groupedInserter.InsertElement(groupElement, using3);
            Assert.AreEqual(1, groupElement.Children.Count, "Group element was not inserted into the parent.");
            Assert.IsTrue(groupElement.Children[0] is GroupElement, "Group element was not inserted into the parent.");
            Assert.AreEqual(3, groupElement.Children[0].Children.Count, "Element was not inserted into the parent.");
            Assert.AreEqual(0, groupElement.Children[0].Children.IndexOf(using2), "Element is not at the correct index[0].Children.");
            Assert.AreEqual(1, groupElement.Children[0].Children.IndexOf(using1), "Element is not at the correct index.");
            Assert.AreEqual(2, groupElement.Children[0].Children.IndexOf(using3), "Element is not at the correct index.");
        }