Esempio n. 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),
                                                                            fileSystem: new InMemoryFileSystem(includes ?? new Dictionary <string, string>()));

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

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

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

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

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

            ShaderTestUtility.CheckForParseErrors(syntaxTree);

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

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

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

            ShaderTestUtility.CheckForParseErrors(syntaxTree2);

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

            ShaderTestUtility.CheckForParseErrors(syntaxTree3);

            Assert.That(formattedCode2, Is.EqualTo(formattedCode1));
        }
Esempio n. 4
0
        public void CanGetSemanticModel(string testFile)
        {
            var sourceCode = File.ReadAllText(testFile);

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

            ShaderTestUtility.CheckForParseErrors(syntaxTree);

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

            Assert.NotNull(semanticModel);

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

            Assert.Equal(0, semanticModel.GetDiagnostics().Count(x => x.Severity == DiagnosticSeverity.Error));
        }
Esempio n. 5
0
 public static IEnumerable <object> GetTestShaders()
 {
     return(ShaderTestUtility.FindTestShaders(@"Hlsl\Shaders"));
 }