public void RazorPageDocumentClassifierPass_SetsNamespace()
        {
            // Arrange
            var codeDocument = CreateDocument("@page");
            var engine       = CreateEngine();
            var irDocument   = CreateIRDocument(engine, codeDocument);
            var pass         = new RazorPageDocumentClassifierPass
            {
                Engine = engine
            };

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

            visitor.Visit(irDocument);

            // Assert
            Assert.Equal("AspNetCore", visitor.Namespace.Content);
        }
        public void RazorPageDocumentClassifierPass_NoOpsIfPageDirectiveIsMalformed()
        {
            // Arrange
            var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create("@page+1", "Test.cshtml"));

            var engine     = CreateEngine();
            var irDocument = CreateIRDocument(engine, codeDocument);

            irDocument.DocumentKind = "some-value";
            var pass = new RazorPageDocumentClassifierPass
            {
                Engine = engine
            };

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

            // Assert
            Assert.Equal("some-value", irDocument.DocumentKind);
        }
Пример #3
0
        public void RazorPageDocumentClassifierPass_SanitizesClassName()
        {
            // Arrange
            var properties   = new RazorSourceDocumentProperties(filePath: @"x:\Test.cshtml", relativePath: "path.with+invalid-chars");
            var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create("@page", properties));

            var engine     = CreateRuntimeEngine();
            var irDocument = CreateIRDocument(engine, codeDocument);
            var pass       = new RazorPageDocumentClassifierPass
            {
                Engine = engine
            };

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

            visitor.Visit(irDocument);

            // Assert
            Assert.Equal("path_with_invalid_chars", visitor.Class.ClassName);
        }
Пример #4
0
        public void RazorPageDocumentClassifierPass_UsesAbsolutePath_IfRelativePathIsNotSet()
        {
            // Arrange
            var properties   = new RazorSourceDocumentProperties(filePath: @"x::\application\Views\Home\Index.cshtml", relativePath: null);
            var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create("@page", properties));

            var engine     = CreateRuntimeEngine();
            var irDocument = CreateIRDocument(engine, codeDocument);
            var pass       = new RazorPageDocumentClassifierPass
            {
                Engine = engine
            };

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

            visitor.Visit(irDocument);

            // Assert
            Assert.Equal("x___application_Views_Home_Index", visitor.Class.ClassName);
        }
Пример #5
0
        public void RazorPageDocumentClassifierPass_UsesRelativePathToGenerateTypeName(string relativePath, string expected)
        {
            // Arrange
            var properties   = new RazorSourceDocumentProperties(filePath: "ignored", relativePath: relativePath);
            var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create("@page", properties));

            var engine     = CreateRuntimeEngine();
            var irDocument = CreateIRDocument(engine, codeDocument);
            var pass       = new RazorPageDocumentClassifierPass
            {
                Engine = engine
            };

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

            visitor.Visit(irDocument);

            // Assert
            Assert.Equal(expected, visitor.Class.ClassName);
        }
Пример #6
0
        public void RazorPageDocumentClassifierPass_SetsUpExecuteAsyncMethod()
        {
            // Arrange
            var codeDocument = CreateDocument("@page");
            var engine       = CreateEngine();
            var irDocument   = CreateIRDocument(engine, codeDocument);
            var pass         = new RazorPageDocumentClassifierPass
            {
                Engine = 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);
        }
Пример #7
0
        public void RazorPageDocumentClassifierPass_UsesAbsolutePath_IfRelativePathIsNotSet()
        {
            // Arrange
            var expected     = "x___application_Views_Home_Index_cshtml";
            var path         = @"x::\application\Views\Home\Index.cshtml";
            var codeDocument = CreateDocument("@page", path);
            var engine       = CreateEngine();
            var irDocument   = CreateIRDocument(engine, codeDocument);
            var pass         = new RazorPageDocumentClassifierPass
            {
                Engine = engine
            };

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

            visitor.Visit(irDocument);

            // Assert
            Assert.Equal(expected, visitor.Class.ClassName);
        }
Пример #8
0
        public void RazorPageDocumentClassifierPass_UsesRelativePathToGenerateTypeName(string relativePath, string expected)
        {
            // Arrange
            var codeDocument = CreateDocument("@page");

            codeDocument.SetRelativePath(relativePath);
            var engine     = CreateEngine();
            var irDocument = CreateIRDocument(engine, codeDocument);
            var pass       = new RazorPageDocumentClassifierPass
            {
                Engine = engine
            };

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

            visitor.Visit(irDocument);

            // Assert
            Assert.Equal(expected, visitor.Class.ClassName);
        }
