public void Execute_NoOpsForBlazorComponents()
    {
        // Arrange
        var properties   = new RazorSourceDocumentProperties(filePath: "ignored", relativePath: "Test.razor");
        var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create("Hello world", properties));

        codeDocument.SetFileKind(FileKinds.Component);

        var engine = CreateProjectEngine(b =>
        {
            PageDirective.Register(b);
        }).Engine;
        var irDocument = CreateIRDocument(engine, codeDocument);
        var pass       = new CreateNewOnMetadataUpdateAttributePass
        {
            Engine = engine
        };
        var documentClassifier = new DefaultDocumentClassifierPass {
            Engine = engine
        };

        // Act
        documentClassifier.Execute(codeDocument, irDocument);
        pass.Execute(codeDocument, irDocument);
        var visitor = new Visitor();

        visitor.Visit(irDocument);

        // Assert
        Assert.Empty(visitor.ExtensionNodes);
    }
Ejemplo n.º 2
0
 private RazorEngine CreateEngine()
 {
     return(RazorProjectEngine.Create(b =>
     {
         PageDirective.Register(b);
     }).Engine);
 }
 private RazorEngine CreateRuntimeEngine()
 {
     return(CreateProjectEngine(b =>
     {
         PageDirective.Register(b);
     }).Engine);
 }
Ejemplo n.º 4
0
        private RazorEngine GetRazorEngine()
        {
            var razorEngine = RazorEngine.Create(builder =>
            {
                InjectDirective.Register(builder);
                ModelDirective.Register(builder);
                NamespaceDirective.Register(builder);
                PageDirective.Register(builder);
                FunctionsDirective.Register(builder);
                InheritsDirective.Register(builder);
                SectionDirective.Register(builder);

                ////builder.AddTargetExtension(new TemplateTargetExtension()
                ////{
                ////    TemplateTypeName = "global::Microsoft.AspNetCore.Mvc.Razor.HelperResult",
                ////});

                ////builder.Features.Add(new SuppressChecksumOptionsFeature());
                builder.Features.Add(new ModelExpressionPass());
                builder.Features.Add(new PagesPropertyInjectionPass());
                builder.Features.Add(new ViewComponentTagHelperPass());
                builder.Features.Add(new RazorPageDocumentClassifierPass());
                builder.Features.Add(new MvcViewDocumentClassifierPass2(RuntimeTemplatesNamespace, BaseViewName));
                builder.Features.Add(new AssemblyAttributeInjectionPass2());

                if (!builder.DesignTime)
                {
                    //builder.Features.Add(new InstrumentationPass());
                }
            });

            return(razorEngine);
        }
Ejemplo n.º 5
0
        private static RazorCodeDocument CreateCodeDocument(string text)
        {
            var projectItem = new TestRazorProjectItem("c:/Test.razor", "c:/Test.razor", "Test.razor")
            {
                Content = text
            };
            var projectEngine = RazorProjectEngine.Create(RazorConfiguration.Default, TestRazorProjectFileSystem.Empty, (builder) =>
            {
                PageDirective.Register(builder);
            });
            var codeDocument = projectEngine.Process(projectItem);

            codeDocument.SetFileKind(FileKinds.Component);
            return(codeDocument);
        }
        public async Task Handle_AddOneUsingToPageDirective()
        {
            // Arrange
            var documentPath = "c:/Test.cshtml";
            var documentUri  = new Uri(documentPath);
            var contents     = $"@page{Environment.NewLine}@model IndexModel";

            var projectItem = new TestRazorProjectItem("c:/Test.cshtml", "c:/Test.cshtml", "Test.cshtml")
            {
                Content = contents
            };
            var projectEngine = RazorProjectEngine.Create(RazorConfiguration.Default, TestRazorProjectFileSystem.Empty, (builder) =>
            {
                PageDirective.Register(builder);
                ModelDirective.Register(builder);
            });
            var codeDocument = projectEngine.Process(projectItem);

            codeDocument.SetFileKind(FileKinds.Legacy);

            var resolver     = new AddUsingsCodeActionResolver(LegacyDispatcher, CreateDocumentResolver(documentPath, codeDocument));
            var actionParams = new AddUsingsCodeActionParams
            {
                Uri       = documentUri,
                Namespace = "System"
            };
            var data = JObject.FromObject(actionParams);

            // Act
            var workspaceEdit = await resolver.ResolveAsync(data, default);

            // Assert
            Assert.NotNull(workspaceEdit);
            Assert.NotNull(workspaceEdit.DocumentChanges);
            Assert.Single(workspaceEdit.DocumentChanges);

            var documentChanges = workspaceEdit.DocumentChanges.ToArray();
            var addUsingsChange = documentChanges[0];

            Assert.True(addUsingsChange.IsTextDocumentEdit);
            var firstEdit = Assert.Single(addUsingsChange.TextDocumentEdit.Edits);

            Assert.Equal(1, firstEdit.Range.Start.Line);
            Assert.Equal($"@using System{Environment.NewLine}", firstEdit.NewText);
        }
    public void Execute_AddsAttributes()
    {
        // Arrange
        var properties   = new RazorSourceDocumentProperties(filePath: "ignored", relativePath: "Test.cshtml");
        var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create("Hello world", properties));

        var engine = CreateProjectEngine(b =>
        {
            PageDirective.Register(b);
        }).Engine;;
        var irDocument = CreateIRDocument(engine, codeDocument);
        var pass       = new CreateNewOnMetadataUpdateAttributePass
        {
            Engine = engine
        };
        var documentClassifier = new MvcViewDocumentClassifierPass {
            Engine = engine
        };

        // Act
        documentClassifier.Execute(codeDocument, irDocument);
        pass.Execute(codeDocument, irDocument);
        var visitor = new Visitor();

        visitor.Visit(irDocument);

        // Assert
        Assert.Collection(
            visitor.ExtensionNodes,
            node =>
        {
            var attributeNode = Assert.IsType <RazorCompiledItemMetadataAttributeIntermediateNode>(node);
            Assert.Equal("Identifier", attributeNode.Key);
            Assert.Equal("/Test.cshtml", attributeNode.Value);
        },
            node =>
        {
            Assert.IsType <CreateNewOnMetadataUpdateAttributePass.CreateNewOnMetadataUpdateAttributeIntermediateNode>(node);
        });
    }
