public void ArrangeNestedRegionTest() { List<ICodeElement> elements = new List<ICodeElement>(); TypeElement type = new TypeElement(); type.Type = TypeElementType.Class; type.Name = "TestClass"; FieldElement field = new FieldElement(); field.Name = "val"; field.Type = "int"; type.AddChild(field); elements.Add(type); // Create a configuration with a nested region CodeConfiguration codeConfiguration = new CodeConfiguration(); ElementConfiguration typeConfiguration = new ElementConfiguration(); typeConfiguration.ElementType = ElementType.Type; RegionConfiguration regionConfiguration1 = new RegionConfiguration(); regionConfiguration1.Name = "Region1"; RegionConfiguration regionConfiguration2 = new RegionConfiguration(); regionConfiguration2.Name = "Region2"; ElementConfiguration fieldConfiguration = new ElementConfiguration(); fieldConfiguration.ElementType = ElementType.Field; regionConfiguration2.Elements.Add(fieldConfiguration); regionConfiguration1.Elements.Add(regionConfiguration2); typeConfiguration.Elements.Add(regionConfiguration1); codeConfiguration.Elements.Add(typeConfiguration); CodeArranger arranger = new CodeArranger(codeConfiguration); ReadOnlyCollection<ICodeElement> arrangedElements = arranger.Arrange(elements.AsReadOnly()); Assert.AreEqual(1, arrangedElements.Count, "Unexpected number of arranged elements."); TypeElement arrangedType = arrangedElements[0] as TypeElement; Assert.IsNotNull(arrangedType, "Expected a type element after arranging."); Assert.AreEqual(1, arrangedType.Children.Count, "Unexpected number of arranged child elements."); RegionElement arrangedRegion1 = arrangedType.Children[0] as RegionElement; Assert.IsNotNull(arrangedRegion1, "Expected a region element after arranging."); Assert.AreEqual(regionConfiguration1.Name, arrangedRegion1.Name); Assert.AreEqual(1, arrangedRegion1.Children.Count, "Unexpected number of arranged child elements."); RegionElement arrangedRegion2 = arrangedRegion1.Children[0] as RegionElement; Assert.IsNotNull(arrangedRegion2, "Expected a region element after arranging."); Assert.AreEqual(regionConfiguration2.Name, arrangedRegion2.Name); Assert.AreEqual(1, arrangedRegion2.Children.Count, "Unexpected number of arranged child elements."); FieldElement arrangedFieldElement = arrangedRegion2.Children[0] as FieldElement; Assert.IsNotNull(arrangedFieldElement, "Expected a field element after arranging."); }
/// <summary> /// Clones an attributed element. /// </summary> /// <returns>Cloned attribute element state.</returns> protected override AttributedElement DoAttributedClone() { TypeElement clone = new TypeElement(); // // Copy state // clone._typeModifiers = _typeModifiers; clone._type = _type; foreach (InterfaceReference interfaceReference in Interfaces) { InterfaceReference referenceClone = interfaceReference.Clone() as InterfaceReference; clone.AddInterface(referenceClone); } foreach (TypeParameter typeParam in TypeParameters) { TypeParameter typeParamClone = typeParam.Clone() as TypeParameter; clone.TypeParametersBase.Add(typeParamClone); } return(clone); }
public void EvaluateElementParentAttributesContainsTest() { IConditionExpression expression = new BinaryOperatorExpression( BinaryExpressionOperator.Contains, new ElementAttributeExpression(ElementAttributeType.Attributes, ElementAttributeScope.Parent), new StringExpression("Attribute2")); FieldElement element = new FieldElement(); element.Name = "Test"; TypeElement typeElement = new TypeElement(); typeElement.Type = TypeElementType.Structure; typeElement.Name = "TestType"; typeElement.AddChild(element); typeElement.AddAttribute(new AttributeElement("Attribute1")); typeElement.AddAttribute(new AttributeElement("Attribute24")); bool result = ConditionExpressionEvaluator.Instance.Evaluate( expression, element); Assert.IsTrue(result, "Unexpected expression evaluation result."); typeElement.ClearAttributes(); result = ConditionExpressionEvaluator.Instance.Evaluate( expression, element); Assert.IsFalse(result, "Unexpected expression evaluation result."); }
/// <summary> /// Processes a type element. /// </summary> /// <param name="element">Type code element.</param> public override void VisitTypeElement(TypeElement element) { this.WriteComments(element.HeaderComments); this.WriteAttributes(element); if (element.Access != CodeAccess.None) { WriteAccess(element.Access); } else { WriteIndented(string.Empty); } if (element.IsNew) { Writer.Write(VBKeyword.Shadows); Writer.Write(' '); } if (element.IsSealed) { Writer.Write(VBKeyword.NotInheritable); Writer.Write(' '); } if (element.IsAbstract) { Writer.Write(VBKeyword.MustInherit); Writer.Write(' '); } if (element.IsPartial) { Writer.Write(VBKeyword.Partial); Writer.Write(' '); } StringBuilder builder = new StringBuilder(DefaultBlockLength); switch (element.Type) { case TypeElementType.Class: builder.Append(VBKeyword.Class); break; case TypeElementType.Enum: builder.Append(VBKeyword.Enumeration); break; case TypeElementType.Interface: builder.Append(VBKeyword.Interface); break; case TypeElementType.Structure: builder.Append(VBKeyword.Structure); break; case TypeElementType.Module: builder.Append(VBKeyword.Module); break; default: throw new ArgumentOutOfRangeException( string.Format( Thread.CurrentThread.CurrentCulture, "Unrecognized type element type {0}", element.Type)); } builder.Append(' '); builder.Append(element.Name); Writer.Write(builder.ToString()); WriteTypeParameters(element); if (element.Interfaces.Count > 0) { if (element.Type == TypeElementType.Enum) { Writer.Write(' '); Writer.Write(VBKeyword.As); Writer.Write(' '); Writer.Write(element.Interfaces[0].Name); } else { TabCount++; Writer.WriteLine(); for (int interfaceIndex = 0; interfaceIndex < element.Interfaces.Count; interfaceIndex++) { InterfaceReference interfaceReference = element.Interfaces[interfaceIndex]; builder = new StringBuilder(); if (interfaceReference.ReferenceType == InterfaceReferenceType.Class) { builder.Append(VBKeyword.Inherits); } else { builder.Append(VBKeyword.Implements); } builder.Append(' '); builder.Append(interfaceReference); WriteIndented(builder.ToString()); if (interfaceIndex < element.Interfaces.Count - 1) { Writer.WriteLine(); } } TabCount--; } } if (element.Type == TypeElementType.Enum) { WriteBody(element); } else { WriteBeginBlock(); WriteBlockChildren(element); WriteEndBlock(element); WriteClosingComment(element, VBSymbol.BeginComment.ToString()); } }
/// <summary> /// Gets all static fields for a Type. /// </summary> /// <param name="typeElement">Type element.</param> /// <returns>All static fields for the specified Type element.</returns> private FieldElement[] GetTypeStaticFields(TypeElement typeElement) { List<FieldElement> staticFields = new List<FieldElement>(); Action<ICodeElement> findStaticFields = delegate(ICodeElement codeElement) { FieldElement fieldElement = codeElement as FieldElement; if (fieldElement != null && fieldElement.MemberModifiers == MemberModifiers.Static) { bool isTypeChild = false; ICodeElement parentElement = codeElement.Parent; while (!isTypeChild && parentElement != null) { isTypeChild = parentElement == typeElement; if (!isTypeChild) { parentElement = parentElement.Parent; } } if (isTypeChild) { staticFields.Add(fieldElement); } } }; ElementUtilities.ProcessElementTree(typeElement, findStaticFields); return staticFields.ToArray(); }
/// <summary> /// If dependent static fields exist, then correct their ordering regardless /// of arranging rules. /// </summary> /// <param name="typeElement">Type code element.</param> private void CorrectStaticFieldDependencies(TypeElement typeElement) { FieldElement[] staticFields = GetTypeStaticFields(typeElement); for (int fieldIndex = 0; fieldIndex < staticFields.Length; fieldIndex++) { FieldElement staticField = staticFields[fieldIndex]; if (!string.IsNullOrEmpty(staticField.InitialValue)) { for (int compareFieldIndex = 0; compareFieldIndex < staticFields.Length; compareFieldIndex++) { FieldElement compareStaticField = staticFields[compareFieldIndex]; if (compareStaticField != staticField) { // If the static field references this field, then move the referenced // field if necessary. // TODO: Consider checking to see if the referenced name is not within // a string. if (staticField.InitialValue.Contains(compareStaticField.Name)) { int fieldPosition = staticField.Parent.Children.IndexOf(staticField); if (!(staticField.Parent == compareStaticField.Parent && compareStaticField.Parent.Children.IndexOf(compareStaticField) < fieldPosition)) { staticField.Parent.InsertChild(fieldPosition, compareStaticField); } } } } } } }
/// <summary> /// Clones an attributed element. /// </summary> /// <returns>Cloned attribute element state.</returns> protected override AttributedElement DoAttributedClone() { TypeElement clone = new TypeElement(); // // Copy state // clone._typeModifiers = _typeModifiers; clone._type = _type; foreach (InterfaceReference interfaceReference in Interfaces) { InterfaceReference referenceClone = interfaceReference.Clone() as InterfaceReference; clone.AddInterface(referenceClone); } foreach (TypeParameter typeParam in TypeParameters) { TypeParameter typeParamClone = typeParam.Clone() as TypeParameter; clone.TypeParametersBase.Add(typeParamClone); } return clone; }
public void DefaultArrangeConditionDirectiveTest() { List<ICodeElement> codeElements = new List<ICodeElement>(); ConditionDirectiveElement ifCondition = new ConditionDirectiveElement(); ifCondition.ConditionExpression = "DEBUG"; FieldElement field1 = new FieldElement(); field1.Name = "zField"; field1.Type = "int"; FieldElement field2 = new FieldElement(); field2.Name = "aField"; field2.Type = "int"; ifCondition.AddChild(field1); ifCondition.AddChild(field2); ifCondition.ElseCondition = new ConditionDirectiveElement(); FieldElement field3 = new FieldElement(); field3.Name = "testField"; field3.Type = "int"; FieldElement field1Clone = field1.Clone() as FieldElement; FieldElement field2Clone = field2.Clone() as FieldElement; TypeElement classElement = new TypeElement(); classElement.Name = "TestClass"; classElement.AddChild(field1Clone); classElement.AddChild(field2Clone); ifCondition.ElseCondition.AddChild(field3); ifCondition.ElseCondition.AddChild(classElement); codeElements.Add(ifCondition); 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."); ConditionDirectiveElement ifConditionTest = arranged[0] as ConditionDirectiveElement; Assert.IsNotNull(ifConditionTest, "Expected a condition directive element."); Assert.AreEqual(2, ifConditionTest.Children.Count, "After arranging, an unexpected number of nested elements were returned."); Assert.AreEqual(field2.Name, ifConditionTest.Children[0].Name); Assert.AreEqual(field1.Name, ifConditionTest.Children[1].Name); ConditionDirectiveElement elseConditionTest = ifConditionTest.ElseCondition; Assert.IsNotNull(elseConditionTest, "Expected a condition directive element."); Assert.AreEqual(2, ifConditionTest.Children.Count, "After arranging, an unexpected number of nested elements were returned."); Assert.AreEqual(field3.Name, elseConditionTest.Children[0].Name); Assert.AreEqual(classElement.Name, elseConditionTest.Children[1].Name); TypeElement classElementTest = elseConditionTest.Children[1] as TypeElement; Assert.IsNotNull(classElementTest, "Expected a type element."); Assert.AreEqual(1, classElementTest.Children.Count); Assert.AreEqual("Fields", classElementTest.Children[0].Name); }
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."); }
/// <summary> /// Parses a type definition. /// </summary> /// <param name="access">Type accessibility.</param> /// <param name="typeAttributes">Type modifiers.</param> /// <param name="elementType">Type element type.</param> /// <returns>Type code element.</returns> private TypeElement ParseType( CodeAccess access, TypeModifiers typeAttributes, TypeElementType elementType) { TypeElement typeElement = new TypeElement(); EatWhiteSpace(); string className = CaptureWord(); typeElement.Name = className; typeElement.Access = access; typeElement.Type = elementType; typeElement.TypeModifiers = typeAttributes; EatWhiteSpace(); if (elementType == TypeElementType.Enum) { EatWhiteSpace(); if (NextChar == CSharpSymbol.TypeImplements) { TryReadChar(); string interfaceName = CaptureTypeName(); InterfaceReference interfaceReference = new InterfaceReference(interfaceName, InterfaceReferenceType.None); typeElement.AddInterface(interfaceReference); } string enumText = ParseBlock(true, typeElement); // TODO: Parse enum values as fields typeElement.BodyText = enumText; } else { bool isGeneric = TryReadChar(CSharpSymbol.BeginGeneric); if (isGeneric) { string[] typeParameterNames = ParseAliasList(); foreach (string typeParameterName in typeParameterNames) { TypeParameter typeParameter = new TypeParameter(); typeParameter.Name = typeParameterName; typeElement.AddTypeParameter(typeParameter); } EatWhiteSpace(); if (!TryReadChar(CSharpSymbol.EndGeneric)) { this.OnParseError("Expected " + CSharpSymbol.EndGeneric); } } EatWhiteSpace(); bool implements = TryReadChar(CSharpSymbol.TypeImplements); if (implements) { string[] typeList = ParseAliasList(); foreach (string type in typeList) { InterfaceReference interfaceReference = new InterfaceReference(type, InterfaceReferenceType.None); typeElement.AddInterface(interfaceReference); } } EatWhiteSpace(); ParseTypeParameterConstraints(typeElement); // Associate any additional comments in the type definition with the type. ReadOnlyCollection<ICommentElement> extraComments = ParseComments(); foreach (ICommentElement comment in extraComments) { typeElement.AddHeaderComment(comment); } EatChar(CSharpSymbol.BeginBlock); EatWhiteSpace(); if (NextChar != CSharpSymbol.EndBlock) { // // Parse child elements // List<ICodeElement> childElements = ParseElements(typeElement); foreach (ICodeElement childElement in childElements) { typeElement.AddChild(childElement); } } EatChar(CSharpSymbol.EndBlock); } // // Types allow a trailing semi-colon // EatTrailingEndOfStatement(); return typeElement; }
public void ArrangeStaticFieldsTest() { List<ICodeElement> codeElements = new List<ICodeElement>(); TypeElement classElement = new TypeElement(); classElement.Type = TypeElementType.Class; classElement.Access = CodeAccess.Public; classElement.Name = "TestClass"; FieldElement fieldElement1 = new FieldElement(); fieldElement1.MemberModifiers = MemberModifiers.Static; fieldElement1.Access = CodeAccess.Protected; fieldElement1.Type = "object"; fieldElement1.Name = "_obj"; fieldElement1.InitialValue = "typeof(int).ToString();"; classElement.AddChild(fieldElement1); // This field has a static dependency. Normally it would be sorted first // due to its access, but we want to make sure it gets added after the // field for which it is dependent. FieldElement fieldElement2 = new FieldElement(); fieldElement2.MemberModifiers = MemberModifiers.Static; fieldElement2.Access = CodeAccess.Public; fieldElement2.Type = "bool"; fieldElement2.Name = "Initialized"; fieldElement2.InitialValue = "_initializationString != null"; classElement.AddChild(fieldElement2); FieldElement fieldElement3 = new FieldElement(); fieldElement3.MemberModifiers = MemberModifiers.Static; fieldElement3.Access = CodeAccess.Private; fieldElement3.Type = "string"; fieldElement3.Name = "_initializationString"; fieldElement3.InitialValue = "_obj"; classElement.AddChild(fieldElement3); codeElements.Add(classElement); 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."); TypeElement typeElement = arranged[0] as TypeElement; Assert.IsNotNull(typeElement, "Expected a type element."); List<FieldElement> staticFields = new List<FieldElement>(); Action<ICodeElement> findStaticFields = delegate(ICodeElement codeElement) { FieldElement fieldElement = codeElement as FieldElement; if (fieldElement != null && fieldElement.MemberModifiers == MemberModifiers.Static) { staticFields.Add(fieldElement); } }; ElementUtilities.ProcessElementTree(typeElement, findStaticFields); Assert.AreEqual(3, staticFields.Count, "Unexpected number of static fields after arranging."); Assert.AreEqual("_obj", staticFields[0].Name); Assert.AreEqual("_initializationString", staticFields[1].Name); Assert.AreEqual("Initialized", staticFields[2].Name); // // Remove the dependency // fieldElement2.InitialValue = "true"; fieldElement3.InitialValue = "\"test\""; arranged = arranger.Arrange(codeElements.AsReadOnly()); Assert.AreEqual(1, arranged.Count, "After arranging, an unexpected number of elements were returned."); typeElement = arranged[0] as TypeElement; Assert.IsNotNull(typeElement, "Expected a type element."); staticFields.Clear(); ElementUtilities.ProcessElementTree(typeElement, findStaticFields); Assert.AreEqual(3, staticFields.Count, "Unexpected number of static fields after arranging."); Assert.AreEqual("Initialized", staticFields[0].Name); Assert.AreEqual("_obj", staticFields[1].Name); Assert.AreEqual("_initializationString", staticFields[2].Name); }
public void MoveUsingsToNamespaceTest() { List<ICodeElement> codeElements = new List<ICodeElement>(); UsingElement using1 = new UsingElement(); using1.Name = "System"; using1.IsMovable = true; codeElements.Add(using1); // Nested region and groups RegionElement region = new RegionElement(); region.Name = "Region"; codeElements.Add(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); NamespaceElement namespaceElement = new NamespaceElement(); namespaceElement.Name = "TestNamespace"; codeElements.Add(namespaceElement); 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 namespace. // configuration.Formatting.Usings.MoveTo = CodeLevel.Namespace; 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."); 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."); GroupElement namespaceGroup = namespaceElementTest.Children[0] as GroupElement; Assert.IsNotNull(namespaceGroup); GroupElement 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); RegionElement typeRegion = namespaceElementTest.Children[1] as RegionElement; Assert.IsNotNull(typeRegion); Assert.AreEqual("Class1", typeRegion.Children[0].Name); Assert.AreEqual("Class2", typeRegion.Children[1].Name); }
public void DefaultArrangeStructLayoutTest() { List<ICodeElement> codeElements = new List<ICodeElement>(); TypeElement structElement = new TypeElement(); structElement.Type = TypeElementType.Structure; structElement.Access = CodeAccess.Public; structElement.Name = "TestStructure"; structElement.AddAttribute(new AttributeElement("System.Runtime.InteropServices.StructLayout")); NamespaceElement namespaceElement = new NamespaceElement(); namespaceElement.Name = "TestNamespace"; namespaceElement.AddChild(structElement); FieldElement fieldElement1 = new FieldElement(); fieldElement1.Type = "int"; fieldElement1.Access = CodeAccess.Public; fieldElement1.Name = "z"; structElement.AddChild(fieldElement1); FieldElement fieldElement2 = new FieldElement(); fieldElement2.Type = "int"; fieldElement2.Access = CodeAccess.Public; fieldElement2.Name = "x"; structElement.AddChild(fieldElement2); FieldElement fieldElement3 = new FieldElement(); fieldElement3.Type = "int"; fieldElement3.Access = CodeAccess.Public; fieldElement3.Name = "y"; structElement.AddChild(fieldElement3); 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(1, namespaceElementTest.Children.Count, "After arranging, an unexpected number of namespace elements were returned."); RegionElement typeRegionElement = namespaceElementTest.Children[0] as RegionElement; Assert.IsNotNull(typeRegionElement, "Expected a region element."); Assert.AreEqual("Types", typeRegionElement.Name); Assert.AreEqual(1, typeRegionElement.Children.Count, "After arranging, an unexpected number of namespace elements were returned."); TypeElement typeElement = typeRegionElement.Children[0] as TypeElement; Assert.IsNotNull(typeElement, "Expected a type element."); Assert.AreEqual(TypeElementType.Structure, typeElement.Type, "Unexpected type element type."); Assert.AreEqual(structElement.Name, typeElement.Name, "Unexpected type element name."); Assert.AreEqual(1, typeElement.Children.Count, "An unexpected number of class child elements were returned."); RegionElement regionElement = typeElement.Children[0] as RegionElement; Assert.IsNotNull(regionElement, "Expected a region element but was {0}.", regionElement.ElementType); Assert.AreEqual("Fixed Fields", regionElement.Name, "Unexpected region name."); Assert.AreEqual(3, regionElement.Children.Count, "Unexpected number of region child elements."); // The fields should not have been sorted Assert.AreEqual(fieldElement1.Name, regionElement.Children[0].Name); Assert.AreEqual(fieldElement2.Name, regionElement.Children[1].Name); Assert.AreEqual(fieldElement3.Name, regionElement.Children[2].Name); }
public void DefaultArrangeSimpleClassTest() { List<ICodeElement> codeElements = new List<ICodeElement>(); TypeElement classElement = new TypeElement(); classElement.Type = TypeElementType.Class; classElement.Access = CodeAccess.Public; classElement.Name = "TestClass"; NamespaceElement namespaceElement = new NamespaceElement(); namespaceElement.Name = "TestNamespace"; namespaceElement.AddChild(classElement); MethodElement methodElement = new MethodElement(); methodElement.Type = "void"; methodElement.Access = CodeAccess.Public; methodElement.Name = "DoSomething"; classElement.AddChild(methodElement); FieldElement fieldElement = new FieldElement(); fieldElement.Type = "bool"; fieldElement.Access = CodeAccess.Private; fieldElement.Name = "_val"; classElement.AddChild(fieldElement); PropertyElement propertyElement = new PropertyElement(); propertyElement.Type = "bool"; propertyElement.Access = CodeAccess.Public; propertyElement.Name = "Value"; propertyElement.BodyText = "return _val"; classElement.AddChild(propertyElement); 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(1, namespaceElementTest.Children.Count, "After arranging, an unexpected number of namespace elements were returned."); RegionElement typeRegionElement = namespaceElementTest.Children[0] as RegionElement; Assert.IsNotNull(typeRegionElement, "Expected a region element."); Assert.AreEqual("Types", typeRegionElement.Name); Assert.AreEqual(1, typeRegionElement.Children.Count, "After arranging, an unexpected number of namespace elements were returned."); TypeElement typeElement = typeRegionElement.Children[0] as TypeElement; Assert.IsNotNull(typeElement, "Expected a type element."); Assert.AreEqual(TypeElementType.Class, typeElement.Type, "Unexpected type element type."); Assert.AreEqual(classElement.Name, typeElement.Name, "Unexpected type element name."); Assert.AreEqual(3, typeElement.Children.Count, "An unexpected number of class child elements were returned."); List<RegionElement> regionElements = new List<RegionElement>(); foreach (ICodeElement classChildElement in typeElement.Children) { RegionElement regionElement = classChildElement as RegionElement; Assert.IsNotNull( regionElement, "Expected a region element but was {0}.", classChildElement.ElementType); regionElements.Add(regionElement); } Assert.AreEqual("Fields", regionElements[0].Name, "Unexpected region element name."); Assert.AreEqual("Properties", regionElements[1].Name, "Unexpected region element name."); Assert.AreEqual("Methods", regionElements[2].Name, "Unexpected region element name."); GroupElement fieldGroupElement = regionElements[0].Children[0].Children[0] as GroupElement; Assert.IsNotNull(fieldGroupElement, "Expected a group element for fields."); foreach (ICodeElement codeElement in fieldGroupElement.Children) { FieldElement fieldElementTest = codeElement as FieldElement; Assert.IsNotNull( fieldElementTest, "Expected a field element but was type {0}: {1}", codeElement.ElementType, codeElement); } Assert.AreEqual(1, regionElements[1].Children.Count); foreach (ICodeElement codeElement in regionElements[1].Children[0].Children) { PropertyElement propertyElementTest = codeElement as PropertyElement; Assert.IsNotNull( propertyElementTest, "Expected a property element but was type {0}: {1}", codeElement.ElementType, codeElement); } Assert.AreEqual(1, regionElements[2].Children.Count); foreach (ICodeElement codeElement in regionElements[2].Children[0].Children) { MethodElement methodElementTest = codeElement as MethodElement; Assert.IsNotNull( methodElementTest, "Expected a method element but was type {0}: {1}", codeElement.ElementType, codeElement); } }
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."); }
/// <summary> /// Processes a type element. /// </summary> /// <param name="element">Type code element.</param> public abstract void VisitTypeElement(TypeElement element);
/// <summary> /// Parses a type definition. /// </summary> /// <param name="access">The access.</param> /// <param name="typeAttributes">The type attributes.</param> /// <param name="elementType">Type of the element.</param> /// <returns>A type code element.</returns> private TypeElement ParseType( CodeAccess access, TypeModifiers typeAttributes, TypeElementType elementType) { TypeElement typeElement = new TypeElement(); EatWhiteSpace(); string className = CaptureWord(); typeElement.Name = className; typeElement.Access = access; typeElement.Type = elementType; typeElement.TypeModifiers = typeAttributes; if (elementType == TypeElementType.Enum) { EatLineContinuation(); if (NextChar == VBKeyword.As[0]) { EatWord(VBKeyword.As); string interfaceName = CaptureTypeName(); InterfaceReference interfaceReference = new InterfaceReference(interfaceName, InterfaceReferenceType.None); typeElement.AddInterface(interfaceReference); } string enumText = ParseBlock(VBKeyword.Enumeration); // TODO: Parse enum values as fields typeElement.BodyText = enumText; } else { EatWhiteSpace(); bool isGeneric = TryReadChar(VBSymbol.BeginParameterList); if (isGeneric) { EatWord(VBKeyword.Of, "Expected Of"); this.ParseTypeParameters(typeElement); } EatWhiteSpace(); // // Parse child elements // List<ICodeElement> childElements = ParseElements(typeElement); foreach (ICodeElement childElement in childElements) { typeElement.AddChild(childElement); } EatWhiteSpace(); string elementTypeString = EnumUtilities.ToString(elementType); EatWord(elementTypeString, "Expected End " + elementTypeString); } return typeElement; }
/// <summary> /// Processes a type element. /// </summary> /// <param name="element">Type elemenent to process.</param> public override void VisitTypeElement(TypeElement element) { this.WriteComments(element.HeaderComments); this.WriteAttributes(element); if (element.Access != CodeAccess.None) { WriteAccess(element.Access); } else { WriteIndented(string.Empty); } if (element.IsNew) { Writer.Write(CSharpKeyword.New); Writer.Write(' '); } if (element.IsUnsafe) { Writer.Write(CSharpKeyword.Unsafe); Writer.Write(' '); } if (element.IsStatic) { Writer.Write(CSharpKeyword.Static); Writer.Write(' '); } if (element.IsSealed) { Writer.Write(CSharpKeyword.Sealed); Writer.Write(' '); } if (element.IsAbstract) { Writer.Write(CSharpKeyword.Abstract); Writer.Write(' '); } if (element.IsPartial) { Writer.Write(CSharpKeyword.Partial); Writer.Write(' '); } StringBuilder builder = new StringBuilder(DefaultBlockLength); switch (element.Type) { case TypeElementType.Class: builder.Append(CSharpKeyword.Class); break; case TypeElementType.Enum: builder.Append(CSharpKeyword.Enumeration); break; case TypeElementType.Interface: builder.Append(CSharpKeyword.Interface); break; case TypeElementType.Structure: builder.Append(CSharpKeyword.Structure); break; default: throw new ArgumentOutOfRangeException( string.Format( Thread.CurrentThread.CurrentCulture, "Unhandled type element type {0}", element.Type)); } builder.Append(' '); builder.Append(element.Name); Writer.Write(builder.ToString()); WriteTypeParameters(element); if (element.Interfaces.Count > 0) { builder = new StringBuilder(DefaultBlockLength); builder.Append(' '); builder.Append(CSharpSymbol.TypeImplements); builder.Append(' '); for (int interfaceIndex = 0; interfaceIndex < element.Interfaces.Count; interfaceIndex++) { InterfaceReference interfaceReference = element.Interfaces[interfaceIndex]; builder.Append(interfaceReference.Name); if (interfaceIndex < element.Interfaces.Count - 1) { builder.Append(CSharpSymbol.AliasSeparator); builder.Append(' '); } } Writer.Write(builder.ToString()); } WriteTypeParameterConstraints(element); Writer.WriteLine(); if (element.Type == TypeElementType.Enum) { WriteBody(element); } else { WriteBeginBlock(); WriteBlockChildren(element); WriteEndBlock(); WriteClosingComment(element, ClosingCommentPrefix); } }
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 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."); }