Пример #9
0
        public void RazorPageDocumentClassifierPass_SanitizesClassName()
        {
            // Arrange
            var expected     = "path_with_invalid_chars";
            var codeDocument = CreateDocument("@page");

            codeDocument.SetRelativePath("path.with+invalid-chars");
            var engine     = CreateEngine();
            var irDocument = CreateIRDocument(engine, codeDocument);
            var pass       = new RazorPageDocumentClassifierPass
            {
                Engine = engine
            };

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

            visitor.Visit(irDocument);

            // Assert
            Assert.Equal(expected, visitor.Class.ClassName);
        }
Пример #10
0
        public void RazorPageDocumentClassifierPass_LogsErrorIfDirectiveNotAtTopOfFile()
        {
            // Arrange
            var sourceSpan = new SourceSpan(
                "Test.cshtml",
                absoluteIndex: 14 + Environment.NewLine.Length * 2,
                lineIndex: 2,
                characterIndex: 0,
                length: 5 + Environment.NewLine.Length);

            var expectedDiagnostic = RazorExtensionsDiagnosticFactory.CreatePageDirective_MustExistAtTheTopOfFile(sourceSpan);
            var content            = @"
@somethingelse
@page
";
            var codeDocument       = RazorCodeDocument.Create(RazorSourceDocument.Create(content, "Test.cshtml"));

            var engine     = CreateRuntimeEngine();
            var irDocument = CreateIRDocument(engine, codeDocument);
            var pass       = new RazorPageDocumentClassifierPass
            {
                Engine = engine
            };

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

            visitor.Visit(irDocument);

            // Assert
            var pageDirectives = irDocument.FindDirectiveReferences(PageDirective.Directive);
            var directive      = Assert.Single(pageDirectives);
            var diagnostic     = Assert.Single(directive.Node.Diagnostics);

            Assert.Equal(expectedDiagnostic, diagnostic);
        }
Пример #11
0
        public void RazorPageDocumentClassifierPass_NullFilePath_SetsClass()
        {
            // Arrange
            var properties   = new RazorSourceDocumentProperties(filePath: null, relativePath: null);
            var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create("@page", properties));

            var engine     = CreateRuntimeEngine();
            var irDocument = CreateIRDocument(engine, codeDocument);
            var pass       = new RazorPageDocumentClassifierPass
            {
                Engine = engine
            };

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

            visitor.Visit(irDocument);

            // Assert
            Assert.Equal("global::Microsoft.AspNetCore.Mvc.RazorPages.Page", visitor.Class.BaseType);
            Assert.Equal(new[] { "public" }, visitor.Class.Modifiers);
            Assert.Equal("AspNetCore_74fbaab062bb228ed1ab09c5ff8d6ed2417320e2", visitor.Class.ClassName);
        }
Пример #12
0
        public void RazorPageDocumentClassifierPass_SetsClass()
        {
            // Arrange
            var properties   = new RazorSourceDocumentProperties(filePath: "ignored", relativePath: "Test.cshtml");
            var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create("@page", properties));

            var engine     = CreateRuntimeEngine();
            var irDocument = CreateIRDocument(engine, codeDocument);
            var pass       = new RazorPageDocumentClassifierPass
            {
                Engine = engine
            };

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

            visitor.Visit(irDocument);

            // Assert
            Assert.Equal("global::Microsoft.AspNetCore.Mvc.RazorPages.Page", visitor.Class.BaseType);
            Assert.Equal(new[] { "public" }, visitor.Class.Modifiers);
            Assert.Equal("Test", visitor.Class.ClassName);
        }
Пример #13
0
        public void RazorPageDocumentClassifierPass_SetsClass()
        {
            // Arrange
            var codeDocument = CreateDocument("@page");
            var engine       = CreateEngine();
            var irDocument   = CreateIRDocument(engine, codeDocument);
            var pass         = new RazorPageDocumentClassifierPass
            {
                Engine = engine
            };

            codeDocument.SetRelativePath("Test.cshtml");

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

            visitor.Visit(irDocument);

            // Assert
            Assert.Equal("global::Microsoft.AspNetCore.Mvc.RazorPages.Page", visitor.Class.BaseType);
            Assert.Equal(new[] { "public" }, visitor.Class.Modifiers);
            Assert.Equal("Test_cshtml", visitor.Class.ClassName);
        }