public void CanArrangeTest() { ChainElementArranger chain = new ChainElementArranger(); FieldElement fieldElement = new FieldElement(); // // No arrangers in chain // Assert.IsFalse( chain.CanArrange(fieldElement), "Empty chain element arranger should not be able to arrange an element."); // // Add an arranger that can't arrange the element // TestElementArranger disabledArranger = new TestElementArranger(false); chain.AddArranger(disabledArranger); Assert.IsFalse(chain.CanArrange(fieldElement), "Unexpected return value from CanArrange."); // // Add an arranger that can arrange the element // TestElementArranger enabledArranger = new TestElementArranger(true); chain.AddArranger(enabledArranger); Assert.IsTrue(chain.CanArrange(fieldElement), "Unexpected return value from CanArrange."); // // Null // Assert.IsFalse(chain.CanArrange(null), "Unexpected return value from CanArrange."); }
public void EvaluateAndTest() { IConditionExpression nameExpression = new BinaryOperatorExpression( BinaryExpressionOperator.Equal, new ElementAttributeExpression(ElementAttributeType.Name), new StringExpression("Test")); IConditionExpression attributeExpression = new BinaryOperatorExpression( BinaryExpressionOperator.Equal, new ElementAttributeExpression(ElementAttributeType.Access), new StringExpression("Protected")); IConditionExpression expression = new BinaryOperatorExpression( BinaryExpressionOperator.And, nameExpression, attributeExpression); FieldElement element = new FieldElement(); element.Name = "Test"; element.Access = CodeAccess.Protected; bool result = ConditionExpressionEvaluator.Instance.Evaluate( expression, element); Assert.IsTrue(result, "Unexpected expression evaluation result."); element.Name = "Foo"; result = ConditionExpressionEvaluator.Instance.Evaluate( expression, element); Assert.IsFalse(result, "Unexpected expression evaluation result."); element.Name = "Test"; element.Access = CodeAccess.Private; result = ConditionExpressionEvaluator.Instance.Evaluate( expression, element); Assert.IsFalse(result, "Unexpected expression evaluation result."); }
public void IsMatchAccessTest() { ElementFilter filter = new ElementFilter("$(Access) : 'Protected'"); // // Not a match // FieldElement publicField = new FieldElement(); publicField.Access = CodeAccess.Public; Assert.IsFalse(filter.IsMatch(publicField), "IsMatch did not return the expected value."); // // Match // FieldElement protectedField = new FieldElement(); protectedField.Access = CodeAccess.Protected; Assert.IsTrue(filter.IsMatch(protectedField), "IsMatch did not return the expected value."); // // Multi flag // FieldElement protectedInternalField = new FieldElement(); protectedInternalField.Access = CodeAccess.Protected | CodeAccess.Internal; Assert.IsTrue(filter.IsMatch(protectedInternalField), "IsMatch did not return the expected value."); // // Null // Assert.IsFalse(filter.IsMatch(null), "IsMatch did not return the expected value."); }
public void IsMatchNameTest() { ElementFilter filter = new ElementFilter("$(Name) : 'Style'"); // // Not a match // FieldElement noMatch = new FieldElement(); noMatch.Name = "Test"; Assert.IsFalse(filter.IsMatch(noMatch), "IsMatch did not return the expected value."); // // Match // FieldElement match = new FieldElement(); match.Name = "Style"; Assert.IsTrue(filter.IsMatch(match), "IsMatch did not return the expected value."); match.Name = "ElementStyle"; Assert.IsTrue(filter.IsMatch(match), "IsMatch did not return the expected value."); match.Name = "StyleElement"; Assert.IsTrue(filter.IsMatch(match), "IsMatch did not return the expected value."); // // Null // Assert.IsFalse(filter.IsMatch(null), "IsMatch did not return the expected value."); }
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."); }
public void GetAttributeAccessTest() { FieldElement fieldElement = new FieldElement(); fieldElement.Name = "TestField"; fieldElement.Access = CodeAccess.Protected; string attribute = ElementUtilities.GetAttribute(ElementAttributeType.Access, fieldElement); Assert.AreEqual("Protected", attribute, "Unexpected attribute."); }
public void UnsupportedArrangeNoParentTest() { ChainElementArranger chain = new ChainElementArranger(); FieldElement fieldElement = new FieldElement(); // // Add an arranger that can't arrange the element // TestElementArranger disabledArranger = new TestElementArranger(false); chain.AddArranger(disabledArranger); Assert.IsFalse(chain.CanArrange(fieldElement), "Unexpected return value from CanArrange."); chain.ArrangeElement(null, fieldElement); }
public void GetAttributeAttributesTest() { FieldElement fieldElement = new FieldElement(); fieldElement.Name = "TestField"; string attribute = ElementUtilities.GetAttribute(ElementAttributeType.Attributes, fieldElement); Assert.AreEqual(string.Empty, attribute, "Unexpected attribute."); // // Add some attributes to the element. // AttributeElement attribute1 = new AttributeElement(); attribute1.Name = "Attribute1"; attribute1.BodyText = "false"; AttributeElement attribute2 = new AttributeElement(); attribute2.Name = "Attribute2"; attribute2.BodyText = "false"; fieldElement.AddAttribute(attribute1); fieldElement.AddAttribute(attribute2); attribute = ElementUtilities.GetAttribute(ElementAttributeType.Attributes, fieldElement); Assert.AreEqual("Attribute1, Attribute2", attribute, "Unexpected attribute."); // // Add nested attributes to the element. // fieldElement.ClearAttributes(); attribute1 = new AttributeElement(); attribute1.Name = "Attribute1"; attribute1.BodyText = "false"; attribute2 = new AttributeElement(); attribute2.Name = "Attribute2"; attribute2.BodyText = "false"; attribute1.AddChild(attribute2); fieldElement.AddAttribute(attribute1); attribute = ElementUtilities.GetAttribute(ElementAttributeType.Attributes, fieldElement); Assert.AreEqual("Attribute1, Attribute2", attribute, "Unexpected attribute."); }
public void EvaluateElementNameContainsTest() { IConditionExpression expression = new BinaryOperatorExpression( BinaryExpressionOperator.Contains, new ElementAttributeExpression(ElementAttributeType.Name), new StringExpression("Test")); FieldElement element = new FieldElement(); element.Name = "Test"; bool result = ConditionExpressionEvaluator.Instance.Evaluate( expression, element); Assert.IsTrue(result, "Unexpected expression evaluation result."); element.Name = "OnTest1"; result = ConditionExpressionEvaluator.Instance.Evaluate( expression, element); Assert.IsTrue(result, "Unexpected expression evaluation result."); element.Name = "Foo"; result = ConditionExpressionEvaluator.Instance.Evaluate( expression, element); Assert.IsFalse(result, "Unexpected expression evaluation result."); }
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 field. /// </summary> /// <param name="wordList">The word list.</param> /// <param name="access">The field access.</param> /// <param name="memberAttributes">The member attributes.</param> /// <param name="untypedAssignment">Whether or not the field is untyped.</param> /// <returns>A field code element.</returns> private FieldElement ParseField( StringCollection wordList, CodeAccess access, MemberModifiers memberAttributes, bool untypedAssignment) { FieldElement field = new FieldElement(); StringBuilder nameBuilder = new StringBuilder(DefaultWordLength); foreach (string word in wordList) { string trimmedWord = word.Trim(' ', VBSymbol.AliasSeparator); string upperWord = trimmedWord.ToUpperInvariant(); if ((!VBKeyword.IsVBKeyword(trimmedWord) || upperWord == VBKeyword.Custom.ToUpperInvariant() || upperWord == VBKeyword.Ansi.ToUpperInvariant() || upperWord == VBKeyword.Unicode.ToUpperInvariant() || upperWord == VBKeyword.Auto.ToUpperInvariant()) && trimmedWord.Length > 0) { nameBuilder.Append(trimmedWord); nameBuilder.Append(VBSymbol.AliasSeparator); nameBuilder.Append(' '); } } field.Name = nameBuilder.ToString().TrimEnd(VBSymbol.AliasSeparator, ' '); EatWhiteSpace(); if (!untypedAssignment) { string returnType = CaptureTypeName(); if (returnType.ToUpperInvariant() == VBKeyword.New.ToUpperInvariant()) { EatWhiteSpace(WhiteSpaceTypes.SpaceAndTab); field.InitialValue = VBKeyword.New + " " + ReadCodeLine().Trim(); } else { field.Type = returnType; } } field.Access = access; field.MemberModifiers = memberAttributes; EatWhiteSpace(WhiteSpaceTypes.SpaceAndTab); bool isAssignment = NextChar == VBSymbol.Assignment || untypedAssignment; if (isAssignment) { if (!untypedAssignment) { EatChar(VBSymbol.Assignment); } string initialValue = ParseInitialValue(); field.InitialValue = initialValue; } EatWhiteSpace(WhiteSpaceTypes.SpaceAndTab); if (NextChar == VBSymbol.BeginComment) { EatChar(VBSymbol.BeginComment); string commentText = ReadLine().Trim(); if (commentText.Length > 0) { CommentElement comment = new CommentElement(commentText); field.TrailingComment = comment; } } return field; }
/// <summary> /// Processes a field element. /// </summary> /// <param name="element">Field code element.</param> public abstract void VisitFieldElement(FieldElement element);
public void EvaluateInvalidOperatorTest() { IConditionExpression expression = new BinaryOperatorExpression( (BinaryExpressionOperator) int.MinValue, new ElementAttributeExpression(ElementAttributeType.Name), new StringExpression("Test")); FieldElement element = new FieldElement(); element.Name = "Test"; bool result = ConditionExpressionEvaluator.Instance.Evaluate( expression, element); }
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); } }
/// <summary> /// Processes a field element. /// </summary> /// <param name="element">Field code element.</param> public override void VisitFieldElement(FieldElement element) { this.WriteComments(element.HeaderComments); this.WriteAttributes(element); WriteAccess(element.Access); WriteMemberAttributes(element.MemberModifiers); if (element.IsVolatile) { Writer.Write(CSharpKeyword.Volatile); Writer.Write(' '); } if (element[CSharpExtendedProperties.Fixed] is bool && (bool)element[CSharpExtendedProperties.Fixed]) { Writer.Write(CSharpKeyword.Fixed); Writer.Write(' '); } Writer.Write(element.Type); Writer.Write(' '); Writer.Write(element.Name); if (!string.IsNullOrEmpty(element.InitialValue)) { Writer.Write(' '); Writer.Write(CSharpSymbol.Assignment); Writer.Write(' '); if (element.InitialValue.IndexOf("\n") >= 0) { string initialValue = element.InitialValue; int lineFeedIndex = initialValue.IndexOf('\n'); if (lineFeedIndex > 0) { string initialValueFirstLine = initialValue.Substring(0, lineFeedIndex + 1); initialValue = initialValue.Substring(lineFeedIndex + 1); Writer.Write(initialValueFirstLine); } WriteTextBlock(initialValue); } else { Writer.Write(element.InitialValue); } } Writer.Write(CSharpSymbol.EndOfStatement); if (element.TrailingComment != null) { Writer.Write(' '); int tabCountTemp = this.TabCount; this.TabCount = 0; element.TrailingComment.Accept(this); this.TabCount = tabCountTemp; } }
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); }
/// <summary> /// Processes a field element. /// </summary> /// <param name="element">Field code element.</param> public override void VisitFieldElement(FieldElement element) { this.WriteComments(element.HeaderComments); this.WriteAttributes(element); WriteAccess(element.Access); WriteMemberAttributes( element.MemberModifiers, element[VBExtendedProperties.Overloads] is bool && (bool)element[VBExtendedProperties.Overloads]); if (element[VBExtendedProperties.Dim] is bool && (bool)element[VBExtendedProperties.Dim]) { if (element.Access != CodeAccess.None) { Writer.Write(' '); } Writer.Write(VBKeyword.Dim); Writer.Write(' '); } if (element[VBExtendedProperties.WithEvents] is bool && (bool)element[VBExtendedProperties.WithEvents]) { Writer.Write(' '); Writer.Write(VBKeyword.WithEvents); Writer.Write(' '); } Writer.Write(element.Name); if (!string.IsNullOrEmpty(element.Type)) { WriteReturnType(element.Type); if (!string.IsNullOrEmpty(element.InitialValue)) { Writer.Write(' '); Writer.Write(VBSymbol.Assignment); Writer.Write(' '); Writer.Write(element.InitialValue); } } else if (!string.IsNullOrEmpty(element.InitialValue)) { Writer.Write(' '); if (element.InitialValue.StartsWith(VBKeyword.New + " ")) { Writer.Write(VBKeyword.As); } else { Writer.Write(VBSymbol.Assignment); } Writer.Write(' '); Writer.Write(element.InitialValue); } if (element.TrailingComment != null) { Writer.Write(' '); int tabCountTemp = this.TabCount; this.TabCount = 0; element.TrailingComment.Accept(this); this.TabCount = tabCountTemp; } }
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."); }
public void UnsupportedArrangeWithParentTest() { GroupElement parentElement = new GroupElement(); ChainElementArranger chain = new ChainElementArranger(); FieldElement fieldElement = new FieldElement(); // // Add an arranger that can't arrange the element // TestElementArranger disabledArranger = new TestElementArranger(false); chain.AddArranger(disabledArranger); Assert.IsFalse(chain.CanArrange(fieldElement), "Unexpected return value from CanArrange."); chain.ArrangeElement(parentElement, fieldElement); Assert.IsTrue(parentElement.Children.Contains(fieldElement)); }
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."); }
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."); }
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."); }
public void GetAttributeNoneTest() { FieldElement fieldElement = new FieldElement(); fieldElement.Name = "TestField"; string attribute = ElementUtilities.GetAttribute(ElementAttributeType.None, fieldElement); Assert.AreEqual(string.Empty, attribute, "Unexpected attribute."); }
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."); }
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 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 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."); }
/// <summary> /// Parses a field. /// </summary> /// <param name="isAssignment">Has field assignment.</param> /// <param name="access">Field access.</param> /// <param name="memberAttributes">The member attributes.</param> /// <param name="memberName">Name of the member.</param> /// <param name="returnType">Return type.</param> /// <param name="isVolatile">Whether or not the field is volatile.</param> /// <param name="isFixed">Whether or not the field is fixed.</param> /// <returns>A field code element.</returns> private FieldElement ParseField( bool isAssignment, CodeAccess access, MemberModifiers memberAttributes, string memberName, string returnType, bool isVolatile, bool isFixed) { FieldElement field = new FieldElement(); field.Name = memberName; field.Type = returnType; field.Access = access; field.MemberModifiers = memberAttributes; field.IsVolatile = isVolatile; field[CSharpExtendedProperties.Fixed] = isFixed; if (isAssignment) { string initialValue = ParseInitialValue(); field.InitialValue = initialValue; } EatWhiteSpace(WhiteSpaceTypes.SpaceAndTab); if (NextChar == CSharpSymbol.BeginComment) { EatChar(CSharpSymbol.BeginComment); if (NextChar == CSharpSymbol.BeginComment) { field.TrailingComment = ParseCommentLine(); } else if (NextChar == CSharpSymbol.BlockCommentModifier) { field.TrailingComment = ParseCommentBlock(); } } return field; }
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."); }
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); }