コード例 #1
0
        public void Execute_MultipleCustomDirectives_RemovesDirectiveNodesFromDocument()
        {
            // Arrange
            var content        = "@custom \"Hello\"" + Environment.NewLine + "@custom \"World\"";
            var sourceDocument = TestRazorSourceDocument.Create(content);
            var codeDocument   = RazorCodeDocument.Create(sourceDocument);
            var defaultEngine  = RazorEngine.Create(b =>
            {
                b.AddDirective(DirectiveDescriptor.CreateDirective("custom", DirectiveKind.SingleLine, d => d.AddStringToken()));
            });
            var documentNode = Lower(codeDocument, defaultEngine);
            var pass         = new DirectiveRemovalOptimizationPass()
            {
                Engine = defaultEngine,
            };

            // Act
            pass.Execute(codeDocument, documentNode);

            // Assert
            Children(documentNode,
                     node => Assert.IsType <NamespaceDeclarationIntermediateNode>(node));
            var @namespace = documentNode.Children[0];

            Children(@namespace,
                     node => Assert.IsType <ClassDeclarationIntermediateNode>(node));
            var @class = @namespace.Children[0];
            var method = SingleChild <MethodDeclarationIntermediateNode>(@class);

            Assert.Empty(method.Children);
        }
コード例 #2
0
        public void Execute_DirectiveWithError_PreservesDiagnosticsAndRemovesDirectiveNodeFromDocument()
        {
            // Arrange
            var content            = "@custom \"Hello\"";
            var expectedDiagnostic = RazorDiagnostic.Create(new RazorDiagnosticDescriptor("RZ9999", () => "Some diagnostic message.", RazorDiagnosticSeverity.Error), SourceSpan.Undefined);
            var sourceDocument     = TestRazorSourceDocument.Create(content);
            var codeDocument       = RazorCodeDocument.Create(sourceDocument);
            var defaultEngine      = RazorProjectEngine.Create(b =>
            {
                b.AddDirective(DirectiveDescriptor.CreateDirective("custom", DirectiveKind.SingleLine, d => d.AddStringToken()));
            }).Engine;
            var documentNode = Lower(codeDocument, defaultEngine);

            // Add the diagnostic to the directive node.
            var directiveNode = documentNode.FindDescendantNodes <DirectiveIntermediateNode>().Single();

            directiveNode.Diagnostics.Add(expectedDiagnostic);

            var pass = new DirectiveRemovalOptimizationPass()
            {
                Engine = defaultEngine,
            };

            // Act
            pass.Execute(codeDocument, documentNode);

            // Assert
            var diagnostic = Assert.Single(documentNode.Diagnostics);

            Assert.Equal(expectedDiagnostic, diagnostic);

            Children(documentNode,
                     node => Assert.IsType <NamespaceDeclarationIntermediateNode>(node));
            var @namespace = documentNode.Children[0];

            Children(@namespace,
                     node => Assert.IsType <ClassDeclarationIntermediateNode>(node));
            var @class = @namespace.Children[0];
            var method = SingleChild <MethodDeclarationIntermediateNode>(@class);

            Assert.Empty(method.Children);
        }