Esempio n. 1
0
    public void MvcViewDocumentClassifierPass_SetsDocumentKind()
    {
        // Arrange
        var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create("some-content", "Test.cshtml"));

        var projectEngine = CreateProjectEngine();
        var irDocument    = CreateIRDocument(projectEngine, codeDocument);
        var pass          = new MvcViewDocumentClassifierPass
        {
            Engine = projectEngine.Engine
        };

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

        // Assert
        Assert.Equal("mvc.1.0.view", irDocument.DocumentKind);
    }
Esempio n. 2
0
    public void MvcViewDocumentClassifierPass_NoOpsIfDocumentKindIsAlreadySet()
    {
        // Arrange
        var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create("some-content", "Test.cshtml"));

        var projectEngine = CreateProjectEngine();
        var irDocument    = CreateIRDocument(projectEngine, codeDocument);

        irDocument.DocumentKind = "some-value";
        var pass = new MvcViewDocumentClassifierPass
        {
            Engine = projectEngine.Engine
        };

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

        // Assert
        Assert.Equal("some-value", irDocument.DocumentKind);
    }
Esempio n. 3
0
    public void MvcViewDocumentClassifierPass_SetsNamespace()
    {
        // Arrange
        var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create("some-content", "Test.cshtml"));

        var projectEngine = CreateProjectEngine();
        var irDocument    = CreateIRDocument(projectEngine, codeDocument);
        var pass          = new MvcViewDocumentClassifierPass
        {
            Engine = projectEngine.Engine
        };

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

        visitor.Visit(irDocument);

        // Assert
        Assert.Equal("AspNetCore", visitor.Namespace.Content);
    }
    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);
        });
    }
Esempio n. 5
0
    public void MvcViewDocumentClassifierPass_SanitizesClassName()
    {
        // Arrange
        var properties   = new RazorSourceDocumentProperties(filePath: @"x:\Test.cshtml", relativePath: "path.with+invalid-chars");
        var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create("@page", properties));

        var projectEngine = CreateProjectEngine();
        var irDocument    = CreateIRDocument(projectEngine, codeDocument);
        var pass          = new MvcViewDocumentClassifierPass
        {
            Engine = projectEngine.Engine
        };

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

        visitor.Visit(irDocument);

        // Assert
        Assert.Equal("path_with_invalid_chars", visitor.Class.ClassName);
    }
Esempio n. 6
0
    public void MvcViewDocumentClassifierPass_UsesAbsolutePath_IfRelativePathIsNotSet()
    {
        // Arrange
        var properties   = new RazorSourceDocumentProperties(filePath: @"x::\application\Views\Home\Index.cshtml", relativePath: null);
        var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create("some-content", properties));

        var projectEngine = CreateProjectEngine();
        var irDocument    = CreateIRDocument(projectEngine, codeDocument);
        var pass          = new MvcViewDocumentClassifierPass
        {
            Engine = projectEngine.Engine
        };

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

        visitor.Visit(irDocument);

        // Assert
        Assert.Equal("x___application_Views_Home_Index", visitor.Class.ClassName);
    }
Esempio n. 7
0
    public void MvcViewDocumentClassifierPass_UsesRelativePathToGenerateTypeName(string relativePath, string expected)
    {
        // Arrange
        var properties   = new RazorSourceDocumentProperties(filePath: "ignored", relativePath: relativePath);
        var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create("some-content", properties));

        var projectEngine = CreateProjectEngine();
        var irDocument    = CreateIRDocument(projectEngine, codeDocument);
        var pass          = new MvcViewDocumentClassifierPass
        {
            Engine = projectEngine.Engine
        };

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

        visitor.Visit(irDocument);

        // Assert
        Assert.Equal(expected, visitor.Class.ClassName);
    }
Esempio n. 8
0
    public void MvcViewDocumentClassifierPass_SetsUpExecuteAsyncMethod()
    {
        // Arrange
        var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create("some-content", "Test.cshtml"));

        var projectEngine = CreateProjectEngine();
        var irDocument    = CreateIRDocument(projectEngine, codeDocument);
        var pass          = new MvcViewDocumentClassifierPass
        {
            Engine = projectEngine.Engine
        };

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

        visitor.Visit(irDocument);

        // Assert
        Assert.Equal("ExecuteAsync", visitor.Method.MethodName);
        Assert.Equal("global::System.Threading.Tasks.Task", visitor.Method.ReturnType);
        Assert.Equal(new[] { "public", "async", "override" }, visitor.Method.Modifiers);
    }
Esempio n. 9
0
    public void MvcViewDocumentClassifierPass_SetsClass()
    {
        // Arrange
        var properties   = new RazorSourceDocumentProperties(filePath: "ignored", relativePath: "Test.cshtml");
        var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create("some-content", properties));

        var projectEngine = CreateProjectEngine();
        var irDocument    = CreateIRDocument(projectEngine, codeDocument);
        var pass          = new MvcViewDocumentClassifierPass
        {
            Engine = projectEngine.Engine
        };

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

        visitor.Visit(irDocument);

        // Assert
        Assert.Equal("global::Microsoft.AspNetCore.Mvc.Razor.RazorPage<TModel>", visitor.Class.BaseType);
        Assert.Equal(new[] { "public" }, visitor.Class.Modifiers);
        Assert.Equal("Test", visitor.Class.ClassName);
    }
Esempio n. 10
0
    public void MvcViewDocumentClassifierPass_NullFilePath_SetsClass()
    {
        // Arrange
        var properties   = new RazorSourceDocumentProperties(filePath: null, relativePath: null);
        var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create("some-content", properties));

        var projectEngine = CreateProjectEngine();
        var irDocument    = CreateIRDocument(projectEngine, codeDocument);
        var pass          = new MvcViewDocumentClassifierPass
        {
            Engine = projectEngine.Engine
        };

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

        visitor.Visit(irDocument);

        // Assert
        Assert.Equal("global::Microsoft.AspNetCore.Mvc.Razor.RazorPage<TModel>", visitor.Class.BaseType);
        Assert.Equal(new[] { "public" }, visitor.Class.Modifiers);
        Assert.Equal("AspNetCore_d9f877a857a7e9928eac04d09a59f25967624155", visitor.Class.ClassName);
    }