public void CloneTest() { ElementConfiguration prototype = new ElementConfiguration(); prototype.ElementType = ElementType.Delegate; prototype.Id = "Test"; FilterBy filterBy = new FilterBy(); filterBy.Condition = "$(Name) == 'Test'"; prototype.FilterBy = filterBy; GroupBy groupBy = new GroupBy(); groupBy.By = ElementAttributeType.Access; prototype.GroupBy = groupBy; SortBy sortBy = new SortBy(); sortBy.By = ElementAttributeType.Name; prototype.SortBy = sortBy; ElementConfiguration clone = prototype.Clone() as ElementConfiguration; Assert.IsNotNull(clone, "Clone did not return an instance."); Assert.AreEqual(prototype.ElementType, clone.ElementType, "ElementType was not cloned correctly."); Assert.AreEqual(prototype.Id, clone.Id, "Id was not cloned correctly."); Assert.AreEqual(prototype.FilterBy.Condition, clone.FilterBy.Condition, "FilterBy was not cloned correctly."); Assert.AreEqual(prototype.GroupBy.By, clone.GroupBy.By, "GroupBy was not cloned correctly."); Assert.AreEqual(prototype.SortBy.By, clone.SortBy.By, "SortBy was not cloned correctly."); }
public void SeparatorTypeTest() { GroupBy groupBy = new GroupBy(); Assert.AreEqual(GroupSeparatorType.NewLine, groupBy.SeparatorType, "Unexpected default value for SeparateType."); groupBy.SeparatorType = GroupSeparatorType.Custom; Assert.AreEqual(GroupSeparatorType.Custom, groupBy.SeparatorType, "SeparateType was not set correctly."); }
public void CustomSeparatorTest() { GroupBy groupBy = new GroupBy(); Assert.IsNull(groupBy.CustomSeparator, "Unexpected default value for CustomSeparator."); groupBy.CustomSeparator = "// This is a group\r\n"; Assert.AreEqual("// This is a group\r\n", groupBy.CustomSeparator, "CustomSeparator was not set correctly."); }
public void CreateTest() { GroupBy groupBy = new GroupBy(); // // Verify default state // Assert.IsNull(groupBy.AttributeCapture, "Unexpected default value AttributeCapture."); Assert.AreEqual(ElementAttributeType.None, groupBy.By, "Unexpected default value for By."); Assert.AreEqual(GroupSeparatorType.NewLine, groupBy.SeparatorType, "Unexpected default value for SeparateType."); Assert.IsNull(groupBy.CustomSeparator, "Unexpected default value for CustomSeparator."); Assert.AreEqual(SortDirection.None, groupBy.Direction, "Unexpected default value for Direction."); }
/// <summary> /// Creates a clone of this instance. /// </summary> /// <returns> /// A new object that is a copy of this instance. /// </returns> public object Clone() { GroupBy clone = new GroupBy(); clone._by = _by; clone._matchCapture = _matchCapture; clone._customSeparator = _customSeparator; clone._separatorType = _separatorType; clone._direction = _direction; if (_innerGroupBy != null) { clone._innerGroupBy = _innerGroupBy.Clone() as GroupBy; } return(clone); }
public void CloneTest() { GroupBy groupBy = new GroupBy(); groupBy.By = ElementAttributeType.Name; groupBy.AttributeCapture = "(.*)"; groupBy.CustomSeparator = "\r\nXXX\r\n"; groupBy.Direction = SortDirection.Descending; groupBy.SeparatorType = GroupSeparatorType.Custom; GroupBy innerGroupBy = new GroupBy(); innerGroupBy.By = ElementAttributeType.Type; groupBy.InnerGroupBy = innerGroupBy; GroupBy clone = groupBy.Clone() as GroupBy; Assert.AreEqual(groupBy.By, clone.By, "By was not copied correctly"); Assert.AreEqual(groupBy.AttributeCapture, clone.AttributeCapture, "AttributeCapture was not copied correctly"); Assert.AreEqual(groupBy.CustomSeparator, clone.CustomSeparator, "CustomSeparator was not copied correctly"); Assert.AreEqual(groupBy.Direction, clone.Direction, "Direction was not copied correctly"); Assert.AreEqual(groupBy.SeparatorType, clone.SeparatorType, "SeparatorType was not copied correctly"); Assert.IsNotNull(clone.InnerGroupBy, "InnerGroupBy was not copied correctly"); Assert.AreEqual(groupBy.InnerGroupBy.By, clone.InnerGroupBy.By, "InnerGroupBy was not copied correctly"); }
/// <summary> /// Creates a clone of this instance. /// </summary> /// <returns> /// A new object that is a copy of this instance. /// </returns> public object Clone() { GroupBy clone = new GroupBy(); clone._by = _by; clone._matchCapture = _matchCapture; clone._customSeparator = _customSeparator; clone._separatorType = _separatorType; clone._direction = _direction; if (_innerGroupBy != null) { clone._innerGroupBy = _innerGroupBy.Clone() as GroupBy; } return clone; }
/// <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; }
public void GroupSortByAccessTest() { GroupBy groupBy = new GroupBy(); groupBy.By = ElementAttributeType.Access; groupBy.Direction = SortDirection.Ascending; GroupedInserter groupedInserter = new GroupedInserter(groupBy); // // Create a parent element // GroupElement groupElement = new GroupElement(); Assert.AreEqual(0, groupElement.Children.Count, "Parent element should not have any children."); // Insert elements PropertyElement property1 = new PropertyElement(); property1.Access = CodeAccess.Internal; property1.Name = "Property1"; property1.Type = "string"; groupedInserter.InsertElement(groupElement, property1); PropertyElement property2 = new PropertyElement(); property2.Access = CodeAccess.Public; property2.Name = "Property2"; property2.Type = "string"; groupedInserter.InsertElement(groupElement, property2); PropertyElement property3 = new PropertyElement(); property3.Access = CodeAccess.Protected | CodeAccess.Internal; property3.Name = "Property3"; property3.Type = "string"; groupedInserter.InsertElement(groupElement, property3); PropertyElement property4 = new PropertyElement(); property4.Access = CodeAccess.Private; property4.Name = "Property4"; property4.Type = "string"; groupedInserter.InsertElement(groupElement, property4); PropertyElement property5 = new PropertyElement(); property5.Access = CodeAccess.Public; property5.Name = "Property5"; property5.Type = "string"; groupedInserter.InsertElement(groupElement, property5); Assert.AreEqual(4, groupElement.Children.Count, "Unexpected number of child groups."); GroupElement childGroup; childGroup = groupElement.Children[0] as GroupElement; Assert.IsNotNull(childGroup, "Expected a child group."); Assert.AreEqual(1, childGroup.Children.Count, "Unexpected number of group children."); Assert.AreEqual("Property4", childGroup.Children[0].Name); childGroup = groupElement.Children[1] as GroupElement; Assert.IsNotNull(childGroup, "Expected a child group."); Assert.AreEqual(1, childGroup.Children.Count, "Unexpected number of group children."); Assert.AreEqual("Property1", childGroup.Children[0].Name); childGroup = groupElement.Children[2] as GroupElement; Assert.IsNotNull(childGroup, "Expected a child group."); Assert.AreEqual(1, childGroup.Children.Count, "Unexpected number of group children."); Assert.AreEqual("Property3", childGroup.Children[0].Name); childGroup = groupElement.Children[3] as GroupElement; Assert.IsNotNull(childGroup, "Expected a child group."); Assert.AreEqual(2, childGroup.Children.Count, "Unexpected number of group children."); Assert.AreEqual("Property2", childGroup.Children[0].Name); Assert.AreEqual("Property5", childGroup.Children[1].Name); }
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); }
public void InsertTest() { GroupBy groupBy = new GroupBy(); groupBy.By = ElementAttributeType.Name; groupBy.AttributeCapture = "^(.*?)(\\.|$)"; GroupedInserter groupedInserter = new GroupedInserter(groupBy); // // 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(using1), "Element is not at the correct index."); Assert.AreEqual(1, groupElement.Children[0].Children.IndexOf(using2), "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(using1), "Element is not at the correct index[0].Children."); Assert.AreEqual(1, groupElement.Children[0].Children.IndexOf(using2), "Element is not at the correct index."); Assert.AreEqual(2, groupElement.Children[0].Children.IndexOf(using3), "Element is not at the correct index."); }
public void CreateTest() { GroupBy groupBy = new GroupBy(); GroupedInserter regionedInserter = new GroupedInserter(groupBy); }
public void GroupSortByNameTest() { GroupBy groupBy = new GroupBy(); groupBy.By = ElementAttributeType.Name; groupBy.AttributeCapture = "^(.*?)(\\.|$)"; groupBy.Direction = SortDirection.Ascending; GroupedInserter groupedInserter = new GroupedInserter(groupBy); // // 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.Core.Configuration")); groupedInserter.InsertElement(groupElement, new UsingElement("System")); groupedInserter.InsertElement(groupElement, new UsingElement("System.IO")); Assert.AreEqual(3, groupElement.Children.Count, "Unexpected number of child groups."); GroupElement childGroup; // System usings should always come first childGroup = groupElement.Children[0] as GroupElement; Assert.IsNotNull(childGroup, "Expected a child group."); Assert.AreEqual(2, childGroup.Children.Count, "Unexpected number of group children."); Assert.AreEqual("System", childGroup.Children[0].Name); Assert.AreEqual("System.IO", childGroup.Children[1].Name); childGroup = groupElement.Children[1] as GroupElement; Assert.IsNotNull(childGroup, "Expected a child group."); Assert.AreEqual(2, childGroup.Children.Count, "Unexpected number of group children."); Assert.AreEqual("NArrange.Core", childGroup.Children[0].Name); Assert.AreEqual("NArrange.Core.Configuration", childGroup.Children[1].Name); childGroup = groupElement.Children[2] as GroupElement; Assert.IsNotNull(childGroup, "Expected a child group."); Assert.AreEqual(1, childGroup.Children.Count, "Unexpected number of group children."); Assert.AreEqual("NUnit.Framework", childGroup.Children[0].Name); }
public void ToStringTest() { GroupBy groupBy = new GroupBy(); groupBy.By = ElementAttributeType.Access; string str = groupBy.ToString(); Assert.AreEqual("Group by: Access", str, "Unexpected string representation."); }
/// <summary> /// Creates a new GroupedInserter using the specified grouping configuration /// and sorter. /// </summary> /// <param name="groupBy">The group by.</param> /// <param name="innerInserter">The inner inserter.</param> public GroupedInserter(GroupBy groupBy, IElementInserter innerInserter) { if (groupBy == null) { throw new ArgumentNullException("groupBy"); } _groupBy = groupBy.Clone() as GroupBy; _innerInserter = innerInserter; if (!string.IsNullOrEmpty(groupBy.AttributeCapture)) { _captureRegex = new Regex(_groupBy.AttributeCapture); } }
/// <summary> /// Creates a new GroupedInserter using the specified grouping configuration. /// </summary> /// <param name="groupBy">The group by.</param> public GroupedInserter(GroupBy groupBy) : this(groupBy, null) { }