public void ComponentDocumentClassifierPass_SetsClass()
        {
            // Arrange
            var properties   = new RazorSourceDocumentProperties(filePath: "/MyApp/Test.razor", relativePath: "Test.razor");
            var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create("some-content", properties));

            codeDocument.SetFileKind(FileKinds.Component);

            var projectEngine = CreateProjectEngine(b =>
            {
                b.SetRootNamespace("MyApp");
            });

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

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

            visitor.Visit(irDocument);

            // Assert
            Assert.Equal($"{ComponentsApi.ComponentBase.FullTypeName}", visitor.Class.BaseType);
            Assert.Equal(new[] { "public", "partial" }, visitor.Class.Modifiers);
            Assert.Equal("Test", visitor.Class.ClassName);
        }
        public void ComponentDocumentClassifierPass_UsesRelativePathToGenerateTypeNameAndNamespace()
        {
            // Arrange
            var relativePath = "/Pages/Announcements/Banner.razor";
            var properties   = new RazorSourceDocumentProperties(filePath: $"/MyApp{relativePath}", relativePath: relativePath);
            var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create("some-content", properties));

            codeDocument.SetFileKind(FileKinds.Component);

            var projectEngine = CreateProjectEngine(b =>
            {
                b.SetRootNamespace("MyApp");
            });

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

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

            visitor.Visit(irDocument);

            // Assert
            Assert.Equal("Banner", visitor.Class.ClassName);
            Assert.Equal("MyApp.Pages.Announcements", visitor.Namespace.Content);
        }
        public void ComponentDocumentClassifierPass_SetsNamespace()
        {
            // Arrange
            var properties   = new RazorSourceDocumentProperties(filePath: "/MyApp/Test.razor", relativePath: "Test.razor");
            var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create("some-content", properties));

            codeDocument.SetFileKind(FileKinds.Component);

            var projectEngine = CreateProjectEngine(b =>
            {
                b.SetRootNamespace("MyApp");
            });

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

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

            visitor.Visit(irDocument);

            // Assert
            Assert.Equal("MyApp", visitor.Namespace.Content);
        }
        public void ComponentDocumentClassifierPass_SetsUpMainMethod()
        {
            // Arrange
            var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create("some-content", "Test.razor"));

            codeDocument.SetFileKind(FileKinds.Component);

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

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

            visitor.Visit(irDocument);

            // Assert
            Assert.Equal(ComponentsApi.ComponentBase.BuildRenderTree, visitor.Method.MethodName);
            Assert.Equal("void", visitor.Method.ReturnType);
            Assert.Equal(new[] { "protected", "override" }, visitor.Method.Modifiers);
        }
        public void ComponentDocumentClassifierPass_SanitizesClassName()
        {
            // Arrange
            var properties   = new RazorSourceDocumentProperties(filePath: @"x:\My.+App\path.with+invalid-chars.razor", relativePath: "path.with+invalid-chars.razor");
            var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create("some-content", properties));

            codeDocument.SetFileKind(FileKinds.Component);

            var projectEngine = CreateProjectEngine(b =>
            {
                b.SetRootNamespace("My.+App");
            });

            var irDocument = CreateIRDocument(projectEngine, codeDocument);
            var pass       = new ComponentDocumentClassifierPass
            {
                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);
            Assert.Equal("My._App", visitor.Namespace.Content);
        }
예제 #6
0
        public void ComponentDocumentClassifierPass_SetsClass()
        {
            // Arrange
            var properties   = new RazorSourceDocumentProperties(filePath: "/MyApp/Test.razor", relativePath: "Test.razor");
            var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create("some-content", properties));

            codeDocument.SetInputDocumentKind(InputDocumentKind.Component);

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

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

            visitor.Visit(irDocument);

            // Assert
            Assert.Equal($"global::{CodeGenerationConstants.RazorComponent.FullTypeName}", visitor.Class.BaseType);
            Assert.Equal(new[] { "public", "sealed" }, visitor.Class.Modifiers);
            Assert.Equal("Test", visitor.Class.ClassName);
        }
        public void Execute_SetsDocumentKind()
        {
            // Arrange
            var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create("some-content", "Test.razor"));

            codeDocument.SetFileKind(FileKinds.Component);

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

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

            // Assert
            Assert.Equal(ComponentDocumentClassifierPass.ComponentDocumentKind, irDocument.DocumentKind);
        }