Esempio n. 1
0
        public void GetInheritedChunks_ReturnsEmptySequenceIfNoGlobalsArePresent()
        {
            // Arrange
            var fileProvider = new TestFileProvider();

            fileProvider.AddFile(@"_ViewImports.cs", string.Empty);
            fileProvider.AddFile(PlatformNormalizer.NormalizePath(@"Views\_Layout.cshtml"), string.Empty);
            fileProvider.AddFile(PlatformNormalizer.NormalizePath(@"Views\home\_not-viewimports.cshtml"), string.Empty);
            var cache         = new DefaultChunkTreeCache(fileProvider);
            var host          = new MvcRazorHost(cache);
            var defaultChunks = new Chunk[]
            {
                new InjectChunk("MyTestHtmlHelper", "Html"),
                new UsingChunk {
                    Namespace = "AppNamespace.Model"
                },
            };
            var utility = new ChunkInheritanceUtility(host, cache, defaultChunks);

            // Act
            var chunkTrees = utility.GetInheritedChunkTrees(PlatformNormalizer.NormalizePath(@"Views\home\Index.cshtml"));

            // Assert
            Assert.Empty(chunkTrees);
        }
Esempio n. 2
0
        public void MergeInheritedChunks_MergesDefaultInheritedChunks()
        {
            // Arrange
            var fileProvider = new TestFileProvider();

            fileProvider.AddFile(PlatformNormalizer.NormalizePath(@"Views\_ViewImports.cshtml"),
                                 "@inject DifferentHelper<TModel> Html");
            var cache         = new DefaultChunkTreeCache(fileProvider);
            var host          = new MvcRazorHost(cache);
            var defaultChunks = new Chunk[]
            {
                new InjectChunk("MyTestHtmlHelper", "Html"),
                new UsingChunk {
                    Namespace = "AppNamespace.Model"
                },
            };
            var inheritedChunkTrees = new ChunkTree[]
            {
                new ChunkTree
                {
                    Chunks = new Chunk[]
                    {
                        new UsingChunk {
                            Namespace = "InheritedNamespace"
                        },
                        new LiteralChunk {
                            Text = "some text"
                        }
                    }
                },
                new ChunkTree
                {
                    Chunks = new Chunk[]
                    {
                        new UsingChunk {
                            Namespace = "AppNamespace.Model"
                        },
                    }
                }
            };

            var utility   = new ChunkInheritanceUtility(host, cache, defaultChunks);
            var chunkTree = new ChunkTree();

            // Act
            utility.MergeInheritedChunkTrees(chunkTree,
                                             inheritedChunkTrees,
                                             "dynamic");

            // Assert
            Assert.Equal(3, chunkTree.Chunks.Count);
            Assert.Same(inheritedChunkTrees[0].Chunks[0], chunkTree.Chunks[0]);
            Assert.Same(inheritedChunkTrees[1].Chunks[0], chunkTree.Chunks[1]);
            Assert.Same(defaultChunks[0], chunkTree.Chunks[2]);
        }
        public void GetViewStartLocations_SkipsCurrentPath_IfCurrentIsViewStart(string inputPath)
        {
            // Arrange
            var expected = new[]
            {
                PlatformNormalizer.NormalizePath(@"Views\_ViewStart.cshtml"),
                @"_ViewStart.cshtml"
            };

            // Act
            var result = ViewHierarchyUtility.GetViewStartLocations(inputPath);

            // Assert
            Assert.Equal(expected, result);
        }
        public void GetViewImportsLocations_ReturnsPotentialViewStartLocations_PathStartswithSlash(string inputPath)
        {
            // Arrange
            var expected = new[]
            {
                PlatformNormalizer.NormalizePath(@"Views\Home\_ViewImports.cshtml"),
                PlatformNormalizer.NormalizePath(@"Views\_ViewImports.cshtml"),
                @"_ViewImports.cshtml"
            };

            // Act
            var result = ViewHierarchyUtility.GetViewImportsLocations(inputPath);

            // Assert
            Assert.Equal(expected, result);
        }
        public void GetViewStartLocations_SkipsCurrentPath_IfPathIsAViewStartFile(string fileName)
        {
            // Arrange
            var expected = new[]
            {
                PlatformNormalizer.NormalizePath(@"Areas\MyArea\Sub\Views\_ViewStart.cshtml"),
                PlatformNormalizer.NormalizePath(@"Areas\MyArea\Sub\_ViewStart.cshtml"),
                PlatformNormalizer.NormalizePath(@"Areas\MyArea\_ViewStart.cshtml"),
                PlatformNormalizer.NormalizePath(@"Areas\_ViewStart.cshtml"),
                @"_ViewStart.cshtml",
            };
            var viewPath = Path.Combine("Areas", "MyArea", "Sub", "Views", "Admin", fileName);

            // Act
            var result = ViewHierarchyUtility.GetViewStartLocations(viewPath);

            // Assert
            Assert.Equal(expected, result);
        }
        public void GetViewImportsLocations_ReturnsPotentialGlobalLocations(string fileName)
        {
            // Arrange
            var expected = new[]
            {
                PlatformNormalizer.NormalizePath(@"Areas\MyArea\Sub\Views\Admin\_ViewImports.cshtml"),
                PlatformNormalizer.NormalizePath(@"Areas\MyArea\Sub\Views\_ViewImports.cshtml"),
                PlatformNormalizer.NormalizePath(@"Areas\MyArea\Sub\_ViewImports.cshtml"),
                PlatformNormalizer.NormalizePath(@"Areas\MyArea\_ViewImports.cshtml"),
                PlatformNormalizer.NormalizePath(@"Areas\_ViewImports.cshtml"),
                @"_ViewImports.cshtml",
            };
            var viewPath = Path.Combine("Areas", "MyArea", "Sub", "Views", "Admin", fileName);

            // Act
            var result = ViewHierarchyUtility.GetViewImportsLocations(viewPath);

            // Assert
            Assert.Equal(expected, result);
        }
