Ejemplo n.º 1
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.º 2
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.");
            }
        }