Ejemplo n.º 8
0
    public static void Register(RazorProjectEngineBuilder builder)
    {
        if (builder == null)
        {
            throw new ArgumentNullException(nameof(builder));
        }

        InjectDirective.Register(builder);
        ModelDirective.Register(builder);
        PageDirective.Register(builder);

        SectionDirective.Register(builder);

        builder.Features.Add(new DefaultTagHelperDescriptorProvider());
        builder.Features.Add(new ViewComponentTagHelperDescriptorProvider());

        builder.AddTargetExtension(new ViewComponentTagHelperTargetExtension());
        builder.AddTargetExtension(new TemplateTargetExtension()
        {
            TemplateTypeName = "global::Microsoft.AspNetCore.Mvc.Razor.HelperResult",
        });

        builder.Features.Add(new ModelExpressionPass());
        builder.Features.Add(new PagesPropertyInjectionPass());
        builder.Features.Add(new ViewComponentTagHelperPass());

        builder.Features.Add(new RazorPageDocumentClassifierPass(builder.Configuration.UseConsolidatedMvcViews));
        builder.Features.Add(new MvcViewDocumentClassifierPass(builder.Configuration.UseConsolidatedMvcViews));

        builder.Features.Add(new MvcImportProjectFeature());

        // The default C# language version for what this Razor configuration supports.
        builder.SetCSharpLanguageVersion(LanguageVersion.CSharp8);

        if (builder.Configuration.LanguageVersion.CompareTo(RazorLanguageVersion.Version_6_0) >= 0)
        {
            builder.Features.Add(new CreateNewOnMetadataUpdateAttributePass());
        }
    }
Ejemplo n.º 9
0
    public static void Register(RazorProjectEngineBuilder builder)
    {
        if (builder == null)
        {
            throw new ArgumentNullException(nameof(builder));
        }

        FunctionsDirective.Register(builder);
        InjectDirective.Register(builder);
        ModelDirective.Register(builder);
        NamespaceDirective.Register(builder);
        PageDirective.Register(builder);

        InheritsDirective.Register(builder);
        SectionDirective.Register(builder);

        builder.Features.Add(new DefaultTagHelperDescriptorProvider());
        builder.Features.Add(new ViewComponentTagHelperDescriptorProvider());

        builder.AddTargetExtension(new ViewComponentTagHelperTargetExtension());
        builder.AddTargetExtension(new TemplateTargetExtension()
        {
            TemplateTypeName = "global::Microsoft.AspNetCore.Mvc.Razor.HelperResult",
        });

        builder.Features.Add(new ModelExpressionPass());
        builder.Features.Add(new PagesPropertyInjectionPass());
        builder.Features.Add(new ViewComponentTagHelperPass());
        builder.Features.Add(new RazorPageDocumentClassifierPass());
        builder.Features.Add(new MvcViewDocumentClassifierPass());
        builder.Features.Add(new AssemblyAttributeInjectionPass());
        builder.Features.Add(new InstrumentationPass());

        builder.Features.Add(new MvcImportProjectFeature());

        // The default C# language version for what this Razor configuration supports.
        builder.SetCSharpLanguageVersion(LanguageVersion.CSharp7_3);
    }
Ejemplo n.º 10
0
 protected override void ConfigureProjectEngine(RazorProjectEngineBuilder builder)
 {
     PageDirective.Register(builder);
 }