Пример #1
0
        private static void AssertFormat(string unformattedText, string expectedNewText,
                                         TextSpan?textSpan = null, FormattingOptions options = null,
                                         Dictionary <string, string> includes = null,
                                         bool allowSyntaxErrors = false)
        {
            Func <string, SyntaxTree> parse = code =>
                                              SyntaxFactory.ParseSyntaxTree(SourceText.From(code, "__RootFile__.hlsl"),
                                                                            fileSystem: new InMemoryFileSystem(includes ?? new Dictionary <string, string>()));

            // Arrange.
            var syntaxTree = parse(unformattedText);

            if (textSpan == null)
            {
                textSpan = new TextSpan(0, unformattedText.Length);
            }
            if (options == null)
            {
                options = new FormattingOptions();
            }

            // Act.
            var edits         = Formatter.GetEdits(syntaxTree, (SyntaxNode)syntaxTree.Root, textSpan.Value, options);
            var formattedCode = Formatter.ApplyEdits(unformattedText, edits);

            // Assert.
            if (!allowSyntaxErrors)
            {
                SyntaxTreeUtility.CheckForParseErrors(parse(formattedCode));
            }
            Assert.Equal(expectedNewText, formattedCode);
        }
        public void CanBuildSyntaxTree(string testFile)
        {
            var sourceCode = File.ReadAllText(testFile);

            // Build syntax tree.
            var syntaxTree = SyntaxFactory.ParseSyntaxTree(SourceText.From(sourceCode, testFile), fileSystem: new TestFileSystem());

            SyntaxTreeUtility.CheckForParseErrors(syntaxTree);

            // Check roundtripping.
            var roundtrippedText = syntaxTree.Root.ToFullString();

            Assert.Equal(sourceCode, roundtrippedText);
        }
        public void CanFormatAndReformat(string testFile)
        {
            // Arrange.
            var sourceCode  = File.ReadAllText(testFile);
            var syntaxTree1 = SyntaxFactory.ParseSyntaxTree(new SourceFile(SourceText.From(sourceCode), testFile), fileSystem: new TestFileSystem());

            // Format.
            var formattedCode1 = FormatCode(sourceCode, syntaxTree1);
            var syntaxTree2    = SyntaxFactory.ParseSyntaxTree(new SourceFile(SourceText.From(formattedCode1), testFile), fileSystem: new TestFileSystem());

            SyntaxTreeUtility.CheckForParseErrors(syntaxTree2);

            // Format again.
            var formattedCode2 = FormatCode(formattedCode1, syntaxTree2);
            var syntaxTree3    = SyntaxFactory.ParseSyntaxTree(new SourceFile(SourceText.From(formattedCode2), testFile), fileSystem: new TestFileSystem());

            SyntaxTreeUtility.CheckForParseErrors(syntaxTree3);

            Assert.Equal(formattedCode1, formattedCode2);
        }
        public void CanGetSemanticModel(string testFile)
        {
            var sourceCode = File.ReadAllText(testFile);

            // Build syntax tree.
            var syntaxTree = SyntaxFactory.ParseSyntaxTree(new SourceFile(SourceText.From(sourceCode), testFile), fileSystem: new TestFileSystem());

            SyntaxTreeUtility.CheckForParseErrors(syntaxTree);

            // Get semantic model.
            var compilation   = new CodeAnalysis.Hlsl.Compilation.Compilation(syntaxTree);
            var semanticModel = compilation.GetSemanticModel();

            Assert.NotNull(semanticModel);

            foreach (var diagnostic in semanticModel.GetDiagnostics())
            {
                _output.WriteLine(diagnostic.ToString());
            }

            Assert.Equal(0, semanticModel.GetDiagnostics().Count(x => x.Severity == DiagnosticSeverity.Error));
        }