Represents a using/import statement within code.
Inheritance: CommentedElement
Ejemplo n.º 1
0
        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.");
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Parses an import directive.
        /// </summary>
        /// <returns>Using directive code element.</returns>
        private UsingElement ParseImport()
        {
            UsingElement usingElement = new UsingElement();
            string alias = CaptureWord();
            if (string.IsNullOrEmpty(alias))
            {
                this.OnParseError("Expected a namepace name");
            }

            EatWhiteSpace(WhiteSpaceTypes.SpaceAndTab);

            bool endOfStatement =
                TryReadChar(Environment.NewLine[0]) ||
                TryReadChar('\n') ||
                TryReadChar(VBSymbol.LineDelimiter);
            if (endOfStatement || NextChar == EmptyChar)
            {
                usingElement.Name = alias;
            }
            else
            {
                EatLineContinuation();
                bool assign = TryReadChar(VBSymbol.Assignment);
                if (!assign)
                {
                    this.OnParseError(
                        string.Format(
                        Thread.CurrentThread.CurrentCulture,
                        "Expected {0} or end of statement.",
                        VBSymbol.Assignment));
                }
                else
                {
                    string name = CaptureWord();
                    if (string.IsNullOrEmpty(name))
                    {
                        this.OnParseError("Expected a type or namepace name");
                    }
                    else
                    {
                        usingElement.Name = alias;
                        usingElement.Redefine = name;
                        TryReadChar(Environment.NewLine[0]);
                    }
                }
            }

            return usingElement;
        }
Ejemplo n.º 3
0
        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.");
        }
Ejemplo n.º 4
0
        /// <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());
        }
Ejemplo n.º 5
0
 /// <summary>
 /// Processes a using element.
 /// </summary>
 /// <param name="element">Using/Import directive code element.</param>
 public abstract void VisitUsingElement(UsingElement element);
Ejemplo n.º 6
0
        /// <summary>
        /// Parses a using directive code element.
        /// </summary>
        /// <returns>Using directive code element.</returns>
        private UsingElement ParseUsing()
        {
            UsingElement usingElement = new UsingElement();
            string alias = CaptureWord();
            if (string.IsNullOrEmpty(alias))
            {
                this.OnParseError("Expected a namepace name");
            }

            EatWhiteSpace();

            bool endOfStatement = TryReadChar(CSharpSymbol.EndOfStatement);
            if (endOfStatement)
            {
                usingElement.Name = alias;
            }
            else
            {
                bool assign = TryReadChar(CSharpSymbol.Assignment);
                if (!assign)
                {
                    this.OnParseError(
                        string.Format(
                        Thread.CurrentThread.CurrentCulture,
                        "Expected {0} or {1}.",
                        CSharpSymbol.Assignment,
                        CSharpSymbol.EndOfStatement));
                }
                else
                {
                    string name = CaptureWord();
                    if (string.IsNullOrEmpty(name))
                    {
                        this.OnParseError("Expected a type or namepace name");
                    }
                    else
                    {
                        EatWhiteSpace();

                        char nextChar = NextChar;
                        if (nextChar == CSharpSymbol.BeginGeneric)
                        {
                            name += CSharpSymbol.BeginGeneric.ToString() +
                                ParseNestedText(CSharpSymbol.BeginGeneric, CSharpSymbol.EndGeneric, true, true) +
                                CSharpSymbol.EndGeneric.ToString();
                        }
                        usingElement.Name = alias;
                        usingElement.Redefine = name;
                        EatChar(CSharpSymbol.EndOfStatement);
                    }
                }
            }

            // C# supports moving of using elements from file to namespace level
            // and vice versa
            usingElement.IsMovable = true;

            return usingElement;
        }
Ejemplo n.º 7
0
        public void InsertTest()
        {
            GroupBy groupBy = new GroupBy();
            groupBy.By = ElementAttributeType.Name;
            groupBy.AttributeCapture = "^(.*?)(\\.|$)";

            GroupedInserter groupedInserter = new GroupedInserter(groupBy);

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

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

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

            UsingElement using3 = new UsingElement();
            using3.Name = "System.Text";
            groupedInserter.InsertElement(groupElement, using3);
            Assert.AreEqual(1, groupElement.Children.Count, "Group element was not inserted into the parent.");
            Assert.IsTrue(groupElement.Children[0] is GroupElement, "Group element was not inserted into the parent.");
            Assert.AreEqual(3, groupElement.Children[0].Children.Count, "Element was not inserted into the parent.");
            Assert.AreEqual(0, groupElement.Children[0].Children.IndexOf(using1), "Element is not at the correct index[0].Children.");
            Assert.AreEqual(1, groupElement.Children[0].Children.IndexOf(using2), "Element is not at the correct index.");
            Assert.AreEqual(2, groupElement.Children[0].Children.IndexOf(using3), "Element is not at the correct index.");
        }
Ejemplo n.º 8
0
        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);
        }
Ejemplo n.º 9
0
        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.");
        }
Ejemplo n.º 10
0
        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.");
            }
        }
Ejemplo n.º 11
0
        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.");
        }
Ejemplo n.º 12
0
        /// <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());
        }