Esempio n. 7
0
        public async Task RazorView_SetsViewPathAndExecutingPagePath()
        {
            // Arrange
            var outputFile      = "compiler/resources/ViewEngineController.ViewWithPaths.txt";
            var expectedContent = await ResourceFile.ReadResourceAsync(_assembly, outputFile, sourceFile : false);

            // Act
            var responseContent = await Client.GetStringAsync("http://localhost/ViewWithPaths");

            // Assert
            responseContent = responseContent.Trim();
#if GENERATE_BASELINES
            ResourceFile.UpdateFile(_assembly, outputFile, expectedContent, responseContent);
#else
            Assert.Equal(
                PlatformNormalizer.NormalizePath(expectedContent),
                responseContent,
                ignoreLineEndingDifferences: true);
#endif
        }
Esempio n. 8
0
        public async Task CompilationFailuresFromViewImportsAreListed()
        {
            // Arrange
            var expectedMessage = "The type or namespace name &#x27;NamespaceDoesNotExist&#x27; could not be found ("
                                  + "are you missing a using directive or an assembly reference?)";
            var expectedMediaType = MediaTypeHeaderValue.Parse("text/html; charset=utf-8");

            // Act
            var response = await Client.GetAsync("http://localhost/ErrorFromViewImports");

            // Assert
            Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);
            Assert.Equal(expectedMediaType, response.Content.Headers.ContentType);
            var content = await response.Content.ReadAsStringAsync();

            Assert.Contains(
                PlatformNormalizer.NormalizePath(@"Views\ErrorFromViewImports\_ViewImports.cshtml"),
                content);

            Assert.Contains(expectedMessage, content);
        }
