Exemplo n.º 1
0
        public void CreateCodeDocument_ThrowsIfPathCannotBeFound()
        {
            // Arrange
            var projectItem    = new TestRazorProjectItem("/Views/Home/Index.cshtml");
            var project        = new TestRazorProjectFileSystem(new[] { projectItem });
            var razorEngine    = RazorProjectEngine.Create().Engine;
            var templateEngine = new RazorTemplateEngine(razorEngine, project)
            {
                Options =
                {
                    ImportsFileName = "MyImport.cshtml",
                }
            };

            // Act & Assert
            var ex = Assert.Throws <InvalidOperationException>(() => templateEngine.CreateCodeDocument("/DoesNotExist.cshtml"));

            // Assert
            Assert.Equal("The item '/DoesNotExist.cshtml' could not be found.", ex.Message);
        }
        public void GetHierarchicalItems_Works()
        {
            // Arrange
            var projectSystem = new VirtualRazorProjectFileSystem();
            var viewImport1   = new TestRazorProjectItem("/_ViewImports.cshtml");
            var viewImport2   = new TestRazorProjectItem("/Views/Home/_ViewImports.cshtml");

            projectSystem.Add(viewImport1);
            projectSystem.Add(viewImport2);

            // Act
            var items = projectSystem.FindHierarchicalItems("/", "/Views/Home/Index.cshtml", "_ViewImports.cshtml");

            // Assert
            Assert.Collection(
                items,
                item => Assert.Same(viewImport2, item),
                item => Assert.False(item.Exists),
                item => Assert.Same(viewImport1, item));
        }
        public void EnumerateItems_AtSubDirectory_ReturnsAllFilesUnderDirectoryHierarchy()
        {
            // Arrange
            var projectSystem = new VirtualRazorProjectFileSystem();
            var file1         = new TestRazorProjectItem("/subDirectory/dir2/file1.cshtml");
            var file2         = new TestRazorProjectItem("/file2.cshtml");
            var file3         = new TestRazorProjectItem("/dir3/file3.cshtml");
            var file4         = new TestRazorProjectItem("/subDirectory/file4.cshtml");

            projectSystem.Add(file1);
            projectSystem.Add(file2);
            projectSystem.Add(file3);
            projectSystem.Add(file4);

            // Act
            var result = projectSystem.EnumerateItems("/subDirectory");

            // Assert
            Assert.Equal(new[] { file4, file1 }, result);
        }
Exemplo n.º 4
0
        public void ProcessDesignTime_SetsOptions_DesignTime()
        {
            // Arrange
            var projectItem = new TestRazorProjectItem("Index.cshtml");

            var projectEngine = RazorProjectEngine.Create(RazorConfiguration.Default, TestRazorProjectFileSystem.Empty);

            // Act
            var codeDocument = projectEngine.ProcessDesignTime(projectItem);

            // Assert
            var parserOptions = codeDocument.GetParserOptions();

            Assert.True(parserOptions.DesignTime);

            var codeGenerationOptions = codeDocument.GetCodeGenerationOptions();

            Assert.True(codeGenerationOptions.DesignTime);
            Assert.True(codeGenerationOptions.SuppressChecksum);
            Assert.True(codeGenerationOptions.SuppressMetadataAttributes);
        }
Exemplo n.º 5
0
        public void CreateCodeDocument_NullImportFileName_IncludesDefaultImportIfNotNull()
        {
            // Arrange
            var projectItem    = new TestRazorProjectItem("/Views/Home/Index.cshtml");
            var project        = new TestRazorProject(new[] { projectItem });
            var razorEngine    = RazorEngine.Create();
            var defaultImport  = RazorSourceDocument.ReadFrom(new MemoryStream(), "Default.cshtml");
            var templateEngine = new RazorTemplateEngine(razorEngine, project)
            {
                Options =
                {
                    DefaultImports = defaultImport,
                }
            };

            // Act
            var codeDocument = templateEngine.CreateCodeDocument(projectItem);

            // Assert
            Assert.Collection(codeDocument.Imports,
                              import => Assert.Same(defaultImport, import));
        }
Exemplo n.º 6
0
        public void ProcessDesignTime_WithImportsAndTagHelpers_SetsOnCodeDocument()
        {
            // Arrange
            var projectItem        = new TestRazorProjectItem("Index.cshtml");
            var importItem         = new TestRazorProjectItem("_import.cshtml");
            var expectedImports    = new[] { RazorSourceDocument.ReadFrom(importItem) };
            var expectedTagHelpers = new[]
            {
                TagHelperDescriptorBuilder.Create("TestTagHelper", "TestAssembly").Build(),
                TagHelperDescriptorBuilder.Create("Test2TagHelper", "TestAssembly").Build(),
            };

            var projectEngine = RazorProjectEngine.Create(RazorConfiguration.Default, TestRazorProjectFileSystem.Empty);

            // Act
            var codeDocument = projectEngine.ProcessDesignTime(RazorSourceDocument.ReadFrom(projectItem), expectedImports, expectedTagHelpers);

            // Assert
            var tagHelpers = codeDocument.GetTagHelpers();

            Assert.Same(expectedTagHelpers, tagHelpers);
            Assert.Equal(expectedImports, codeDocument.Imports);
        }
