Exemplo n.º 1
0
        public void SpacesTest()
        {
            CodeConfiguration configuration = CodeConfiguration.Load(Path.Combine(TestContext.CurrentContext.WorkDirectory, "TestConfigurations", "SpacesConfig.xml"));

            Assert.IsNotNull(configuration);
            Assert.AreEqual(TabStyle.Spaces, configuration.Formatting.Tabs.TabStyle, "Unexpected tab style.");
        }
Exemplo n.º 2
0
        public void SpacesTest()
        {
            CodeConfiguration configuration = CodeConfiguration.Load(@"TestConfigurations\SpacesConfig.xml");

            Assert.IsNotNull(configuration);
            Assert.AreEqual(TabStyle.Spaces, configuration.Formatting.Tabs.TabStyle, "Unexpected tab style.");
        }
Exemplo n.º 3
0
        public void NoEndRegionNamesTest()
        {
            CodeConfiguration configuration = CodeConfiguration.Load(Path.Combine(TestContext.CurrentContext.WorkDirectory, "TestConfigurations", "NoEndRegionNames.xml"));

            Assert.IsNotNull(configuration);
            Assert.IsFalse(configuration.Formatting.Regions.EndRegionNameEnabled, "Unexpected value for EndRegionNameEnabled.");
        }
Exemplo n.º 4
0
        public static string Format(string cSharpCode)
        {
            // We prepare stuff
            CodeConfiguration configuration = CodeConfiguration.Default;

            configuration.Formatting.Tabs.TabStyle = TabStyle.Tabs;
            configuration.Formatting.LineSpacing.RemoveConsecutiveBlankLines = true;
            configuration.Formatting.Regions.Style = RegionStyle.NoDirective;
            CodeArranger       codeArranger = new CodeArranger(configuration);
            ICodeElementParser parser       = new CSharpParser {
                Configuration = configuration
            };

            // We parse
            StringReader reader = new StringReader(cSharpCode);
            ReadOnlyCollection <ICodeElement> elements = parser.Parse(reader);

            // We reorganize the AST
            elements = codeArranger.Arrange(elements);

            // We rewrite
            ICodeElementWriter codeWriter = new CSharpWriter {
                Configuration = configuration
            };
            StringWriter writer = new StringWriter(CultureInfo.InvariantCulture);

            codeWriter.Write(elements, writer);
            return(writer.ToString());
        }
Exemplo n.º 5
0
        public void NoEndRegionNamesTest()
        {
            CodeConfiguration configuration = CodeConfiguration.Load(@"TestConfigurations\NoEndRegionNames.xml");

            Assert.IsNotNull(configuration);
            Assert.IsFalse(configuration.Formatting.Regions.EndRegionNameEnabled, "Unexpected value for EndRegionNameEnabled.");
        }
Exemplo n.º 6
0
        public void NoEndRegionNamesTest()
        {
            string testPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase).Replace(@"file:\", string.Empty);

            CodeConfiguration configuration = CodeConfiguration.Load(Path.Combine(testPath, @"TestConfigurations\NoEndRegionNames.xml"));

            Assert.IsNotNull(configuration);
            Assert.IsFalse(configuration.Formatting.Regions.EndRegionNameEnabled, "Unexpected value for EndRegionNameEnabled.");
        }
Exemplo n.º 7
0
        public void SpacesTest()
        {
            string testPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase).Replace(@"file:\", string.Empty);

            CodeConfiguration configuration = CodeConfiguration.Load(Path.Combine(testPath, @"TestConfigurations\SpacesConfig.xml"));

            Assert.IsNotNull(configuration);
            Assert.AreEqual(TabStyle.Spaces, configuration.Formatting.Tabs.TabStyle, "Unexpected tab style.");
        }
        public async Task ExecuteFuncTest()
        {
            // Arrange
            var config = new CodeConfiguration();

            // Act
            var result = await config.ExecuteAsync(() => true);

            // Assert
            Assert.True(result);
        }
Exemplo n.º 9
0
        /// <summary>
        /// Creates a new FileManager.
        /// </summary>
        /// <param name="configuration">The configuration.</param>
        public ProjectManager(CodeConfiguration configuration)
        {
            if (configuration == null)
            {
                throw new ArgumentNullException("configuration");
            }

            _configuration = configuration;

            Initialize();
        }
