diff --git a/src/Microsoft.AspNetCore.Mvc.Razor.Host/IMvcRazorHost.cs b/src/Microsoft.AspNetCore.Mvc.Razor.Host/IMvcRazorHost.cs index 9bd444fe55..fa8b6f98a9 100644 --- a/src/Microsoft.AspNetCore.Mvc.Razor.Host/IMvcRazorHost.cs +++ b/src/Microsoft.AspNetCore.Mvc.Razor.Host/IMvcRazorHost.cs @@ -1,7 +1,6 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. -using System; using System.IO; using Microsoft.AspNetCore.Razor.CodeGenerators; @@ -10,7 +9,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor /// /// Specifies the contracts for a Razor host that parses Razor files and generates C# code. /// - public interface IMvcRazorHost : IDisposable + public interface IMvcRazorHost { /// /// Parses and generates the contents of a Razor file represented by . diff --git a/src/Microsoft.AspNetCore.Mvc.Razor.Host/MvcRazorHost.cs b/src/Microsoft.AspNetCore.Mvc.Razor.Host/MvcRazorHost.cs index 139b80304f..4c97ecbea3 100644 --- a/src/Microsoft.AspNetCore.Mvc.Razor.Host/MvcRazorHost.cs +++ b/src/Microsoft.AspNetCore.Mvc.Razor.Host/MvcRazorHost.cs @@ -295,7 +295,6 @@ namespace Microsoft.AspNetCore.Mvc.Razor } var inheritedChunkTrees = GetInheritedChunkTrees(sourceFileName); - return new MvcRazorParser(razorParser, inheritedChunkTrees, DefaultInheritedChunks, ModelExpressionType); } @@ -345,11 +344,6 @@ namespace Microsoft.AspNetCore.Mvc.Razor }); } - public void Dispose() - { - _chunkTreeCache.Dispose(); - } - private IReadOnlyList GetInheritedChunkTrees(string sourceFileName) { var inheritedChunkTrees = GetInheritedChunkTreeResults(sourceFileName) diff --git a/test/Microsoft.AspNetCore.Mvc.Razor.Host.Test/Directives/ChunkInheritanceUtilityTest.cs b/test/Microsoft.AspNetCore.Mvc.Razor.Host.Test/Directives/ChunkInheritanceUtilityTest.cs index 1ebce27f20..f4debb6bf2 100644 --- a/test/Microsoft.AspNetCore.Mvc.Razor.Host.Test/Directives/ChunkInheritanceUtilityTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.Razor.Host.Test/Directives/ChunkInheritanceUtilityTest.cs @@ -32,83 +32,81 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Directives new UsingChunk { Namespace = "AppNamespace.Model" }, }; var cache = new DefaultChunkTreeCache(fileProvider); - using (var host = new MvcRazorHost(cache, new TagHelperDescriptorResolver(designTime: false))) - { - var utility = new ChunkInheritanceUtility(host, cache, defaultChunks); + var host = new MvcRazorHost(cache, new TagHelperDescriptorResolver(designTime: false)); + var utility = new ChunkInheritanceUtility(host, cache, defaultChunks); - // Act - var chunkTreeResults = utility.GetInheritedChunkTreeResults( - PlatformNormalizer.NormalizePath(@"Views\home\Index.cshtml")); + // Act + var chunkTreeResults = utility.GetInheritedChunkTreeResults( + PlatformNormalizer.NormalizePath(@"Views\home\Index.cshtml")); - // Assert - Assert.Collection(chunkTreeResults, - chunkTreeResult => - { - var viewImportsPath = @"/Views/_ViewImports.cshtml"; - Assert.Collection(chunkTreeResult.ChunkTree.Children, - chunk => - { - Assert.IsType(chunk); - Assert.Equal(viewImportsPath, chunk.Start.FilePath); - }, - chunk => - { - var injectChunk = Assert.IsType(chunk); - Assert.Equal("MyHelper", injectChunk.TypeName); - Assert.Equal("Helper", injectChunk.MemberName); - Assert.Equal(viewImportsPath, chunk.Start.FilePath); - }, - chunk => - { - Assert.IsType(chunk); - Assert.Equal(viewImportsPath, chunk.Start.FilePath); - }, - chunk => - { - var setBaseTypeChunk = Assert.IsType(chunk); - Assert.Equal("MyBaseType", setBaseTypeChunk.TypeName); - Assert.Equal(viewImportsPath, chunk.Start.FilePath); - }, - chunk => - { - Assert.IsType(chunk); - Assert.Equal(viewImportsPath, chunk.Start.FilePath); - }, - chunk => - { - Assert.IsType(chunk); - Assert.Equal(viewImportsPath, chunk.Start.FilePath); - }, - chunk => - { - Assert.IsType(chunk); - Assert.Equal(viewImportsPath, chunk.Start.FilePath); - }); - Assert.Equal(viewImportsPath, chunkTreeResult.FilePath); - }, - chunkTreeResult => - { - var viewImportsPath = "/Views/home/_ViewImports.cshtml"; - Assert.Collection(chunkTreeResult.ChunkTree.Children, - chunk => - { - Assert.IsType(chunk); - Assert.Equal(viewImportsPath, chunk.Start.FilePath); - }, - chunk => - { - var usingChunk = Assert.IsType(chunk); - Assert.Equal("MyNamespace", usingChunk.Namespace); - Assert.Equal(viewImportsPath, chunk.Start.FilePath); - }, - chunk => - { - Assert.IsType(chunk); - Assert.Equal(viewImportsPath, chunk.Start.FilePath); - }); - Assert.Equal(viewImportsPath, chunkTreeResult.FilePath); - }); - } + // Assert + Assert.Collection(chunkTreeResults, + chunkTreeResult => + { + var viewImportsPath = @"/Views/_ViewImports.cshtml"; + Assert.Collection(chunkTreeResult.ChunkTree.Children, + chunk => + { + Assert.IsType(chunk); + Assert.Equal(viewImportsPath, chunk.Start.FilePath); + }, + chunk => + { + var injectChunk = Assert.IsType(chunk); + Assert.Equal("MyHelper", injectChunk.TypeName); + Assert.Equal("Helper", injectChunk.MemberName); + Assert.Equal(viewImportsPath, chunk.Start.FilePath); + }, + chunk => + { + Assert.IsType(chunk); + Assert.Equal(viewImportsPath, chunk.Start.FilePath); + }, + chunk => + { + var setBaseTypeChunk = Assert.IsType(chunk); + Assert.Equal("MyBaseType", setBaseTypeChunk.TypeName); + Assert.Equal(viewImportsPath, chunk.Start.FilePath); + }, + chunk => + { + Assert.IsType(chunk); + Assert.Equal(viewImportsPath, chunk.Start.FilePath); + }, + chunk => + { + Assert.IsType(chunk); + Assert.Equal(viewImportsPath, chunk.Start.FilePath); + }, + chunk => + { + Assert.IsType(chunk); + Assert.Equal(viewImportsPath, chunk.Start.FilePath); + }); + Assert.Equal(viewImportsPath, chunkTreeResult.FilePath); + }, + chunkTreeResult => + { + var viewImportsPath = "/Views/home/_ViewImports.cshtml"; + Assert.Collection(chunkTreeResult.ChunkTree.Children, + chunk => + { + Assert.IsType(chunk); + Assert.Equal(viewImportsPath, chunk.Start.FilePath); + }, + chunk => + { + var usingChunk = Assert.IsType(chunk); + Assert.Equal("MyNamespace", usingChunk.Namespace); + Assert.Equal(viewImportsPath, chunk.Start.FilePath); + }, + chunk => + { + Assert.IsType(chunk); + Assert.Equal(viewImportsPath, chunk.Start.FilePath); + }); + Assert.Equal(viewImportsPath, chunkTreeResult.FilePath); + }); } [Fact] @@ -120,21 +118,19 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Directives fileProvider.AddFile(@"/Views/_Layout.cshtml", string.Empty); fileProvider.AddFile(@"/Views/home/_not-viewimports.cshtml", string.Empty); var cache = new DefaultChunkTreeCache(fileProvider); - using (var host = new MvcRazorHost(cache, new TagHelperDescriptorResolver(designTime: false))) + var host = new MvcRazorHost(cache, new TagHelperDescriptorResolver(designTime: false)); + var defaultChunks = new Chunk[] { - var defaultChunks = new Chunk[] - { new InjectChunk("MyTestHtmlHelper", "Html"), new UsingChunk { Namespace = "AppNamespace.Model" }, - }; - var utility = new ChunkInheritanceUtility(host, cache, defaultChunks); + }; + var utility = new ChunkInheritanceUtility(host, cache, defaultChunks); - // Act - var chunkTrees = utility.GetInheritedChunkTreeResults(PlatformNormalizer.NormalizePath(@"Views\home\Index.cshtml")); + // Act + var chunkTrees = utility.GetInheritedChunkTreeResults(PlatformNormalizer.NormalizePath(@"Views\home\Index.cshtml")); - // Assert - Assert.Empty(chunkTrees); - } + // Assert + Assert.Empty(chunkTrees); } [Fact] @@ -145,15 +141,14 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Directives fileProvider.AddFile(@"/Views/_ViewImports.cshtml", "@inject DifferentHelper Html"); var cache = new DefaultChunkTreeCache(fileProvider); - using (var host = new MvcRazorHost(cache, new TagHelperDescriptorResolver(designTime: false))) + var host = new MvcRazorHost(cache, new TagHelperDescriptorResolver(designTime: false)); + var defaultChunks = new Chunk[] { - var defaultChunks = new Chunk[] - { new InjectChunk("MyTestHtmlHelper", "Html"), new UsingChunk { Namespace = "AppNamespace.Model" }, - }; - var inheritedChunkTrees = new ChunkTree[] - { + }; + var inheritedChunkTrees = new ChunkTree[] + { new ChunkTree { Children = new Chunk[] @@ -169,20 +164,19 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Directives new UsingChunk { Namespace = "AppNamespace.Model" }, } } - }; + }; - var utility = new ChunkInheritanceUtility(host, cache, defaultChunks); - var chunkTree = new ChunkTree(); + var utility = new ChunkInheritanceUtility(host, cache, defaultChunks); + var chunkTree = new ChunkTree(); - // Act - utility.MergeInheritedChunkTrees(chunkTree, inheritedChunkTrees, "dynamic"); + // Act + utility.MergeInheritedChunkTrees(chunkTree, inheritedChunkTrees, "dynamic"); - // Assert - Assert.Collection(chunkTree.Children, - chunk => Assert.Same(defaultChunks[1], chunk), - chunk => Assert.Same(inheritedChunkTrees[0].Children[0], chunk), - chunk => Assert.Same(defaultChunks[0], chunk)); - } + // Assert + Assert.Collection(chunkTree.Children, + chunk => Assert.Same(defaultChunks[1], chunk), + chunk => Assert.Same(inheritedChunkTrees[0].Children[0], chunk), + chunk => Assert.Same(defaultChunks[0], chunk)); } } } \ No newline at end of file diff --git a/test/Microsoft.AspNetCore.Mvc.Razor.Host.Test/MvcRazorHostTest.cs b/test/Microsoft.AspNetCore.Mvc.Razor.Host.Test/MvcRazorHostTest.cs index 7842ee1652..74402918dd 100644 --- a/test/Microsoft.AspNetCore.Mvc.Razor.Host.Test/MvcRazorHostTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.Razor.Host.Test/MvcRazorHostTest.cs @@ -56,23 +56,22 @@ namespace Microsoft.AspNetCore.Mvc.Razor var rootedAppPath = $"{rootPrefix}SomeComputer/Location/Project/"; var rootedFilePath = $"{rootPrefix}SomeComputer/Location/Project/src/file.cshtml"; var chunkTreeCache = new DefaultChunkTreeCache(new TestFileProvider()); - using (var host = new MvcRazorHost( + var host = new MvcRazorHost( chunkTreeCache, - pathNormalizer: new DesignTimeRazorPathNormalizer(rootedAppPath))) - { - var parser = new RazorParser( - host.CodeLanguage.CreateCodeParser(), - host.CreateMarkupParser(), - tagHelperDescriptorResolver: null); - var chunkInheritanceUtility = new PathValidatingChunkInheritanceUtility(host, chunkTreeCache); - host.ChunkInheritanceUtility = chunkInheritanceUtility; + pathNormalizer: new DesignTimeRazorPathNormalizer(rootedAppPath)); - // Act - host.DecorateRazorParser(parser, rootedFilePath); + var parser = new RazorParser( + host.CodeLanguage.CreateCodeParser(), + host.CreateMarkupParser(), + tagHelperDescriptorResolver: null); + var chunkInheritanceUtility = new PathValidatingChunkInheritanceUtility(host, chunkTreeCache); + host.ChunkInheritanceUtility = chunkInheritanceUtility; - // Assert - Assert.Equal("src/file.cshtml", chunkInheritanceUtility.InheritedChunkTreePagePath, StringComparer.Ordinal); - } + // Act + host.DecorateRazorParser(parser, rootedFilePath); + + // Assert + Assert.Equal("src/file.cshtml", chunkInheritanceUtility.InheritedChunkTreePagePath, StringComparer.Ordinal); } [Theory] @@ -84,28 +83,27 @@ namespace Microsoft.AspNetCore.Mvc.Razor var rootedAppPath = $"{rootPrefix}SomeComputer/Location/Project/"; var rootedFilePath = $"{rootPrefix}SomeComputer/Location/Project/src/file.cshtml"; var chunkTreeCache = new DefaultChunkTreeCache(new TestFileProvider()); - using (var host = new MvcRazorHost( + var host = new MvcRazorHost( chunkTreeCache, - pathNormalizer: new DesignTimeRazorPathNormalizer(rootedAppPath))) - { - var chunkInheritanceUtility = new PathValidatingChunkInheritanceUtility(host, chunkTreeCache); - var codeGeneratorContext = new CodeGeneratorContext( - new ChunkGeneratorContext( - host, - host.DefaultClassName, - host.DefaultNamespace, - rootedFilePath, - shouldGenerateLinePragmas: true), - new ErrorSink()); - var codeGenerator = new CSharpCodeGenerator(codeGeneratorContext); - host.ChunkInheritanceUtility = chunkInheritanceUtility; + pathNormalizer: new DesignTimeRazorPathNormalizer(rootedAppPath)); - // Act - host.DecorateCodeGenerator(codeGenerator, codeGeneratorContext); + var chunkInheritanceUtility = new PathValidatingChunkInheritanceUtility(host, chunkTreeCache); + var codeGeneratorContext = new CodeGeneratorContext( + new ChunkGeneratorContext( + host, + host.DefaultClassName, + host.DefaultNamespace, + rootedFilePath, + shouldGenerateLinePragmas: true), + new ErrorSink()); + var codeGenerator = new CSharpCodeGenerator(codeGeneratorContext); + host.ChunkInheritanceUtility = chunkInheritanceUtility; - // Assert - Assert.Equal("src/file.cshtml", chunkInheritanceUtility.InheritedChunkTreePagePath, StringComparer.Ordinal); - } + // Act + host.DecorateCodeGenerator(codeGenerator, codeGeneratorContext); + + // Assert + Assert.Equal("src/file.cshtml", chunkInheritanceUtility.InheritedChunkTreePagePath, StringComparer.Ordinal); } [Fact] @@ -113,14 +111,15 @@ namespace Microsoft.AspNetCore.Mvc.Razor { // Arrange var fileProvider = new TestFileProvider(); - using (var host = new MvcRazorHost(new DefaultChunkTreeCache(fileProvider), new TagHelperDescriptorResolver(designTime: false))) - { - // Act - var instrumented = host.EnableInstrumentation; + var host = new MvcRazorHost( + new DefaultChunkTreeCache(fileProvider), + new TagHelperDescriptorResolver(designTime: false)); - // Assert - Assert.True(instrumented); - } + // Act + var instrumented = host.EnableInstrumentation; + + // Assert + Assert.True(instrumented); } [Fact] @@ -128,53 +127,52 @@ namespace Microsoft.AspNetCore.Mvc.Razor { // Arrange var fileProvider = new TestFileProvider(); - using (var host = new MvcRazorHostWithNormalizedNewLine(new DefaultChunkTreeCache(fileProvider)) + var host = new MvcRazorHostWithNormalizedNewLine(new DefaultChunkTreeCache(fileProvider)) { DesignTimeMode = true - }) - { - var expectedLineMappings = new[] - { - BuildLineMapping( - documentAbsoluteIndex: 33, - documentLineIndex: 2, - documentCharacterIndex: 14, - generatedAbsoluteIndex: 654, - generatedLineIndex: 17, - generatedCharacterIndex: 48, - contentLength: 91), - BuildLineMapping( - documentAbsoluteIndex: 7, - documentLineIndex: 0, - documentCharacterIndex: 7, - generatedAbsoluteIndex: 836, - generatedLineIndex: 19, - generatedCharacterIndex: 28, - contentLength: 8), - BuildLineMapping( - documentAbsoluteIndex: 145, - documentLineIndex: 4, - documentCharacterIndex: 17, - generatedAbsoluteIndex: 2530, - generatedLineIndex: 50, - generatedCharacterIndex: 133, - contentLength: 3), - BuildLineMapping( - documentAbsoluteIndex: 172, - documentLineIndex: 5, - documentCharacterIndex: 18, - generatedAbsoluteIndex: 2897, - generatedLineIndex: 56, - generatedCharacterIndex: 125, - contentLength: 5), - }; + }; - // Act and Assert - RunDesignTimeTest( - host, - testName: "ModelExpressionTagHelper", - expectedLineMappings: expectedLineMappings); - } + var expectedLineMappings = new[] + { + BuildLineMapping( + documentAbsoluteIndex: 33, + documentLineIndex: 2, + documentCharacterIndex: 14, + generatedAbsoluteIndex: 654, + generatedLineIndex: 17, + generatedCharacterIndex: 48, + contentLength: 91), + BuildLineMapping( + documentAbsoluteIndex: 7, + documentLineIndex: 0, + documentCharacterIndex: 7, + generatedAbsoluteIndex: 836, + generatedLineIndex: 19, + generatedCharacterIndex: 28, + contentLength: 8), + BuildLineMapping( + documentAbsoluteIndex: 145, + documentLineIndex: 4, + documentCharacterIndex: 17, + generatedAbsoluteIndex: 2530, + generatedLineIndex: 50, + generatedCharacterIndex: 133, + contentLength: 3), + BuildLineMapping( + documentAbsoluteIndex: 172, + documentLineIndex: 5, + documentCharacterIndex: 18, + generatedAbsoluteIndex: 2897, + generatedLineIndex: 56, + generatedCharacterIndex: 125, + contentLength: 5), + }; + + // Act and Assert + RunDesignTimeTest( + host, + testName: "ModelExpressionTagHelper", + expectedLineMappings: expectedLineMappings); } [Theory] @@ -189,11 +187,10 @@ namespace Microsoft.AspNetCore.Mvc.Razor { // Arrange var fileProvider = new TestFileProvider(); - using (var host = new TestMvcRazorHost(new DefaultChunkTreeCache(fileProvider))) - { - // Act and Assert - RunRuntimeTest(host, scenarioName); - } + var host = new TestMvcRazorHost(new DefaultChunkTreeCache(fileProvider)); + + // Act and Assert + RunRuntimeTest(host, scenarioName); } [Fact] @@ -201,36 +198,35 @@ namespace Microsoft.AspNetCore.Mvc.Razor { // Arrange var fileProvider = new TestFileProvider(); - using (var host = new MvcRazorHostWithNormalizedNewLine(new DefaultChunkTreeCache(fileProvider)) + var host = new MvcRazorHostWithNormalizedNewLine(new DefaultChunkTreeCache(fileProvider)) { DesignTimeMode = true - }) + }; + + host.NamespaceImports.Clear(); + var expectedLineMappings = new[] { - host.NamespaceImports.Clear(); - var expectedLineMappings = new[] - { - BuildLineMapping( - documentAbsoluteIndex: 13, - documentLineIndex: 0, - documentCharacterIndex: 13, - generatedAbsoluteIndex: 1499, - generatedLineIndex: 34, - generatedCharacterIndex: 13, - contentLength: 4), - BuildLineMapping( - documentAbsoluteIndex: 43, - documentLineIndex: 2, - documentCharacterIndex: 5, - generatedAbsoluteIndex: 1583, - generatedLineIndex: 39, - generatedCharacterIndex: 6, - contentLength: 21), - }; + BuildLineMapping( + documentAbsoluteIndex: 13, + documentLineIndex: 0, + documentCharacterIndex: 13, + generatedAbsoluteIndex: 1499, + generatedLineIndex: 34, + generatedCharacterIndex: 13, + contentLength: 4), + BuildLineMapping( + documentAbsoluteIndex: 43, + documentLineIndex: 2, + documentCharacterIndex: 5, + generatedAbsoluteIndex: 1583, + generatedLineIndex: 39, + generatedCharacterIndex: 6, + contentLength: 21), + }; - // Act and Assert - RunDesignTimeTest(host, "Basic", expectedLineMappings); - } + // Act and Assert + RunDesignTimeTest(host, "Basic", expectedLineMappings); } [Fact] @@ -238,27 +234,26 @@ namespace Microsoft.AspNetCore.Mvc.Razor { // Arrange var fileProvider = new TestFileProvider(); - using (var host = new MvcRazorHostWithNormalizedNewLine(new DefaultChunkTreeCache(fileProvider)) + var host = new MvcRazorHostWithNormalizedNewLine(new DefaultChunkTreeCache(fileProvider)) { DesignTimeMode = true - }) - { - host.NamespaceImports.Clear(); - var expectedLineMappings = new[] - { - BuildLineMapping( - documentAbsoluteIndex: 8, - documentLineIndex: 0, - documentCharacterIndex: 8, - generatedAbsoluteIndex: 666, - generatedLineIndex: 21, - generatedCharacterIndex: 8, - contentLength: 26), - }; + }; - // Act and Assert - RunDesignTimeTest(host, "_ViewImports", expectedLineMappings); - } + host.NamespaceImports.Clear(); + var expectedLineMappings = new[] + { + BuildLineMapping( + documentAbsoluteIndex: 8, + documentLineIndex: 0, + documentCharacterIndex: 8, + generatedAbsoluteIndex: 666, + generatedLineIndex: 21, + generatedCharacterIndex: 8, + contentLength: 26), + }; + + // Act and Assert + RunDesignTimeTest(host, "_ViewImports", expectedLineMappings); } [Fact] @@ -266,35 +261,34 @@ namespace Microsoft.AspNetCore.Mvc.Razor { // Arrange var fileProvider = new TestFileProvider(); - using (var host = new MvcRazorHostWithNormalizedNewLine(new DefaultChunkTreeCache(fileProvider)) + var host = new MvcRazorHostWithNormalizedNewLine(new DefaultChunkTreeCache(fileProvider)) { DesignTimeMode = true - }) - { - host.NamespaceImports.Clear(); - var expectedLineMappings = new[] - { - BuildLineMapping( - documentAbsoluteIndex: 1, - documentLineIndex: 0, - documentCharacterIndex: 1, - generatedAbsoluteIndex: 66, - generatedLineIndex: 3, - generatedCharacterIndex: 0, - contentLength: 17), - BuildLineMapping( - documentAbsoluteIndex: 28, - documentLineIndex: 1, - documentCharacterIndex: 8, - generatedAbsoluteIndex: 711, - generatedLineIndex: 26, - generatedCharacterIndex: 8, - contentLength: 20), - }; + }; - // Act and Assert - RunDesignTimeTest(host, "Inject", expectedLineMappings); - } + host.NamespaceImports.Clear(); + var expectedLineMappings = new[] + { + BuildLineMapping( + documentAbsoluteIndex: 1, + documentLineIndex: 0, + documentCharacterIndex: 1, + generatedAbsoluteIndex: 66, + generatedLineIndex: 3, + generatedCharacterIndex: 0, + contentLength: 17), + BuildLineMapping( + documentAbsoluteIndex: 28, + documentLineIndex: 1, + documentCharacterIndex: 8, + generatedAbsoluteIndex: 711, + generatedLineIndex: 26, + generatedCharacterIndex: 8, + contentLength: 20), + }; + + // Act and Assert + RunDesignTimeTest(host, "Inject", expectedLineMappings); } [Fact] @@ -302,43 +296,42 @@ namespace Microsoft.AspNetCore.Mvc.Razor { // Arrange var fileProvider = new TestFileProvider(); - using (var host = new MvcRazorHostWithNormalizedNewLine(new DefaultChunkTreeCache(fileProvider)) + var host = new MvcRazorHostWithNormalizedNewLine(new DefaultChunkTreeCache(fileProvider)) { DesignTimeMode = true - }) - { - host.NamespaceImports.Clear(); - var expectedLineMappings = new[] - { - BuildLineMapping( - documentAbsoluteIndex: 7, - documentLineIndex: 0, - documentCharacterIndex: 7, - generatedAbsoluteIndex: 397, - generatedLineIndex: 11, - generatedCharacterIndex: 28, - contentLength: 7), - BuildLineMapping( - documentAbsoluteIndex: 24, - documentLineIndex: 1, - documentCharacterIndex: 8, - generatedAbsoluteIndex: 760, - generatedLineIndex: 25, - generatedCharacterIndex: 8, - contentLength: 20), - BuildLineMapping( - documentAbsoluteIndex: 54, - documentLineIndex: 2, - documentCharacterIndex: 8, - generatedAbsoluteIndex: 990, - generatedLineIndex: 33, - generatedCharacterIndex: 8, - contentLength: 23), - }; + }; - // Act and Assert - RunDesignTimeTest(host, "InjectWithModel", expectedLineMappings); - } + host.NamespaceImports.Clear(); + var expectedLineMappings = new[] + { + BuildLineMapping( + documentAbsoluteIndex: 7, + documentLineIndex: 0, + documentCharacterIndex: 7, + generatedAbsoluteIndex: 397, + generatedLineIndex: 11, + generatedCharacterIndex: 28, + contentLength: 7), + BuildLineMapping( + documentAbsoluteIndex: 24, + documentLineIndex: 1, + documentCharacterIndex: 8, + generatedAbsoluteIndex: 760, + generatedLineIndex: 25, + generatedCharacterIndex: 8, + contentLength: 20), + BuildLineMapping( + documentAbsoluteIndex: 54, + documentLineIndex: 2, + documentCharacterIndex: 8, + generatedAbsoluteIndex: 990, + generatedLineIndex: 33, + generatedCharacterIndex: 8, + contentLength: 23), + }; + + // Act and Assert + RunDesignTimeTest(host, "InjectWithModel", expectedLineMappings); } [Fact] @@ -346,59 +339,58 @@ namespace Microsoft.AspNetCore.Mvc.Razor { // Arrange var fileProvider = new TestFileProvider(); - using (var host = new MvcRazorHostWithNormalizedNewLine(new DefaultChunkTreeCache(fileProvider)) + var host = new MvcRazorHostWithNormalizedNewLine(new DefaultChunkTreeCache(fileProvider)) { DesignTimeMode = true - }) - { - host.NamespaceImports.Clear(); - var expectedLineMappings = new[] - { - BuildLineMapping( - documentAbsoluteIndex: 7, - documentLineIndex: 0, - documentCharacterIndex: 7, - generatedAbsoluteIndex: 405, - generatedLineIndex: 11, - generatedCharacterIndex: 28, - contentLength: 7), - BuildLineMapping( - documentAbsoluteIndex: 24, - documentLineIndex: 1, - documentCharacterIndex: 8, - generatedAbsoluteIndex: 776, - generatedLineIndex: 25, - generatedCharacterIndex: 8, - contentLength: 20), - BuildLineMapping( - documentAbsoluteIndex: 58, - documentLineIndex: 2, - documentCharacterIndex: 8, - generatedAbsoluteIndex: 1010, - generatedLineIndex: 33, - generatedCharacterIndex: 8, - contentLength: 23), - BuildLineMapping( - documentAbsoluteIndex: 93, - documentLineIndex: 3, - documentCharacterIndex: 8, - generatedAbsoluteIndex: 1247, - generatedLineIndex: 41, - generatedCharacterIndex: 8, - contentLength: 21), - BuildLineMapping( - documentAbsoluteIndex: 129, - documentLineIndex: 4, - documentCharacterIndex: 8, - generatedAbsoluteIndex: 1482, - generatedLineIndex: 49, - generatedCharacterIndex: 8, - contentLength: 24), - }; + }; - // Act and Assert - RunDesignTimeTest(host, "InjectWithSemicolon", expectedLineMappings); - } + host.NamespaceImports.Clear(); + var expectedLineMappings = new[] + { + BuildLineMapping( + documentAbsoluteIndex: 7, + documentLineIndex: 0, + documentCharacterIndex: 7, + generatedAbsoluteIndex: 405, + generatedLineIndex: 11, + generatedCharacterIndex: 28, + contentLength: 7), + BuildLineMapping( + documentAbsoluteIndex: 24, + documentLineIndex: 1, + documentCharacterIndex: 8, + generatedAbsoluteIndex: 776, + generatedLineIndex: 25, + generatedCharacterIndex: 8, + contentLength: 20), + BuildLineMapping( + documentAbsoluteIndex: 58, + documentLineIndex: 2, + documentCharacterIndex: 8, + generatedAbsoluteIndex: 1010, + generatedLineIndex: 33, + generatedCharacterIndex: 8, + contentLength: 23), + BuildLineMapping( + documentAbsoluteIndex: 93, + documentLineIndex: 3, + documentCharacterIndex: 8, + generatedAbsoluteIndex: 1247, + generatedLineIndex: 41, + generatedCharacterIndex: 8, + contentLength: 21), + BuildLineMapping( + documentAbsoluteIndex: 129, + documentLineIndex: 4, + documentCharacterIndex: 8, + generatedAbsoluteIndex: 1482, + generatedLineIndex: 49, + generatedCharacterIndex: 8, + contentLength: 24), + }; + + // Act and Assert + RunDesignTimeTest(host, "InjectWithSemicolon", expectedLineMappings); } [Fact] @@ -406,27 +398,25 @@ namespace Microsoft.AspNetCore.Mvc.Razor { // Arrange var fileProvider = new TestFileProvider(); - using (var host = new MvcRazorHostWithNormalizedNewLine(new DefaultChunkTreeCache(fileProvider)) + var host = new MvcRazorHostWithNormalizedNewLine(new DefaultChunkTreeCache(fileProvider)) { DesignTimeMode = true - }) + }; + host.NamespaceImports.Clear(); + var expectedLineMappings = new[] { - host.NamespaceImports.Clear(); - var expectedLineMappings = new[] - { - BuildLineMapping( - documentAbsoluteIndex: 7, - documentLineIndex: 0, - documentCharacterIndex: 7, - generatedAbsoluteIndex: 400, - generatedLineIndex: 11, - generatedCharacterIndex: 28, - contentLength: 30), - }; + BuildLineMapping( + documentAbsoluteIndex: 7, + documentLineIndex: 0, + documentCharacterIndex: 7, + generatedAbsoluteIndex: 400, + generatedLineIndex: 11, + generatedCharacterIndex: 28, + contentLength: 30), + }; - // Act and Assert - RunDesignTimeTest(host, "Model", expectedLineMappings); - } + // Act and Assert + RunDesignTimeTest(host, "Model", expectedLineMappings); } [Fact] @@ -434,33 +424,32 @@ namespace Microsoft.AspNetCore.Mvc.Razor { // Arrange var fileProvider = new TestFileProvider(); - using (var host = new MvcRazorHostWithNormalizedNewLine(new DefaultChunkTreeCache(fileProvider)) + var host = new MvcRazorHostWithNormalizedNewLine(new DefaultChunkTreeCache(fileProvider)) { DesignTimeMode = true - }) + }; + + host.NamespaceImports.Clear(); + var inputFile = "TestFiles/Input/MultipleModels.cshtml"; + var outputFile = "TestFiles/Output/DesignTime/MultipleModels.cs"; + var expectedCode = ResourceFile.ReadResource(_assembly, outputFile, sourceFile: false); + + // Act + GeneratorResults results; + using (var stream = ResourceFile.GetResourceStream(_assembly, inputFile, sourceFile: true)) { - host.NamespaceImports.Clear(); - var inputFile = "TestFiles/Input/MultipleModels.cshtml"; - var outputFile = "TestFiles/Output/DesignTime/MultipleModels.cs"; - var expectedCode = ResourceFile.ReadResource(_assembly, outputFile, sourceFile: false); - - // Act - GeneratorResults results; - using (var stream = ResourceFile.GetResourceStream(_assembly, inputFile, sourceFile: true)) - { - results = host.GenerateCode(inputFile, stream); - } - - // Assert - Assert.False(results.Success); - var parserError = Assert.Single(results.ParserErrors); - Assert.Equal("Only one 'model' statement is allowed in a file.", parserError.Message); -#if GENERATE_BASELINES - ResourceFile.UpdateFile(_assembly, outputFile, expectedCode, results.GeneratedCode); -#else - Assert.Equal(expectedCode, results.GeneratedCode, ignoreLineEndingDifferences: true); -#endif + results = host.GenerateCode(inputFile, stream); } + + // Assert + Assert.False(results.Success); + var parserError = Assert.Single(results.ParserErrors); + Assert.Equal("Only one 'model' statement is allowed in a file.", parserError.Message); +#if GENERATE_BASELINES + ResourceFile.UpdateFile(_assembly, outputFile, expectedCode, results.GeneratedCode); +#else + Assert.Equal(expectedCode, results.GeneratedCode, ignoreLineEndingDifferences: true); +#endif } private static void RunRuntimeTest(