Exemplo n.º 7
0
        public void GetImportItems_WithItem_ReturnsAllImportsForFile()
        {
            // Arrange
            var expected       = new[] { "/Views/Home/MyImport.cshtml", "/Views/MyImport.cshtml", "/MyImport.cshtml" };
            var projectItem    = new TestRazorProjectItem("/Views/Home/Index.cshtml");
            var project        = new TestRazorProject(new[] { projectItem });
            var razorEngine    = RazorEngine.Create();
            var templateEngine = new RazorTemplateEngine(razorEngine, project)
            {
                Options =
                {
                    ImportsFileName = "MyImport.cshtml"
                }
            };

            // Act
            var imports = templateEngine.GetImportItems(projectItem);

            // Assert
            var paths = imports.Select(i => i.FilePath);

            Assert.Equal(expected, paths);
        }
Exemplo n.º 8
0
        public void GenerateCode_WithCodeDocument()
        {
            // Arrange
            var path           = "/Views/Home/Index.cshtml";
            var projectItem    = new TestRazorProjectItem(path);
            var project        = new TestRazorProject(new[] { projectItem });
            var razorEngine    = RazorEngine.Create();
            var templateEngine = new RazorTemplateEngine(razorEngine, project);

            // Act - 1
            var codeDocument = templateEngine.CreateCodeDocument(path);

            // Assert - 1
            Assert.NotNull(codeDocument);

            // Act - 2
            var cSharpDocument = templateEngine.GenerateCode(codeDocument);

            // Assert
            Assert.NotNull(cSharpDocument);
            Assert.NotEmpty(cSharpDocument.GeneratedCode);
            Assert.Empty(cSharpDocument.Diagnostics);
        }
Exemplo n.º 9
0
        public void DirectoryNode_AddsFiles_ToSiblingDirectories()
        {
            // Arrange
            var root         = new DirectoryNode("/");
            var projectItem1 = new TestRazorProjectItem("/Pages/Products/Index.cshtml");
            var projectItem2 = new TestRazorProjectItem("/Pages/Accounts/About.cshtml");

            // Act
            root.AddFile(new FileNode(projectItem1.FilePath, projectItem1));
            root.AddFile(new FileNode(projectItem2.FilePath, projectItem2));

            // Assert
            Assert.Collection(
                root.Directories,
                directory =>
            {
                Assert.Equal("/Pages/", directory.Path);
                Assert.Empty(directory.Files);

                Assert.Collection(
                    directory.Directories,
                    subDirectory =>
                {
                    Assert.Equal("/Pages/Products/", subDirectory.Path);
                    Assert.Collection(
                        subDirectory.Files,
                        file => Assert.Same(projectItem1, file.ProjectItem));
                },
                    subDirectory =>
                {
                    Assert.Equal("/Pages/Accounts/", subDirectory.Path);
                    Assert.Collection(
                        subDirectory.Files,
                        file => Assert.Same(projectItem2, file.ProjectItem));
                });
            });
        }
Exemplo n.º 10
0
        public void GetImports_CanQueryInformationOnNonExistentFileWithImports()
        {
            // Arrange
            var path           = "/Views/Home/MyImport.cshtml";
            var projectItem    = new TestRazorProjectItem(path);
            var project        = new TestRazorProject(new[] { projectItem });
            var razorEngine    = RazorEngine.Create();
            var templateEngine = new RazorTemplateEngine(razorEngine, project)
            {
                Options =
                {
                    ImportsFileName = "MyImport.cshtml"
                }
            };
            var projectItemToQuery = project.GetItem("/Views/Home/Index.cshtml");

            // Act
            var imports = templateEngine.GetImports(projectItemToQuery);

            // Assert
            var import = Assert.Single(imports);

            Assert.Equal(projectItem.FilePath, import.FilePath);
        }
Exemplo n.º 11
0
        public void CreateCodeDocument_IncludesImportsIfFileIsPresent()
        {
            // Arrange
            var projectItem    = new TestRazorProjectItem("/Views/Home/Index.cshtml");
            var import1        = new TestRazorProjectItem("/MyImport.cshtml");
            var import2        = new TestRazorProjectItem("/Views/Home/MyImport.cshtml");
            var project        = new TestRazorProject(new[] { import1, import2, projectItem });
            var razorEngine    = RazorEngine.Create();
            var templateEngine = new RazorTemplateEngine(razorEngine, project)
            {
                Options =
                {
                    ImportsFileName = "MyImport.cshtml",
                }
            };

            // Act
            var codeDocument = templateEngine.CreateCodeDocument("/Views/Home/Index.cshtml");

            // Assert
            Assert.Collection(codeDocument.Imports,
                              import => Assert.Equal("/MyImport.cshtml", import.FilePath),
                              import => Assert.Equal("/Views/Home/MyImport.cshtml", import.FilePath));
        }