Exemplo n.º 10
0
        /// <summary>
        /// Creates a new code arranger with the specified configuration.
        /// </summary>
        /// <param name="configuration">Configuration to use for arranging code members.</param>
        public CodeArranger(CodeConfiguration configuration)
        {
            if (configuration == null)
            {
                throw new ArgumentNullException("configuration");
            }

            // Clone the configuration information so we don't have to worry about it
            // changing during processing.
            _configuration = configuration.Clone() as CodeConfiguration;
        }
        public void ExecuteFuncTest()
        {
            // Arrange
            var config = new CodeConfiguration();

            // Act
            var result = config.Execute(() => true);

            // Assert
            Assert.True(result);
        }
        public void ExecuteFuncTest()
        {
            // Arrange
            var config = new CodeConfiguration();

            // Act
            var result = config.Execute(() => true);

            // Assert
            Assert.True(result);
        }
        public async Task ExecuteActionTest()
        {
            // Arrange
            var config = new CodeConfiguration();
            var num    = 0;

            // Act
            await config.ExecuteAsync(() => { num++; });

            // Assert
            Assert.AreEqual(1, num);
        }
        public async Task ExecuteActionTest()
        {
            // Arrange
            var config = new CodeConfiguration();
            var num = 0;

            // Act
            await config.ExecuteAsync(() => { num++; });

            // Assert
            Assert.AreEqual(1, num);
        }
        public void ExecuteActionTest()
        {
            // Arrange
            var config = new CodeConfiguration();
            var num = 0;

            // Act
            config.Execute(() => { num++; });

            // Assert
            Assert.AreEqual(1, num);
        }
Exemplo n.º 16
0
        /// <summary>
        /// Creates a new VBWriteVisitor.
        /// </summary>
        /// <param name="writer">The writer.</param>
        /// <param name="configuration">The configuration.</param>
        protected CodeWriteVisitor(TextWriter writer, CodeConfiguration configuration)
        {
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }

            Debug.Assert(configuration != null, "Configuration should not be null.");

            _writer        = writer;
            _configuration = configuration;
        }
        public void ExecuteActionTest()
        {
            // Arrange
            var config = new CodeConfiguration();
            var num    = 0;

            // Act
            config.Execute(() => { num++; });

            // Assert
            Assert.AreEqual(1, num);
        }
Exemplo n.º 18
0
        public void DefaultArrangeNoUsingMoveTest()
        {
            CodeConfiguration configuration = CodeConfiguration.Default.Clone() as CodeConfiguration;

            configuration.Formatting.Usings.MoveTo = CodeLevel.None;

            CodeArranger arranger = new CodeArranger(configuration);

            ReadOnlyCollection <ICodeElement> arranged = arranger.Arrange(_testElements);

            //
            // Verify using statements were grouped and sorted correctly
            //
            Assert.AreEqual(3, arranged.Count, "An unexpected number of root elements were returned from Arrange.");

            RegionElement regionElement = arranged[0] as RegionElement;

            Assert.IsNotNull(regionElement, "Expected a region element.");
            Assert.AreEqual("Header", regionElement.Name);

            GroupElement groupElement = arranged[1] as GroupElement;

            Assert.IsNotNull(groupElement, "Expected a group element.");
            Assert.AreEqual("Namespace", groupElement.Name, "Unexpected group name.");
            Assert.AreEqual(1, groupElement.Children.Count, "Group contains an unexpected number of child elements.");

            groupElement = groupElement.Children[0] as GroupElement;
            Assert.IsNotNull(groupElement, "Expected a group element.");
            Assert.AreEqual("System", groupElement.Name, "Unexpected group name.");
            Assert.AreEqual(7, groupElement.Children.Count, "Group contains an unexpected number of child elements.");

            string lastUsingName = null;

            foreach (CodeElement groupedElement in groupElement.Children)
            {
                UsingElement usingElement = groupedElement as UsingElement;
                Assert.IsNotNull(usingElement, "Expected a using element.");

                string usingName = usingElement.Name;
                if (lastUsingName != null)
                {
                    Assert.AreEqual(
                        -1, lastUsingName.CompareTo(usingName), "Expected using statements to be sorted by name.");
                }
            }

            //
            // Verify the namespace arrangement
            //
            NamespaceElement namespaceElement = arranged[2] as NamespaceElement;

            Assert.IsNotNull(namespaceElement, "Expected a namespace element.");
        }
