public void Execute_ErrorsForRazorBlockFileScopedSinglyOccurringDirectives()
    {
        // Arrange
        var directive = DirectiveDescriptor.CreateRazorBlockDirective("custom", b => b.Usage = DirectiveUsage.FileScopedSinglyOccurring);
        var phase     = new DefaultRazorIntermediateNodeLoweringPhase();
        var engine    = RazorProjectEngine.CreateEmpty(b =>
        {
            b.Phases.Add(phase);
            b.Features.Add(new DefaultRazorCodeGenerationOptionsFeature(designTime: false));
            b.AddDirective(directive);
        });
        var options      = RazorParserOptions.Create(builder => builder.Directives.Add(directive));
        var importSource = TestRazorSourceDocument.Create("@custom { }", filePath: "import.cshtml");
        var codeDocument = TestRazorCodeDocument.Create("<p>NonDirective</p>");

        codeDocument.SetSyntaxTree(RazorSyntaxTree.Parse(codeDocument.Source, options));
        codeDocument.SetImportSyntaxTrees(new[] { RazorSyntaxTree.Parse(importSource, options) });
        var expectedDiagnostic = RazorDiagnosticFactory.CreateDirective_BlockDirectiveCannotBeImported("custom");

        // Act
        phase.Execute(codeDocument);

        // Assert
        var documentNode = codeDocument.GetDocumentIntermediateNode();
        var directives   = documentNode.Children.OfType <DirectiveIntermediateNode>();

        Assert.Empty(directives);
        var diagnostic = Assert.Single(documentNode.GetAllDiagnostics());

        Assert.Equal(expectedDiagnostic, diagnostic);
    }
Example #2
0
        public void Convert_Converts()
        {
            // Arrange
            var razorDiagnostic = RazorDiagnosticFactory.CreateDirective_BlockDirectiveCannotBeImported("test");
            var sourceText      = SourceText.From(string.Empty);

            // Act
            var diagnostic = RazorDiagnosticConverter.Convert(razorDiagnostic, sourceText);

            // Assert
            Assert.Equal(razorDiagnostic.Id, diagnostic.Code);
            Assert.Equal(razorDiagnostic.GetMessage(CultureInfo.InvariantCulture), diagnostic.Message);
            Assert.Null(diagnostic.Range);
            Assert.Equal(DiagnosticSeverity.Error, diagnostic.Severity);
        }