public void GetInheritedChunks_ReadsChunksFromGlobalFilesInPath() { // Arrange var fileProvider = new TestFileProvider(); fileProvider.AddFile(@"Views\accounts\_GlobalImport.cshtml", "@using AccountModels"); fileProvider.AddFile(@"Views\Shared\_GlobalImport.cshtml", "@inject SharedHelper Shared"); fileProvider.AddFile(@"Views\home\_GlobalImport.cshtml", "@using MyNamespace"); fileProvider.AddFile(@"Views\_GlobalImport.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 DefaultCodeTreeCache(fileProvider); var host = new MvcRazorHost(cache); var utility = new ChunkInheritanceUtility(host, cache, defaultChunks); // Act var codeTrees = utility.GetInheritedCodeTrees(@"Views\home\Index.cshtml"); // Assert Assert.Equal(2, codeTrees.Count); var viewStartChunks = codeTrees[0].Chunks; Assert.Equal(3, viewStartChunks.Count); Assert.IsType<LiteralChunk>(viewStartChunks[0]); var usingChunk = Assert.IsType<UsingChunk>(viewStartChunks[1]); Assert.Equal("MyNamespace", usingChunk.Namespace); Assert.IsType<LiteralChunk>(viewStartChunks[2]); viewStartChunks = codeTrees[1].Chunks; Assert.Equal(5, viewStartChunks.Count); Assert.IsType<LiteralChunk>(viewStartChunks[0]); var injectChunk = Assert.IsType<InjectChunk>(viewStartChunks[1]); Assert.Equal("MyHelper<TModel>", injectChunk.TypeName); Assert.Equal("Helper", injectChunk.MemberName); var setBaseTypeChunk = Assert.IsType<SetBaseTypeChunk>(viewStartChunks[2]); Assert.Equal("MyBaseType", setBaseTypeChunk.TypeName); Assert.IsType<StatementChunk>(viewStartChunks[3]); Assert.IsType<LiteralChunk>(viewStartChunks[4]); }
public void GetOrAdd_ReturnsNullValues_IfFileDoesNotExistInFileProvider() { // Arrange var path = @"Views\_ViewStart.cshtml"; var mockFileProvider = new Mock<TestFileProvider> { CallBase = true }; var fileProvider = mockFileProvider.Object; var codeTreeCache = new DefaultCodeTreeCache(fileProvider); var expected = new CodeTree(); // Act var result1 = codeTreeCache.GetOrAdd(path, fileInfo => expected); var result2 = codeTreeCache.GetOrAdd(path, fileInfo => { throw new Exception("Shouldn't be called."); }); // Assert Assert.Null(result1); Assert.Null(result2); mockFileProvider.Verify(f => f.GetFileInfo(It.IsAny<string>()), Times.Once()); }
public void GetOrAdd_ReturnsCachedEntriesOnSubsequentCalls() { // Arrange var path = @"Views\_ViewStart.cshtml"; var mockFileProvider = new Mock<TestFileProvider> { CallBase = true }; var fileProvider = mockFileProvider.Object; fileProvider.AddFile(path, "test content"); var codeTreeCache = new DefaultCodeTreeCache(fileProvider); var expected = new CodeTree(); // Act var result1 = codeTreeCache.GetOrAdd(path, fileInfo => expected); var result2 = codeTreeCache.GetOrAdd(path, fileInfo => { throw new Exception("Shouldn't be called."); }); // Assert Assert.Same(expected, result1); Assert.Same(expected, result2); mockFileProvider.Verify(f => f.GetFileInfo(It.IsAny<string>()), Times.Once()); }
public void GetInheritedChunks_ReturnsEmptySequenceIfNoGlobalsArePresent() { // Arrange var fileProvider = new TestFileProvider(); fileProvider.AddFile(@"_GlobalImport.cs", string.Empty); fileProvider.AddFile(@"Views\_Layout.cshtml", string.Empty); fileProvider.AddFile(@"Views\home\_not-globalimport.cshtml", string.Empty); var cache = new DefaultCodeTreeCache(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 codeTrees = utility.GetInheritedCodeTrees(@"Views\home\Index.cshtml"); // Assert Assert.Empty(codeTrees); }
public void GetOrAdd_UpdatesCache_IfFileExpirationTriggerExpires() { // Arrange var path = @"Views\Home\_ViewStart.cshtml"; var fileProvider = new TestFileProvider(); fileProvider.AddFile(path, "test content"); var codeTreeCache = new DefaultCodeTreeCache(fileProvider); var expected1 = new CodeTree(); var expected2 = new CodeTree(); // Act 1 var result1 = codeTreeCache.GetOrAdd(path, fileInfo => expected1); // Assert 1 Assert.Same(expected1, result1); // Act 2 fileProvider.GetTrigger(path).IsExpired = true; var result2 = codeTreeCache.GetOrAdd(path, fileInfo => expected2); // Assert 2 Assert.Same(expected2, result2); }
public void MergeInheritedChunks_MergesDefaultInheritedChunks() { // Arrange var fileProvider = new TestFileProvider(); fileProvider.AddFile(@"Views\_GlobalImport.cshtml", "@inject DifferentHelper<TModel> Html"); var cache = new DefaultCodeTreeCache(fileProvider); var host = new MvcRazorHost(cache); var defaultChunks = new Chunk[] { new InjectChunk("MyTestHtmlHelper", "Html"), new UsingChunk { Namespace = "AppNamespace.Model" }, }; var inheritedCodeTrees = new CodeTree[] { new CodeTree { Chunks = new Chunk[] { new UsingChunk { Namespace = "InheritedNamespace" }, new LiteralChunk { Text = "some text" } } }, new CodeTree { Chunks = new Chunk[] { new UsingChunk { Namespace = "AppNamespace.Model" }, } } }; var utility = new ChunkInheritanceUtility(host, cache, defaultChunks); var codeTree = new CodeTree(); // Act utility.MergeInheritedCodeTrees(codeTree, inheritedCodeTrees, "dynamic"); // Assert Assert.Equal(3, codeTree.Chunks.Count); Assert.Same(inheritedCodeTrees[0].Chunks[0], codeTree.Chunks[0]); Assert.Same(inheritedCodeTrees[1].Chunks[0], codeTree.Chunks[1]); Assert.Same(defaultChunks[0], codeTree.Chunks[2]); }
public void GetInheritedChunks_ReadsChunksFromGlobalFilesInPath() { // Arrange var fileProvider = new TestFileProvider(); fileProvider.AddFile(@"Views\accounts\_ViewImports.cshtml", "@using AccountModels"); fileProvider.AddFile(@"Views\Shared\_ViewImports.cshtml", "@inject SharedHelper Shared"); fileProvider.AddFile(@"Views\home\_ViewImports.cshtml", "@using MyNamespace"); fileProvider.AddFile(@"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 DefaultCodeTreeCache(fileProvider); var host = new MvcRazorHost(cache); var utility = new ChunkInheritanceUtility(host, cache, defaultChunks); // Act var codeTrees = utility.GetInheritedCodeTrees(@"Views\home\Index.cshtml"); // Assert Assert.Collection(codeTrees, codeTree => { var viewImportsPath = @"Views\home\_ViewImports.cshtml"; Assert.Collection(codeTree.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); }); }, codeTree => { var viewImportsPath = @"Views\_ViewImports.cshtml"; Assert.Collection(codeTree.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); }); }); }
public void GetOrAdd_UpdatesCacheWithNullValue_IfFileWasDeleted() { // Arrange var path = @"Views\Home\_ViewStart.cshtml"; var fileProvider = new TestFileProvider(); fileProvider.AddFile(path, "test content"); var codeTreeCache = new DefaultCodeTreeCache(fileProvider); var expected1 = new CodeTree(); // Act 1 var result1 = codeTreeCache.GetOrAdd(path, fileInfo => expected1); // Assert 1 Assert.Same(expected1, result1); // Act 2 fileProvider.DeleteFile(path); fileProvider.GetTrigger(path).IsExpired = true; var result2 = codeTreeCache.GetOrAdd(path, fileInfo => { throw new Exception("Shouldn't be called."); }); // Assert 2 Assert.Null(result2); }
public void GetOrAdd_ExpiresEntriesAfterOneMinute() { // Arrange var path = @"Views\Home\_ViewStart.cshtml"; var fileProvider = new TestFileProvider(); fileProvider.AddFile(path, "some content"); var clock = new Mock<ISystemClock>(); var utcNow = DateTimeOffset.UtcNow; clock.SetupGet(c => c.UtcNow) .Returns(() => utcNow); var options = new MemoryCacheOptions { Clock = clock.Object }; var codeTreeCache = new DefaultCodeTreeCache(fileProvider, options); var codeTree1 = new CodeTree(); var codeTree2 = new CodeTree(); // Act 1 var result1 = codeTreeCache.GetOrAdd(path, fileInfo => codeTree1); // Assert 1 Assert.Same(codeTree1, result1); // Act 2 utcNow = utcNow.AddSeconds(59); var result2 = codeTreeCache.GetOrAdd(path, fileInfo => { throw new Exception("Shouldn't be called."); }); // Assert 2 Assert.Same(codeTree1, result2); // Act 3 utcNow = utcNow.AddSeconds(65); var result3 = codeTreeCache.GetOrAdd(path, fileInfo => codeTree2); // Assert 3 Assert.Same(codeTree2, result3); }
private static CodeBuilderContext CreateContext() { var codeTreeCache = new DefaultCodeTreeCache(new TestFileProvider()); return new CodeBuilderContext( new CodeGeneratorContext(new MvcRazorHost(codeTreeCache), "MyClass", "MyNamespace", string.Empty, shouldGenerateLinePragmas: true), new ErrorSink()); }