Пример #1
0
        public void CreateCodeDocument_IncludesDefaultImportIfNotNull()
        {
            // 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 defaultImport  = RazorSourceDocument.ReadFrom(new MemoryStream(), "Default.cshtml");
            var templateEngine = new RazorTemplateEngine(razorEngine, project)
            {
                Options =
                {
                    ImportsFileName = "MyImport.cshtml",
                    DefaultImports  = defaultImport,
                }
            };

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

            // Assert
            Assert.Collection(codeDocument.Imports,
                              import => Assert.Same(defaultImport, import),
                              import => Assert.Equal("/MyImport.cshtml", import.FilePath),
                              import => Assert.Equal("/Views/Home/MyImport.cshtml", import.FilePath));
        }
Пример #2
0
        public void FindHierarchicalItems_WithNestedBasePath(string basePath)
        {
            // Arrange
            var path  = "/Areas/MyArea/Views/Home/Test.cshtml";
            var items = new List <RazorProjectItem>
            {
                CreateProjectItem("/Areas/MyArea/File.cshtml"),
                CreateProjectItem("/File.cshtml")
            };
            var project = new TestRazorProject(items);

            // Act
            var result = project.FindHierarchicalItems(basePath, path, "File.cshtml");

            // Assert
            Assert.Collection(
                result,
                item =>
            {
                Assert.Equal("/Areas/MyArea/Views/Home/File.cshtml", item.FilePath);
                Assert.False(item.Exists);
            },
                item =>
            {
                Assert.Equal("/Areas/MyArea/Views/File.cshtml", item.FilePath);
                Assert.False(item.Exists);
            });
        }
Пример #3
0
        public void FindHierarchicalItems_DoesNotIncludePassedInItem()
        {
            // Arrange
            var path  = "/Areas/MyArea/Views/Home/File.cshtml";
            var items = new List <RazorProjectItem>
            {
                CreateProjectItem("/Areas/MyArea/Views/Home/File.cshtml"),
                CreateProjectItem("/Areas/MyArea/Views/File.cshtml"),
                CreateProjectItem("/Areas/MyArea/File.cshtml"),
                CreateProjectItem("/Areas/File.cshtml"),
                CreateProjectItem("/File.cshtml"),
            };
            var project = new TestRazorProject(items);

            // Act
            var result = project.FindHierarchicalItems(path, "File.cshtml");

            // Assert
            Assert.Collection(
                result,
                item => Assert.Equal("/Areas/MyArea/Views/File.cshtml", item.FilePath),
                item => Assert.Equal("/Areas/MyArea/File.cshtml", item.FilePath),
                item => Assert.Equal("/Areas/File.cshtml", item.FilePath),
                item => Assert.Equal("/File.cshtml", item.FilePath));
        }
Пример #4
0
        public void NormalizeAndEnsureValidPath_ThrowsIfPathIsNullOrEmpty(string path)
        {
            // Arrange
            var project = new TestRazorProject();

            // Act and Assert
            ExceptionAssert.ThrowsArgumentNullOrEmptyString(() => project.NormalizeAndEnsureValidPath(path), "path");
        }
Пример #5
0
        public void NormalizeAndEnsureValidPath_ThrowsIfPathDoesNotStartWithForwardSlash(string path)
        {
            // Arrange
            var project = new TestRazorProject();

            // Act and Assert
            ExceptionAssert.ThrowsArgument(
                () => project.NormalizeAndEnsureValidPath(path),
                "path",
                "Path must begin with a forward slash '/'.");
        }
Пример #6
0
        public void FindHierarchicalItems_ReturnsEmptySequenceIfPathIsAtRoot()
        {
            // Arrange
            var project = new TestRazorProject();

            // Act
            var result = project.FindHierarchicalItems("/", "File.cshtml");

            // Assert
            Assert.Empty(result);
        }
Пример #7
0
        public void NormalizeAndEnsureValidPath_DoesNotModifyPath()
        {
            // Arrange
            var project = new TestRazorProject();

            // Act
            var path = project.NormalizeAndEnsureValidPath("/Views/Home/Index.cshtml");

            // Assert
            Assert.Equal("/Views/Home/Index.cshtml", path);
        }
Пример #8
0
        public void SettingOptions_ThrowsIfValueIsNull()
        {
            // Arrange
            var project        = new TestRazorProject(new RazorProjectItem[] { });
            var razorEngine    = RazorEngine.Create();
            var templateEngine = new RazorTemplateEngine(razorEngine, project);

            // Act & Assert
            ExceptionAssert.ThrowsArgumentNull(
                () => templateEngine.Options = null,
                "value");
        }
Пример #9
0
        public void GenerateCode_ThrowsIfItemCannotBeFound()
        {
            // Arrange
            var project        = new TestRazorProject(new RazorProjectItem[] { });
            var razorEngine    = RazorEngine.Create();
            var templateEngine = new RazorTemplateEngine(razorEngine, project);

            // Act & Assert
            var ex = Assert.Throws <InvalidOperationException>(
                () => templateEngine.GenerateCode("/does-not-exist.cstml"));

            Assert.Equal("The item '/does-not-exist.cstml' could not be found.", ex.Message);
        }
