public void GetAttributeModifierTest() { FieldElement fieldElement = new FieldElement(); fieldElement.Name = "TestField"; fieldElement.Access = CodeAccess.Protected; fieldElement.Type = "int"; fieldElement.MemberModifiers = MemberModifiers.Static; string attribute = ElementUtilities.GetAttribute(ElementAttributeType.Modifier, fieldElement); Assert.AreEqual("Static", attribute, "Unexpected attribute."); TypeElement typeElement = new TypeElement(); typeElement.TypeModifiers = TypeModifiers.Sealed; attribute = ElementUtilities.GetAttribute(ElementAttributeType.Modifier, typeElement); Assert.AreEqual("Sealed", attribute, "Unexpected attribute."); UsingElement usingElement = new UsingElement(); usingElement.Name = "System"; attribute = ElementUtilities.GetAttribute(ElementAttributeType.Modifier, usingElement); Assert.AreEqual(string.Empty, attribute, "Unexpected attribute."); }
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."); }
/// <summary> /// Clones this instance. /// </summary> /// <returns>Clone of the code element.</returns> protected override CodeElement DoClone() { UsingElement clone = new UsingElement(); clone._redefine = _redefine; clone._isMovable = _isMovable; return(clone); }
public void DefaultArrangeNoUsingMoveTest() { CodeConfiguration configuration = CodeConfiguration.Default.Clone() as CodeConfiguration; configuration.Formatting.Usings.MoveTo = CodeLevel.None; CodeArranger arranger = new CodeArranger(configuration); ReadOnlyCollection <ICodeElement> arranged = arranger.Arrange(_testElements); // // Verify using statements were grouped and sorted correctly // Assert.AreEqual(3, arranged.Count, "An unexpected number of root elements were returned from Arrange."); RegionElement regionElement = arranged[0] as RegionElement; Assert.IsNotNull(regionElement, "Expected a region element."); Assert.AreEqual("Header", regionElement.Name); GroupElement groupElement = arranged[1] as GroupElement; Assert.IsNotNull(groupElement, "Expected a group element."); Assert.AreEqual("Namespace", groupElement.Name, "Unexpected group name."); Assert.AreEqual(1, groupElement.Children.Count, "Group contains an unexpected number of child elements."); groupElement = groupElement.Children[0] as GroupElement; Assert.IsNotNull(groupElement, "Expected a group element."); Assert.AreEqual("System", groupElement.Name, "Unexpected group name."); Assert.AreEqual(7, groupElement.Children.Count, "Group contains an unexpected number of child elements."); string lastUsingName = null; foreach (CodeElement groupedElement in groupElement.Children) { UsingElement usingElement = groupedElement as UsingElement; Assert.IsNotNull(usingElement, "Expected a using element."); string usingName = usingElement.Name; if (lastUsingName != null) { Assert.AreEqual( -1, lastUsingName.CompareTo(usingName), "Expected using statements to be sorted by name."); } } // // Verify the namespace arrangement // NamespaceElement namespaceElement = arranged[2] as NamespaceElement; Assert.IsNotNull(namespaceElement, "Expected a namespace element."); }
public void DefaultArrangeEnumerationTest() { List <ICodeElement> codeElements = new List <ICodeElement>(); UsingElement usingElement = new UsingElement(); usingElement.Name = "System"; TypeElement enumElement = new TypeElement(); enumElement.Type = TypeElementType.Enum; enumElement.Access = CodeAccess.Public; enumElement.Name = "TestEnum"; enumElement.BodyText = "Value1 = 1,\r\nValue2 = 2"; NamespaceElement namespaceElement = new NamespaceElement(); namespaceElement.Name = "TestNamespace"; namespaceElement.AddChild(usingElement); namespaceElement.AddChild(enumElement); codeElements.Add(namespaceElement); CodeArranger arranger = new CodeArranger(CodeConfiguration.Default); ReadOnlyCollection <ICodeElement> arranged = arranger.Arrange(codeElements.AsReadOnly()); Assert.AreEqual(1, arranged.Count, "After arranging, an unexpected number of elements were returned."); NamespaceElement namespaceElementTest = arranged[0] as NamespaceElement; Assert.IsNotNull(namespaceElementTest, "Expected a namespace element."); Assert.AreEqual(2, namespaceElementTest.Children.Count, "After arranging, an unexpected number of namespace elements were returned."); Assert.AreEqual(ElementType.Using, namespaceElement.Children[0].ElementType); RegionElement regionElement = namespaceElementTest.Children[1] as RegionElement; Assert.IsNotNull(regionElement, "Expected a region element."); Assert.AreEqual("Enumerations", regionElement.Name, "Unexpected region name."); Assert.AreEqual(1, regionElement.Children.Count, "After arranging, an unexpected number of region elements were returned."); TypeElement typeElement = regionElement.Children[0] as TypeElement; Assert.IsNotNull(typeElement, "Expected a type element."); Assert.AreEqual(TypeElementType.Enum, typeElement.Type, "Unexpected type element type."); Assert.AreEqual(enumElement.Name, typeElement.Name, "Unexpected type element name."); }
public void CreateTest() { UsingElement element = new UsingElement(); // // Verify default values // Assert.AreEqual(string.Empty, element.Name, "Unexpected default value for Name."); Assert.IsNull(element.Redefine, "Unexpected default value for Alias."); Assert.IsNotNull(element.Children, "Children collection should not be null."); Assert.AreEqual(0, element.Children.Count, "Children collection should be empty."); Assert.IsNotNull(element.HeaderComments, "HeaderCommentLines collection should not be null."); Assert.AreEqual(0, element.HeaderComments.Count, "HeaderCommentLines collection should be empty."); }
/// <summary> /// Processes a using element. /// </summary> /// <param name="element">Using/Import directive code element.</param> public override void VisitUsingElement(UsingElement element) { this.WriteComments(element.HeaderComments); StringBuilder builder = new StringBuilder(DefaultBlockLength); builder.Append(VBKeyword.Imports); builder.Append(' '); builder.Append(element.Name); if (!string.IsNullOrEmpty(element.Redefine)) { builder.Append(" " + VBSymbol.Assignment.ToString() + " "); builder.Append(element.Redefine); } WriteIndented(builder.ToString()); }
public void DefaultArrangeUsingsInRegionTest() { CodeArranger arranger = new CodeArranger(CodeConfiguration.Default); List <ICodeElement> codeElements = new List <ICodeElement>(); RegionElement regionElement = new RegionElement(); regionElement.Name = "Using Directives"; UsingElement usingElement1 = new UsingElement(); usingElement1.Name = "System"; regionElement.AddChild(usingElement1); UsingElement usingElement2 = new UsingElement(); usingElement2.Name = "System.Text"; regionElement.AddChild(usingElement2); codeElements.Add(regionElement); ReadOnlyCollection <ICodeElement> arranged = arranger.Arrange(codeElements.AsReadOnly()); // // Verify using statements were stripped from the region // Assert.AreEqual(1, arranged.Count, "An unexpected number of root elements were returned from Arrange."); GroupElement groupElement = arranged[0] as GroupElement; Assert.IsNotNull(groupElement, "Expected a group element."); Assert.AreEqual("Namespace", groupElement.Name); groupElement = groupElement.Children[0] as GroupElement; Assert.IsNotNull(groupElement, "Expected a group element."); Assert.AreEqual("System", groupElement.Name); foreach (ICodeElement arrangedElement in groupElement.Children) { Assert.IsTrue(arrangedElement is UsingElement, "Expected a using element."); } }
/// <summary> /// Processes a using element. /// </summary> /// <param name="element">Using element to process.</param> public override void VisitUsingElement(UsingElement element) { this.WriteComments(element.HeaderComments); StringBuilder builder = new StringBuilder(DefaultBlockLength); builder.Append(CSharpKeyword.Using); builder.Append(' '); if (element.IsStatic) { builder.Append("static "); } builder.Append(element.Name); if (!string.IsNullOrEmpty(element.Redefine)) { builder.Append(" " + CSharpSymbol.Assignment.ToString() + " "); builder.Append(element.Redefine); } builder.Append(CSharpSymbol.EndOfStatement); WriteIndented(builder.ToString()); }
public void GetAttributeTypeTest() { FieldElement fieldElement = new FieldElement(); fieldElement.Name = "TestField"; fieldElement.Access = CodeAccess.Protected; fieldElement.Type = "int"; string attribute = ElementUtilities.GetAttribute(ElementAttributeType.Type, fieldElement); Assert.AreEqual("int", attribute, "Unexpected attribute."); TypeElement typeElement = new TypeElement(); typeElement.Type = TypeElementType.Interface; attribute = ElementUtilities.GetAttribute(ElementAttributeType.Type, typeElement); Assert.AreEqual("Interface", attribute, "Unexpected attribute."); CommentElement commentElement = new CommentElement(CommentType.Block); attribute = ElementUtilities.GetAttribute(ElementAttributeType.Type, commentElement); Assert.AreEqual("Block", attribute, "Unexpected attribute."); UsingElement usingElement = new UsingElement(); usingElement.Name = "MySystem"; usingElement.Redefine = "System"; attribute = ElementUtilities.GetAttribute(ElementAttributeType.Type, usingElement); Assert.AreEqual("Alias", attribute, "Unexpected attribute."); ConditionDirectiveElement conditionElement = new ConditionDirectiveElement(); attribute = ElementUtilities.GetAttribute(ElementAttributeType.Type, conditionElement); Assert.AreEqual(string.Empty, attribute, "Unexpected attribute."); }
public void MoveUsingsBasicTest() { List <ICodeElement> codeElements = new List <ICodeElement>(); UsingElement using1 = new UsingElement(); using1.Name = "System"; using1.IsMovable = true; UsingElement using2 = new UsingElement(); using2.Name = "System.IO"; using2.IsMovable = true; UsingElement using3 = new UsingElement(); using3.Name = "System.Collections"; using3.IsMovable = true; codeElements.Add(using1); codeElements.Add(using2); NamespaceElement namespaceElement = new NamespaceElement(); namespaceElement.Name = "TestNamespace"; namespaceElement.AddChild(using3); codeElements.Add(namespaceElement); CodeConfiguration configuration = CodeConfiguration.Default.Clone() as CodeConfiguration; CodeArranger arranger; // // Do not move. // configuration.Formatting.Usings.MoveTo = CodeLevel.None; arranger = new CodeArranger(configuration); ReadOnlyCollection <ICodeElement> arranged = arranger.Arrange(codeElements.AsReadOnly()); Assert.AreEqual(2, arranged.Count, "After arranging, an unexpected number of elements were returned."); GroupElement fileGroup = arranged[0] as GroupElement; Assert.IsNotNull(fileGroup); GroupElement innerGroup = fileGroup.Children[0] as GroupElement; Assert.AreEqual("System", innerGroup.Children[0].Name); Assert.AreEqual("System.IO", innerGroup.Children[1].Name); NamespaceElement namespaceElementTest = arranged[1] as NamespaceElement; Assert.IsNotNull(namespaceElementTest, "Expected a namespace element."); Assert.AreEqual(1, namespaceElementTest.Children.Count, "After arranging, an unexpected number of namespace elements were returned."); GroupElement namespaceGroup = namespaceElementTest.Children[0] as GroupElement; Assert.IsNotNull(namespaceGroup); innerGroup = namespaceGroup.Children[0] as GroupElement; Assert.AreEqual("System.Collections", innerGroup.Children[0].Name); // // Move to file level; // configuration.Formatting.Usings.MoveTo = CodeLevel.File; arranger = new CodeArranger(configuration); arranged = arranger.Arrange(codeElements.AsReadOnly()); Assert.AreEqual(2, arranged.Count, "After arranging, an unexpected number of elements were returned."); fileGroup = arranged[0] as GroupElement; Assert.IsNotNull(fileGroup); innerGroup = fileGroup.Children[0] as GroupElement; Assert.AreEqual("System", innerGroup.Children[0].Name); Assert.AreEqual("System.Collections", innerGroup.Children[1].Name); Assert.AreEqual("System.IO", innerGroup.Children[2].Name); namespaceElementTest = arranged[1] as NamespaceElement; Assert.IsNotNull(namespaceElementTest, "Expected a namespace element."); // // Move to namespace. // configuration.Formatting.Usings.MoveTo = CodeLevel.Namespace; arranger = new CodeArranger(configuration); arranged = arranger.Arrange(codeElements.AsReadOnly()); Assert.AreEqual(1, arranged.Count, "After arranging, an unexpected number of elements were returned."); namespaceElementTest = arranged[0] as NamespaceElement; Assert.IsNotNull(namespaceElementTest, "Expected a namespace element."); Assert.AreEqual(1, namespaceElementTest.Children.Count, "After arranging, an unexpected number of namespace elements were returned."); namespaceGroup = namespaceElementTest.Children[0] as GroupElement; Assert.IsNotNull(namespaceGroup); innerGroup = namespaceGroup.Children[0] as GroupElement; Assert.AreEqual("System", innerGroup.Children[0].Name); Assert.AreEqual("System.Collections", innerGroup.Children[1].Name); Assert.AreEqual("System.IO", innerGroup.Children[2].Name); // // Move back to file level; // configuration.Formatting.Usings.MoveTo = CodeLevel.File; arranger = new CodeArranger(configuration); arranged = arranger.Arrange(codeElements.AsReadOnly()); Assert.AreEqual(2, arranged.Count, "After arranging, an unexpected number of elements were returned."); fileGroup = arranged[0] as GroupElement; Assert.IsNotNull(fileGroup); innerGroup = fileGroup.Children[0] as GroupElement; Assert.AreEqual("System", innerGroup.Children[0].Name); Assert.AreEqual("System.Collections", innerGroup.Children[1].Name); Assert.AreEqual("System.IO", innerGroup.Children[2].Name); namespaceElementTest = arranged[1] as NamespaceElement; Assert.IsNotNull(namespaceElementTest, "Expected a namespace element."); }
/// <summary> /// Clones this instance. /// </summary> /// <returns>Clone of the code element.</returns> protected override CodeElement DoClone() { UsingElement clone = new UsingElement(); clone._redefine = this._redefine; clone._isMovable = this._isMovable; return clone; }
/// <summary> /// Moves using directives if configured to do so. /// </summary> /// <param name="elements">List of top-level code elements.</param> /// <param name="namespaceElement">Namespace namespace to use when moving usings.</param> private void MoveUsings(List <ICodeElement> elements, NamespaceElement namespaceElement) { CodeLevel moveUsingsTo = _configuration.Formatting.Usings.MoveTo; List <ICodeElement> tempElements; if (moveUsingsTo != CodeLevel.None && namespaceElement != null) { if (moveUsingsTo == CodeLevel.Namespace) { tempElements = new List <ICodeElement>(elements); for (int elementIndex = 0; elementIndex < tempElements.Count; elementIndex++) { UsingElement usingElement = tempElements[elementIndex] as UsingElement; if (usingElement != null && usingElement.IsMovable) { if (elements.Contains(usingElement)) { elements.Remove(usingElement); } tempElements.Remove(usingElement); namespaceElement.InsertChild(0, usingElement); elementIndex--; } else { if (tempElements[elementIndex] is RegionElement || tempElements[elementIndex] is GroupElement) { tempElements.AddRange(tempElements[elementIndex].Children); } else if (tempElements[elementIndex] is ConditionDirectiveElement) { ConditionDirectiveElement condition = tempElements[elementIndex] as ConditionDirectiveElement; while (condition != null) { if (namespaceElement.Parent == condition) { tempElements.AddRange(tempElements[elementIndex].Children); break; } condition = condition.ElseCondition; } } } } } else if (moveUsingsTo == CodeLevel.File) { tempElements = new List <ICodeElement>(); for (int elementIndex = 0; elementIndex < namespaceElement.Children.Count; elementIndex++) { UsingElement usingElement = namespaceElement.Children[elementIndex] as UsingElement; if (usingElement != null && usingElement.IsMovable) { namespaceElement.RemoveChild(usingElement); elements.Insert(0, usingElement); elementIndex--; } else if (namespaceElement.Children[elementIndex] is RegionElement || namespaceElement.Children[elementIndex] is GroupElement) { foreach (ICodeElement childElement in namespaceElement.Children[elementIndex].Children) { if (childElement is UsingElement || childElement is RegionElement || childElement is GroupElement) { tempElements.Add(childElement); } } } } for (int elementIndex = 0; elementIndex < tempElements.Count; elementIndex++) { UsingElement usingElement = tempElements[elementIndex] as UsingElement; if (usingElement != null && usingElement.IsMovable) { tempElements.Remove(usingElement); elements.Insert(0, usingElement); elementIndex--; } else if (tempElements[elementIndex] is RegionElement || tempElements[elementIndex] is GroupElement) { tempElements.AddRange(tempElements[elementIndex].Children); } } } else { throw new ArgumentOutOfRangeException( string.Format( "Unknown code level '{0}'.", moveUsingsTo)); } } }
internal override bool ParseSingleElement(ICollection<XName> unprocessedElements, XElement elem) { // Conceptual EntityModel needs to create Conceptual EntityContainer objects if (elem.Name.LocalName == BaseEntityContainer.ElementName) { if (_entityContainers.Count > 0) { // multiple EntityContainers detected, report an error var msg = String.Format(CultureInfo.CurrentCulture, Resources.TOO_MANY_ENTITY_CONTAINER_ELEMENTS, Namespace.Value); var error = new ErrorInfo( ErrorInfo.Severity.ERROR, msg, this, ErrorCodes.TOO_MANY_ENTITY_CONTAINER_ELEMENTS, ErrorClass.ParseError); Artifact.AddParseErrorForObject(this, error); } var ec = new ConceptualEntityContainer(this, elem); _entityContainers.Add(ec); ec.Parse(unprocessedElements); } else if (elem.Name.LocalName == ComplexType.ElementName) { var complexType = new ComplexType(this, elem); _complexTypes.Add(complexType); complexType.Parse(unprocessedElements); } else if (elem.Name.LocalName == UsingElement.ElementName) { var use = new UsingElement(this, elem); _usings.Add(use); use.Parse(unprocessedElements); } else if (elem.Name.LocalName == EnumType.ElementName) { // Check if enumType that represents the XElement <see DoParse method> var enumType = ModelItemAnnotation.GetModelItem(elem) as EnumType; if (enumType == null || enumType.IsDisposed) { enumType = new EnumType(this, elem); _enumTypes.Add(enumType); enumType.Parse(unprocessedElements); } } else { return base.ParseSingleElement(unprocessedElements, elem); } return true; }
public void MoveUsingsToFileTest() { List <ICodeElement> codeElements = new List <ICodeElement>(); UsingElement using1 = new UsingElement(); using1.Name = "System"; using1.IsMovable = true; codeElements.Add(using1); NamespaceElement namespaceElement = new NamespaceElement(); namespaceElement.Name = "TestNamespace"; codeElements.Add(namespaceElement); // Nested region and groups RegionElement region = new RegionElement(); region.Name = "Region"; namespaceElement.AddChild(region); GroupElement group = new GroupElement(); group.Name = "Group"; region.AddChild(group); UsingElement using2 = new UsingElement(); using2.Name = "System.IO"; using2.IsMovable = true; group.AddChild(using2); UsingElement using3 = new UsingElement(); using3.Name = "System.Collections"; using3.IsMovable = true; namespaceElement.AddChild(using3); TypeElement class1 = new TypeElement(); class1.Name = "Class1"; namespaceElement.AddChild(class1); TypeElement class2 = new TypeElement(); class2.Name = "Class2"; namespaceElement.AddChild(class2); CodeConfiguration configuration = CodeConfiguration.Default.Clone() as CodeConfiguration; CodeArranger arranger; // // Move to file. // configuration.Formatting.Usings.MoveTo = CodeLevel.File; arranger = new CodeArranger(configuration); ReadOnlyCollection <ICodeElement> arranged = arranger.Arrange(codeElements.AsReadOnly()); Assert.AreEqual(2, arranged.Count, "After arranging, an unexpected number of elements were returned."); GroupElement fileGroup = arranged[0] as GroupElement; Assert.IsNotNull(fileGroup); GroupElement innerGroup = fileGroup.Children[0] as GroupElement; Assert.AreEqual("System", innerGroup.Children[0].Name); Assert.AreEqual("System.Collections", innerGroup.Children[1].Name); Assert.AreEqual("System.IO", innerGroup.Children[2].Name); NamespaceElement namespaceElementTest = arranged[1] as NamespaceElement; Assert.IsNotNull(namespaceElementTest, "Expected a namespace element."); Assert.AreEqual(2, namespaceElementTest.Children.Count, "After arranging, an unexpected number of namespace elements were returned."); RegionElement typeRegion = namespaceElementTest.Children[1] as RegionElement; Assert.IsNotNull(typeRegion); Assert.AreEqual("Class1", typeRegion.Children[0].Name); Assert.AreEqual("Class2", typeRegion.Children[1].Name); }
/// <summary> /// Processes a using element. /// </summary> /// <param name="element">Using/Import directive code element.</param> public abstract void VisitUsingElement(UsingElement element);
/// <summary> /// Clones this instance. /// </summary> /// <returns>Clone of the code element.</returns> protected override CodeElement DoClone() { UsingElement clone = new UsingElement(); clone._redefine = _redefine; clone._isMovable = _isMovable; clone.IsStatic = IsStatic; return clone; }