Esempio n. 9
0
        public void GetInheritedChunks_ReadsChunksFromGlobalFilesInPath()
        {
            // Arrange
            var fileProvider = new TestFileProvider();

            fileProvider.AddFile(PlatformNormalizer.NormalizePath(@"Views\accounts\_ViewImports.cshtml"), "@using AccountModels");
            fileProvider.AddFile(PlatformNormalizer.NormalizePath(@"Views\Shared\_ViewImports.cshtml"), "@inject SharedHelper Shared");
            fileProvider.AddFile(PlatformNormalizer.NormalizePath(@"Views\home\_ViewImports.cshtml"), "@using MyNamespace");
            fileProvider.AddFile(PlatformNormalizer.NormalizePath(@"Views\_ViewImports.cshtml"),
                                 @"@inject MyHelper<TModel> Helper
@inherits MyBaseType

@{
    Layout = ""test.cshtml"";
}

");
            var defaultChunks = new Chunk[]
            {
                new InjectChunk("MyTestHtmlHelper", "Html"),
                new UsingChunk {
                    Namespace = "AppNamespace.Model"
                },
            };
            var cache   = new DefaultChunkTreeCache(fileProvider);
            var host    = new MvcRazorHost(cache);
            var utility = new ChunkInheritanceUtility(host, cache, defaultChunks);

            // Act
            var chunkTrees = utility.GetInheritedChunkTrees(PlatformNormalizer.NormalizePath(@"Views\home\Index.cshtml"));

            // Assert
            Assert.Collection(chunkTrees,
                              chunkTree =>
            {
                var viewImportsPath = PlatformNormalizer.NormalizePath(@"Views\home\_ViewImports.cshtml");
                Assert.Collection(chunkTree.Chunks,
                                  chunk =>
                {
                    Assert.IsType <LiteralChunk>(chunk);
                    Assert.Equal(viewImportsPath, chunk.Start.FilePath);
                },
                                  chunk =>
                {
                    var usingChunk = Assert.IsType <UsingChunk>(chunk);
                    Assert.Equal("MyNamespace", usingChunk.Namespace);
                    Assert.Equal(viewImportsPath, chunk.Start.FilePath);
                },
                                  chunk =>
                {
                    Assert.IsType <LiteralChunk>(chunk);
                    Assert.Equal(viewImportsPath, chunk.Start.FilePath);
                });
            },
                              chunkTree =>
            {
                var viewImportsPath = PlatformNormalizer.NormalizePath(@"Views\_ViewImports.cshtml");
                Assert.Collection(chunkTree.Chunks,
                                  chunk =>
                {
                    Assert.IsType <LiteralChunk>(chunk);
                    Assert.Equal(viewImportsPath, chunk.Start.FilePath);
                },
                                  chunk =>
                {
                    var injectChunk = Assert.IsType <InjectChunk>(chunk);
                    Assert.Equal("MyHelper<TModel>", injectChunk.TypeName);
                    Assert.Equal("Helper", injectChunk.MemberName);
                    Assert.Equal(viewImportsPath, chunk.Start.FilePath);
                },
                                  chunk =>
                {
                    Assert.IsType <LiteralChunk>(chunk);
                    Assert.Equal(viewImportsPath, chunk.Start.FilePath);
                },
                                  chunk =>
                {
                    var setBaseTypeChunk = Assert.IsType <SetBaseTypeChunk>(chunk);
                    Assert.Equal("MyBaseType", setBaseTypeChunk.TypeName);
                    Assert.Equal(viewImportsPath, chunk.Start.FilePath);
                },
                                  chunk =>
                {
                    Assert.IsType <LiteralChunk>(chunk);
                    Assert.Equal(viewImportsPath, chunk.Start.FilePath);
                },
                                  chunk =>
                {
                    Assert.IsType <StatementChunk>(chunk);
                    Assert.Equal(viewImportsPath, chunk.Start.FilePath);
                },
                                  chunk =>
                {
                    Assert.IsType <LiteralChunk>(chunk);
                    Assert.Equal(viewImportsPath, chunk.Start.FilePath);
                });
            });
        }