Пример #10
0
        public void GenerateCode_ThrowsIfProjectItemCannotBeFound()
        {
            // Arrange
            var path           = "/Views/Home/Index.cshtml";
            var project        = new TestRazorProject(new RazorProjectItem[] { });
            var razorEngine    = RazorEngine.Create();
            var templateEngine = new RazorTemplateEngine(razorEngine, project);

            // Act & Assert
            var ex = Assert.Throws <InvalidOperationException>(() => templateEngine.GenerateCode(path));

            Assert.Equal($"The item '{path}' could not be found.", ex.Message);
        }
Пример #11
0
        public void FindHierarchicalItems_ReturnsEmptySequenceIfPassedInItemWithFileNameIsAtRoot()
        {
            // Arrange
            var path  = "/File.cshtml";
            var items = new List <RazorProjectItem>
            {
                CreateProjectItem("/File.cshtml")
            };
            var project = new TestRazorProject(items);

            // Act
            var result = project.FindHierarchicalItems(path, "File.cshtml");

            // Assert
            Assert.Empty(result);
        }
Пример #12
0
        public void GenerateCode_WithPath()
        {
            // 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
            var cSharpDocument = templateEngine.GenerateCode(path);

            // Assert
            Assert.NotNull(cSharpDocument);
            Assert.NotEmpty(cSharpDocument.GeneratedCode);
            Assert.Empty(cSharpDocument.Diagnostics);
        }
Пример #13
0
        public void FindHierarchicalItems_ReturnsItemsForPathAtRoot()
        {
            // Arrange
            var path  = "/Index.cshtml";
            var items = new List <RazorProjectItem>
            {
                CreateProjectItem("/File.cshtml")
            };
            var project = new TestRazorProject(items);

            // Act
            var result = project.FindHierarchicalItems(path, "File.cshtml");

            // Assert
            Assert.Collection(
                result,
                item => Assert.Equal("/File.cshtml", item.FilePath));
        }
Пример #14
0
        public void FindHierarchicalItems_ReturnsEmptySequenceIfPathIsNotASubPathOfBasePath()
        {
            // Arrange
            var basePath = "/Pages";
            var path     = "/Areas/MyArea/Views/Home/Test.cshtml";
            var items    = new List <RazorProjectItem>
            {
                CreateProjectItem("/Areas/MyArea/File.cshtml"),
                CreateProjectItem("/File.cshtml"),
            };
            var project = new TestRazorProject(items);

            // Act
            var result = project.FindHierarchicalItems(basePath, path, "File.cshtml");

            // Assert
            Assert.Empty(result);
        }
Пример #15
0
        public void CreateCodeDocument_ThrowsIfPathCannotBeFound()
        {
            // Arrange
            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 & Assert
            var ex = Assert.Throws <InvalidOperationException>(() => templateEngine.CreateCodeDocument("/DoesNotExist.cshtml"));

            // Assert
            Assert.Equal("The item '/DoesNotExist.cshtml' could not be found.", ex.Message);
        }
Пример #16
0
        public void GetImports_CanQueryInformationOnNonExistentFileWithoutImports()
        {
            // Arrange
            var project        = new TestRazorProject();
            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
            Assert.Empty(imports);
        }
Пример #17
0
        public void FindHierarchicalItems_ReturnsItemsForPath(string fileName)
        {
            // Arrange
            var path  = "/Views/Home/Index.cshtml";
            var items = new List <RazorProjectItem>
            {
                CreateProjectItem($"/{fileName}"),
                CreateProjectItem($"/Views/{fileName}"),
                CreateProjectItem($"/Views/Home/{fileName}")
            };
            var project = new TestRazorProject(items);

            // Act
            var result = project.FindHierarchicalItems(path, $"{fileName}");

            // Assert
            Assert.Collection(
                result,
                item => Assert.Equal($"/Views/Home/{fileName}", item.FilePath),
                item => Assert.Equal($"/Views/{fileName}", item.FilePath),
                item => Assert.Equal($"/{fileName}", item.FilePath));
        }
Пример #18
0
        public void GetImportItems_WithPath_ReturnsAllImportsForFile()
        {
            // Arrange
            var expected       = new[] { "/Views/Home/MyImport.cshtml", "/Views/MyImport.cshtml", "/MyImport.cshtml" };
            var project        = new TestRazorProject();
            var razorEngine    = RazorEngine.Create();
            var templateEngine = new RazorTemplateEngine(razorEngine, project)
            {
                Options =
                {
                    ImportsFileName = "MyImport.cshtml"
                }
            };

            // Act
            var imports = templateEngine.GetImportItems("/Views/Home/Index.cshtml");

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

            Assert.Equal(expected, paths);
        }
Пример #19
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));
        }
Пример #20
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);
        }