public void CodeText_WhenAllFormattingsRun_OutputsExpectedCodeText(string inputString, string fileName, string expectedOutput)
        {
            TestHelper.InitializeConfig(TestData.TestConfig);
            CSharpSyntaxNode root = TestHelper.CreateCSharpSyntaxRoot(inputString);

            var        regionRemover = new RegionRemover();
            SyntaxNode regionRoot    = regionRemover.Visit(root);

            var        usingsHelper = new UsingsPlacer();
            SyntaxNode usingsRoot   = usingsHelper.ProcessUsings(regionRoot);

            var        fhf     = new FileHeaderFormatter();
            SyntaxNode fhfRoot = fhf.AddHeader(usingsRoot, fileName);

            var        sorter     = new CSharpSorter();
            SyntaxNode sorterRoot = sorter.Visit(fhfRoot);

            var        newLineFormatter = new NewlineFormatter();
            SyntaxNode newLineRoot      = newLineFormatter.Visit(sorterRoot);

            var        ebf     = new ExpressionBodiedFormatter();
            SyntaxNode ebfRoot = ebf.Visit(newLineRoot);

            string actualOutput = ebfRoot.ToFullString();

            Debug.WriteLine(actualOutput);
            Assert.That(actualOutput, Is.EqualTo(expectedOutput));
        }
Exemple #2
0
        private async Task FormatAndSortCodeAsync()
        {
            try
            {
                Document activeDocument = await VisualStudioHelper.GetActiveDocumentAsync();

                if (VisualStudioHelper.FileIsExcludedType(activeDocument.FilePath))
                {
                    return;
                }

                SyntaxNode root = await VisualStudioHelper.GetDocumentRootAsync(activeDocument);

                bool isCSharpDocument = root.Language == VisualStudioHelper.CSharpLanguageName;
                if (!isCSharpDocument)
                {
                    return;
                }

                var        regionRemover = new RegionRemover();
                SyntaxNode regionRoot    = regionRemover.Visit(root);

                var        usingsPlacer = new UsingsPlacer();
                SyntaxNode usingsRoot   = usingsPlacer.ProcessUsings(regionRoot);

                string     fileName = Path.GetFileName(activeDocument.FilePath);
                var        fhf      = new FileHeaderFormatter();
                SyntaxNode fhfRoot  = fhf.AddHeader(usingsRoot, fileName);

                var        sorter     = new CSharpSorter();
                SyntaxNode sorterRoot = sorter.Visit(fhfRoot);

                var        accessModifier = new AccessLevelModifierFormatter();
                SyntaxNode accessRoot     = accessModifier.Visit(sorterRoot);

                var        newLineFormatter = new NewlineFormatter();
                SyntaxNode newLineRoot      = newLineFormatter.Visit(accessRoot);

                var        ebf     = new ExpressionBodiedFormatter();
                SyntaxNode ebfRoot = ebf.Visit(newLineRoot);

                Document newDocument = activeDocument.WithSyntaxRoot(ebfRoot);
                bool     success     = await VisualStudioHelper.ApplyDocumentChangesAsync(newDocument);

                await VisualStudioHelper.InvokeCommandAsync(VisualStudioHelper.RunDefaultCodeCleanup);

                await VisualStudioHelper.InvokeCommandAsync(VisualStudioHelper.FormatDocumentCommandName);
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
            }
        }
Exemple #3
0
        public void CodeText_WhenSorterIsRun_OutputsSortedCodeText(string inputString, string expectedOutput)
        {
            TestHelper.InitializeConfig(TestData.TestConfig);
            CSharpSyntaxNode root = TestHelper.CreateCSharpSyntaxRoot(inputString);

            var        sh     = new CSharpSorter();
            SyntaxNode output = sh.Visit(root);

            string actualOutput = output.ToFullString();

            Debug.WriteLine(actualOutput);
            Assert.That(actualOutput, Is.EqualTo(expectedOutput));
        }