Exemplo n.º 19
0
        public void CreateTest()
        {
            CodeConfiguration configuration = new CodeConfiguration();

            Assert.IsNotNull(configuration.Elements, "Elements collection should not be null.");
            Assert.AreEqual(0, configuration.Elements.Count, "Elements collection should be empty.");

            //
            // Test the default tab configuration
            //
            Assert.IsNotNull(configuration.Formatting.Tabs, "Tabs configuration should not be null.");
            Assert.AreEqual(TabStyle.Spaces, configuration.Formatting.Tabs.TabStyle, "Unexpected default tab style.");
            Assert.AreEqual(4, configuration.Formatting.Tabs.SpacesPerTab, "Unexpected defatult number of spaces per tab.");
        }
        public async Task ExecuteFuncAsyncTest()
        {
            // Arrange
            var config = new CodeConfiguration();

            // Act
            var result = await config.ExecuteAsync(async() =>
            {
                await Task.Yield();
                return(true);
            });

            // Assert
            Assert.True(result);
        }
Exemplo n.º 21
0
        /// <summary>
        /// Loads the configuration file that specifies how elements will be arranged.
        /// </summary>
        /// <param name="configFile">The config file.</param>
        private void LoadConfiguration(string configFile)
        {
            if (_configuration == null)
            {
                if (configFile != null)
                {
                    _configuration = CodeConfiguration.Load(configFile);
                }
                else
                {
                    _configuration = CodeConfiguration.Default;
                }

                _projectManager = new ProjectManager(_configuration);
                _encoding       = _configuration.Encoding.GetEncoding();
            }
        }
        public async Task ExecuteActionRetryTest()
        {
            // Arrange
            var config = new CodeConfiguration();
            var num = 0;
            // Act
            await config.ExecuteAsync(() =>
            {
                num++;
                throw new Exception();
            }, e =>
            {
                num++;
            });

            // Assert
            Assert.AreEqual(4, num);
        }
        public async Task ExecuteActionRetryTest()
        {
            // Arrange
            var config = new CodeConfiguration();
            var num    = 0;
            // Act
            await config.ExecuteAsync(() =>
            {
                num++;
                throw new Exception();
            }, e =>
            {
                num++;
            });

            // Assert
            Assert.AreEqual(4, num);
        }
        public void ExecuteFuncRetryTest()
        {
            // Arrange
            var config = new CodeConfiguration();
            var num = 0;
            // Act
            var actual = config.Execute(() =>
            {
                num++;
                throw new Exception();
            }, e =>
            {
                num++;
                return num;
            });

            // Assert
            Assert.AreEqual(4, actual);
        }
        public async Task ExecuteFuncRetryTest()
        {
            // Arrange
            var config = new CodeConfiguration();
            var num    = 0;
            // Act
            var actual = await config.ExecuteAsync(() =>
            {
                num++;
                throw new Exception();
            }, e =>
            {
                num++;
                return(num);
            });

            // Assert
            Assert.AreEqual(4, actual);
        }
Exemplo n.º 26
0
        public void CloneTest()
        {
            CodeConfiguration defaultConfig = CodeConfiguration.Default;

            Assert.IsNotNull(defaultConfig, "Default configuration should not be null.");

            Assert.AreEqual(7, defaultConfig.Elements.Count, "Unexpected number of root level elements.");

            CodeConfiguration clonedConfig = defaultConfig.Clone() as CodeConfiguration;

            Assert.IsNotNull(clonedConfig, "Clone should return an instance.");

            Assert.AreNotSame(defaultConfig, clonedConfig, "Clone should be a different instance.");

            Assert.AreEqual(defaultConfig.Elements.Count, clonedConfig.Elements.Count, "Child element state was not copied correctly.");
            Assert.AreEqual(defaultConfig.Handlers.Count, clonedConfig.Handlers.Count, "Handler state was not copied correctly.");
            Assert.AreEqual(defaultConfig.Formatting.Tabs.TabStyle, clonedConfig.Formatting.Tabs.TabStyle, "Tab configuration was not copied correctly.");
            Assert.AreEqual(defaultConfig.Encoding.CodePage, clonedConfig.Encoding.CodePage, "Encoding configuration was not copied correctly.");
            Assert.AreEqual(defaultConfig.Formatting.Regions.EndRegionNameEnabled, clonedConfig.Formatting.Regions.EndRegionNameEnabled, "Regions configuration was not copied correctly.");
        }
Exemplo n.º 27
0
        /// <summary>
        /// Creates a new configuration using the specified filename and updates
        /// the UI.
        /// </summary>
        /// <param name="filename">The filename.</param>
        private void CreateConfiguration(string filename)
        {
            try
            {
                CodeConfiguration configuration = CodeConfiguration.Default.Clone() as CodeConfiguration;
                configuration.Save(filename);

                _configurationEditorControl.Configuration = configuration;
                this.CanSelectConfig = false;
            }
            catch
            {
                string message = string.Format(
                    CultureInfo.CurrentUICulture,
                    "Unable to create configuration file {0}.",
                    filename);

                MessageBox.Show(this, message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Exemplo n.º 28
0
        static async Task Main(string[] args)
        {
            // Done:
            // * Rocks (https://github.com/jasonbock/rocks)
            // * Autofac (https://github.com/autofac/Autofac)
            // * AutoMapper (https://github.com/AutoMapper/AutoMapper)
            // * CSLA (https://github.com/marimerLLC/csla)
            // * Moq (https://github.com/moq/moq)
            // * Json.Net (https://github.com/JamesNK/Newtonsoft.Json)
            // * AngleSharp (https://github.com/AngleSharp/AngleSharp)
            // * NLog (https://github.com/NLog/NLog/)
            // * NodaTime (https://github.com/nodatime/nodatime)
            var configuration = new CodeConfiguration(
                new CodeItemConfiguration("NodaTime", @"src\NodaTime-All.sln"));

            var reformatters = new Reformatter[configuration.Items.Length];

            for (var i = 0; i < configuration.Items.Length; i++)
            {
                reformatters[i] = new Reformatter(configuration.Items[i]);
                await reformatters[i].ReformatAsync();
            }

            var performance = new PerformanceRunner(reformatters);
            await performance.RunAsync();

            foreach (var reformatter in reformatters)
            {
                var size = new SolutionComparisonSize(
                    (Path.Combine(reformatter.Directories.tabDirectory, reformatter.Configuration.SolutionFile),
                     Path.Combine(reformatter.Directories.spaceDirectory, reformatter.Configuration.SolutionFile)));
                Console.Out.WriteLine(
                    $"Sizes for {reformatter.Configuration.SourceFolder}, tab is {size.TabSize}, space is {size.SpaceSize}, difference is {size.TabSmallerBy}");
            }

            //var size = new SolutionComparisonSize(
            //	(@"M:\TVSSource\Repos\Newtonsoft.Json\tab\Src\Newtonsoft.Json.sln",
            //	@"M:\TVSSource\Repos\Newtonsoft.Json\space\Src\Newtonsoft.Json.sln"));
            //Console.Out.WriteLine(
            //	$"Sizes for Newtonsoft.Json, tab is {size.TabSize}, space is {size.SpaceSize}, difference is {size.TabSmallerBy}");
        }
Exemplo n.º 29
0
        public virtual void TabStyleUnknownTest()
        {
            TypeElement classElement = new TypeElement();

            classElement.Name   = "TestClass";
            classElement.Type   = TypeElementType.Class;
            classElement.Access = CodeAccess.Public;

            MethodElement methodElement = new MethodElement();

            methodElement.Name   = "DoSomething";
            methodElement.Access = CodeAccess.Public;
            methodElement.Type   = "Object";

            classElement.AddChild(methodElement);

            List <ICodeElement> codeElements = new List <ICodeElement>();

            StringWriter writer;

            codeElements.Add(classElement);

            CodeConfiguration configuration = new CodeConfiguration();
            TCodeWriter       codeWriter    = new TCodeWriter();

            codeWriter.Configuration = configuration;

            //
            // Unknown tab style
            //
            configuration.Formatting.Tabs.SpacesPerTab = 4;
            configuration.Formatting.Tabs.TabStyle     = (TabStyle)int.MinValue;

            writer = new StringWriter();
            Assert.Throws <InvalidOperationException>(
                delegate
            {
                codeWriter.Write(codeElements.AsReadOnly(), writer);
            });
        }
Exemplo n.º 30
0
        public void ArrangeProjectFilteredTest()
        {
            CodeConfiguration filterProjectConfig = CodeConfiguration.Default.Clone() as CodeConfiguration;

            // Set up the filter
            FilterBy filter = new FilterBy();

            filter.Condition = "!($(File.Path) : '.Filtered.')";
            ((ProjectHandlerConfiguration)filterProjectConfig.Handlers[0]).ProjectExtensions[0].FilterBy = filter;

            string filterProjectConfigFile = Path.Combine(Path.GetTempPath(), "FilterProjectConfig.xml");

            try
            {
                filterProjectConfig.Save(filterProjectConfigFile);

                TestLogger   logger       = new TestLogger();
                FileArranger fileArranger = new FileArranger(filterProjectConfigFile, logger);

                bool success = fileArranger.Arrange(_testFilteredProjectFile, null);

                Assert.IsTrue(success, "Expected file to be arranged succesfully.");
                Assert.IsTrue(
                    logger.HasMessage(LogLevel.Verbose, "0 files written."),
                    "Expected 0 files to be written - " + logger.ToString());
            }
            finally
            {
                try
                {
                    File.Delete(filterProjectConfigFile);
                }
                catch
                {
                }
            }
        }
Exemplo n.º 31
0
        /// <summary>
        /// Loads the configuration file into the editor and updates the UI state.
        /// </summary>
        /// <param name="filename">The filename.</param>
        private void LoadConfiguration(string filename)
        {
            if (filename.Length == 0)
            {
                MessageBox.Show(this, "Please select a configuration to load.", this.Text);
            }
            else
            {
                try
                {
                    CodeConfiguration configuration = CodeConfiguration.Load(filename, false);
                    _configurationEditorControl.Configuration = configuration;
                    this.CanSelectConfig = false;
                }
                catch (Exception ex)
                {
                    StringBuilder messageBuilder = new StringBuilder(
                        string.Format(
                            CultureInfo.CurrentUICulture,
                            "Unable to load configuration file {0}: {1}",
                            filename,
                            ex.Message));
                    if (ex.InnerException != null)
                    {
                        messageBuilder.AppendFormat(" {0}", ex.InnerException.Message);
                    }

                    MessageBox.Show(
                        this,
                        messageBuilder.ToString(),
                        this.Text,
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Error);
                }
            }
        }
Exemplo n.º 32
0
        public void UpgradeProjectExtensionsTest()
        {
            string filename = Path.GetTempFileName();

            try
            {
                CodeConfiguration oldConfiguration = new CodeConfiguration();

                SourceHandlerConfiguration sourceHandler = new SourceHandlerConfiguration();
                ExtensionConfiguration     oldExtension  = new ExtensionConfiguration();
                oldExtension.Name = "csproj";
                sourceHandler.ProjectExtensions.Add(oldExtension);
                oldConfiguration.Handlers.Add(sourceHandler);
                oldConfiguration.Save(filename);

                CodeConfiguration newConfiguration = CodeConfiguration.Load(filename);
                Assert.AreEqual(2, newConfiguration.Handlers.Count, "New handler was not created.");
                ProjectHandlerConfiguration projectHandlerConfiguration =
                    newConfiguration.Handlers[0] as ProjectHandlerConfiguration;
                Assert.IsNotNull(projectHandlerConfiguration, "Expected a project handler config to be created.");
                Assert.IsNull(projectHandlerConfiguration.AssemblyName);
                Assert.AreEqual(typeof(MSBuildProjectParser).FullName, projectHandlerConfiguration.ParserType);
                Assert.AreEqual(1, projectHandlerConfiguration.ProjectExtensions.Count, "Unexpected number of project extensions.");
                Assert.AreEqual(oldExtension.Name, projectHandlerConfiguration.ProjectExtensions[0].Name);
            }
            finally
            {
                try
                {
                    File.Delete(filename);
                }
                catch
                {
                }
            }
        }
        public async Task ExecuteFuncAsyncTest()
        {
            // Arrange
            var config = new CodeConfiguration();

            // Act
            var result = await config.ExecuteAsync(async () =>
            {
                await Task.Yield();
                return true;
            });

            // Assert
            Assert.True(result);
        }
Exemplo n.º 34
0
        public void MoveUsingsToFileTest()
        {
            List <ICodeElement> codeElements = new List <ICodeElement>();

            UsingElement using1 = new UsingElement();

            using1.Name      = "System";
            using1.IsMovable = true;

            codeElements.Add(using1);

            NamespaceElement namespaceElement = new NamespaceElement();

            namespaceElement.Name = "TestNamespace";
            codeElements.Add(namespaceElement);

            // Nested region and groups
            RegionElement region = new RegionElement();

            region.Name = "Region";
            namespaceElement.AddChild(region);
            GroupElement group = new GroupElement();

            group.Name = "Group";
            region.AddChild(group);

            UsingElement using2 = new UsingElement();

            using2.Name      = "System.IO";
            using2.IsMovable = true;

            group.AddChild(using2);

            UsingElement using3 = new UsingElement();

            using3.Name      = "System.Collections";
            using3.IsMovable = true;
            namespaceElement.AddChild(using3);

            TypeElement class1 = new TypeElement();

            class1.Name = "Class1";
            namespaceElement.AddChild(class1);

            TypeElement class2 = new TypeElement();

            class2.Name = "Class2";
            namespaceElement.AddChild(class2);

            CodeConfiguration configuration = CodeConfiguration.Default.Clone() as CodeConfiguration;
            CodeArranger      arranger;

            //
            // Move to file.
            //
            configuration.Formatting.Usings.MoveTo = CodeLevel.File;
            arranger = new CodeArranger(configuration);
            ReadOnlyCollection <ICodeElement> arranged = arranger.Arrange(codeElements.AsReadOnly());

            Assert.AreEqual(2, arranged.Count, "After arranging, an unexpected number of elements were returned.");

            GroupElement fileGroup = arranged[0] as GroupElement;

            Assert.IsNotNull(fileGroup);
            GroupElement innerGroup = fileGroup.Children[0] as GroupElement;

            Assert.AreEqual("System", innerGroup.Children[0].Name);
            Assert.AreEqual("System.Collections", innerGroup.Children[1].Name);
            Assert.AreEqual("System.IO", innerGroup.Children[2].Name);

            NamespaceElement namespaceElementTest = arranged[1] as NamespaceElement;

            Assert.IsNotNull(namespaceElementTest, "Expected a namespace element.");
            Assert.AreEqual(2, namespaceElementTest.Children.Count,
                            "After arranging, an unexpected number of namespace elements were returned.");

            RegionElement typeRegion = namespaceElementTest.Children[1] as RegionElement;

            Assert.IsNotNull(typeRegion);
            Assert.AreEqual("Class1", typeRegion.Children[0].Name);
            Assert.AreEqual("Class2", typeRegion.Children[1].Name);
        }
Exemplo n.º 35
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.");
        }
        public async Task ExecuteFuncTest()
        {
            // Arrange
            var config = new CodeConfiguration();

            // Act
            var result = await config.ExecuteAsync(() => true);

            // Assert
            Assert.True(result);
        }
Exemplo n.º 37
0
        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 async Task ExecuteFuncAsyncRetryTest()
        {
            // Arrange
            var config = new CodeConfiguration();
            var num = 0;
            // Act
            var actual = await config.ExecuteAsync(async () =>
            {
                await Task.Yield();
                num++;
                throw new Exception();
            }, async e =>
            {
                await Task.Yield();
                num++;
                return num;
            });

            // Assert
            Assert.AreEqual(4, actual);
        }