コード例 #1
0
 /// <summary>
 /// Initializes a new instance of <see cref="ChunkInheritanceUtility"/>.
 /// </summary>
 /// <param name="razorHost">The <see cref="MvcRazorHost"/> used to parse <c>_ViewImports</c> pages.</param>
 /// <param name="codeTreeCache"><see cref="ICodeTreeCache"/> that caches <see cref="CodeTree"/> instances.
 /// </param>
 /// <param name="defaultInheritedChunks">Sequence of <see cref="Chunk"/>s inherited by default.</param>
 public ChunkInheritanceUtility([NotNull] MvcRazorHost razorHost,
                                [NotNull] ICodeTreeCache codeTreeCache,
                                [NotNull] IReadOnlyList <Chunk> defaultInheritedChunks)
 {
     _razorHost = razorHost;
     _defaultInheritedChunks = defaultInheritedChunks;
     _codeTreeCache          = codeTreeCache;
 }
コード例 #2
0
 /// <summary>
 /// Initializes a new instance of <see cref="ChunkInheritanceUtility"/>.
 /// </summary>
 /// <param name="razorHost">The <see cref="MvcRazorHost"/> used to parse <c>_GlobalImport</c> pages.</param>
 /// <param name="codeTreeCache"><see cref="ICodeTreeCache"/> that caches <see cref="CodeTree"/> instances.
 /// </param>
 /// <param name="defaultInheritedChunks">Sequence of <see cref="Chunk"/>s inherited by default.</param>
 public ChunkInheritanceUtility([NotNull] MvcRazorHost razorHost,
                                [NotNull] ICodeTreeCache codeTreeCache,
                                [NotNull] IReadOnlyList<Chunk> defaultInheritedChunks)
 {
     _razorHost = razorHost;
     _defaultInheritedChunks = defaultInheritedChunks;
     _codeTreeCache = codeTreeCache;
 }
コード例 #3
0
 /// <summary>
 /// Initializes a new instance of <see cref="ChunkInheritanceUtility"/>.
 /// </summary>
 /// <param name="razorHost">The <see cref="MvcRazorHost"/> used to parse _ViewStart pages.</param>
 /// <param name="fileProvider">The fileProvider that represents the application.</param>
 /// <param name="defaultInheritedChunks">Sequence of <see cref="Chunk"/>s inherited by default.</param>
 public ChunkInheritanceUtility([NotNull] MvcRazorHost razorHost,
                                [NotNull] IFileProvider fileProvider,
                                [NotNull] IReadOnlyList <Chunk> defaultInheritedChunks)
 {
     _razorHost              = razorHost;
     _fileProvider           = fileProvider;
     _defaultInheritedChunks = defaultInheritedChunks;
     _parsedCodeTrees        = new Dictionary <string, CodeTree>(StringComparer.Ordinal);
 }
コード例 #4
0
 /// <summary>
 /// Initializes a new instance of <see cref="ChunkInheritanceUtility"/>.
 /// </summary>
 /// <param name="razorHost">The <see cref="MvcRazorHost"/> used to parse _ViewStart pages.</param>
 /// <param name="fileSystem">The filesystem that represents the application.</param>
 /// <param name="defaultInheritedChunks">Sequence of <see cref="Chunk"/>s inherited by default.</param>
 public ChunkInheritanceUtility([NotNull] MvcRazorHost razorHost,
                                [NotNull] IFileSystem fileSystem,
                                [NotNull] IEnumerable <Chunk> defaultInheritedChunks)
 {
     _razorHost              = razorHost;
     _fileSystem             = fileSystem;
     _defaultInheritedChunks = defaultInheritedChunks;
     _parsedCodeTrees        = new Dictionary <string, CodeTree>(StringComparer.Ordinal);
 }
コード例 #5
0
        public void MergeInheritedChunks_MergesDefaultInheritedChunks()
        {
            // Arrange
            var fileProvider = new TestFileProvider();

            fileProvider.AddFile(@"/Views/_ViewImports.cshtml",
                                 "@inject DifferentHelper<TModel> Html");
            var cache = new DefaultChunkTreeCache(fileProvider);

            using (var host = new MvcRazorHost(cache))
            {
                var defaultChunks = new Chunk[]
                {
                    new InjectChunk("MyTestHtmlHelper", "Html"),
                    new UsingChunk {
                        Namespace = "AppNamespace.Model"
                    },
                };
                var inheritedChunkTrees = new ChunkTree[]
                {
                    new ChunkTree
                    {
                        Children = new Chunk[]
                        {
                            new UsingChunk {
                                Namespace = "InheritedNamespace"
                            },
                            new LiteralChunk {
                                Text = "some text"
                            }
                        }
                    },
                    new ChunkTree
                    {
                        Children = 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.Collection(chunkTree.Children,
                                  chunk => Assert.Same(defaultChunks[1], chunk),
                                  chunk => Assert.Same(inheritedChunkTrees[0].Children[0], chunk),
                                  chunk => Assert.Same(defaultChunks[0], chunk));
            }
        }
コード例 #6
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]);
        }
コード例 #7
0
        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]);
        }
コード例 #8
0
        public void GetInheritedChunks_ReadsChunksFromViewStartsInPath()
        {
            // Arrange
            var fileSystem = new TestFileSystem();

            fileSystem.AddFile(@"x:\myapproot\views\accounts\_viewstart.cshtml", "@using AccountModels");
            fileSystem.AddFile(@"x:\myapproot\views\Shared\_viewstart.cshtml", "@inject SharedHelper Shared");
            fileSystem.AddFile(@"x:\myapproot\views\home\_viewstart.cshtml", "@using MyNamespace");
            fileSystem.AddFile(@"x:\myapproot\views\_viewstart.cshtml",
                               @"@inject MyHelper<TModel> Helper
@inherits MyBaseType

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

");
            var host    = new MvcRazorHost(fileSystem);
            var utility = new ChunkInheritanceUtility(host, fileSystem, new Chunk[0]);

            // Act
            var chunks = utility.GetInheritedChunks(@"x:\myapproot\views\home\Index.cshtml");

            // Assert
            Assert.Equal(8, chunks.Count);
            Assert.IsType <LiteralChunk>(chunks[0]);

            var usingChunk = Assert.IsType <UsingChunk>(chunks[1]);

            Assert.Equal("MyNamespace", usingChunk.Namespace);

            Assert.IsType <LiteralChunk>(chunks[2]);
            Assert.IsType <LiteralChunk>(chunks[3]);

            var injectChunk = Assert.IsType <InjectChunk>(chunks[4]);

            Assert.Equal("MyHelper<TModel>", injectChunk.TypeName);
            Assert.Equal("Helper", injectChunk.MemberName);

            var setBaseTypeChunk = Assert.IsType <SetBaseTypeChunk>(chunks[5]);

            Assert.Equal("MyBaseType", setBaseTypeChunk.TypeName);

            Assert.IsType <StatementChunk>(chunks[6]);
            Assert.IsType <LiteralChunk>(chunks[7]);
        }
コード例 #9
0
        public void GetInheritedChunks_ReturnsEmptySequenceIfNoViewStartsArePresent()
        {
            // Arrange
            var fileSystem = new TestFileSystem();

            fileSystem.AddFile(@"x:\myapproot\_viewstart.cs", string.Empty);
            fileSystem.AddFile(@"x:\myapproot\views\_Layout.cshtml", string.Empty);
            fileSystem.AddFile(@"x:\myapproot\views\home\_not-viewstart.cshtml", string.Empty);
            var host    = new MvcRazorHost(fileSystem);
            var utility = new ChunkInheritanceUtility(host, fileSystem, new Chunk[0]);

            // Act
            var chunks = utility.GetInheritedChunks(@"x:\myapproot\views\home\Index.cshtml");

            // Assert
            Assert.Empty(chunks);
        }
コード例 #10
0
        public void GetInheritedChunks_ReturnsDefaultInheritedChunks()
        {
            // Arrange
            var fileSystem = new TestFileSystem();

            fileSystem.AddFile(@"x:\myapproot\views\_viewstart.cshtml",
                               @"@inject DifferentHelper<TModel> Html
@using AppNamespace.Models
@{
    Layout = ""test.cshtml"";
}

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

            // Act
            var chunks = utility.GetInheritedChunks(host,
                                                    fileSystem,
                                                    @"x:\myapproot\views\home\Index.cshtml");

            // Assert
            Assert.Equal(4, chunks.Count);
            var injectChunk = Assert.IsType <InjectChunk>(chunks[0]);

            Assert.Equal("DifferentHelper<TModel>", injectChunk.TypeName);
            Assert.Equal("Html", injectChunk.MemberName);

            var usingChunk = Assert.IsType <UsingChunk>(chunks[1]);

            Assert.Equal("AppNamespace.Models", usingChunk.Namespace);

            injectChunk = Assert.IsType <InjectChunk>(chunks[2]);
            Assert.Equal("MyTestHtmlHelper", injectChunk.TypeName);
            Assert.Equal("Html", injectChunk.MemberName);

            usingChunk = Assert.IsType <UsingChunk>(chunks[3]);
            Assert.Equal("AppNamespace.Model", usingChunk.Namespace);
        }
コード例 #11
0
        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);
        }
コード例 #12
0
        private static void GenerateCodeFile(string file, string @namespace, int iterations, bool dump)
        {
            var basePath = Path.GetDirectoryName(file);
            var fileName = Path.GetFileName(file);

            var fileNameNoExtension = Path.GetFileNameWithoutExtension(fileName);
            var codeLang            = new CSharpRazorCodeLanguage();

            var host   = new MvcRazorHost(new DefaultChunkTreeCache(new PhysicalFileProvider(basePath)));
            var engine = new RazorTemplateEngine(host);

            Console.WriteLine("Press the ANY key to start.");
            Console.ReadLine();

            Console.WriteLine($"Starting Code Generation: {file}");
            var timer = Stopwatch.StartNew();

            for (var i = 0; i < iterations; i++)
            {
                using (var fileStream = File.OpenText(file))
                {
                    var code = engine.GenerateCode(
                        input: fileStream,
                        className: fileNameNoExtension,
                        rootNamespace: Path.GetFileName(@namespace),
                        sourceFileName: fileName);

                    if (dump)
                    {
                        File.WriteAllText(Path.ChangeExtension(file, ".cs"), code.GeneratedCode);
                    }
                }

                Console.WriteLine("Completed iteration: " + (i + 1));
            }
            Console.WriteLine($"Completed after {timer.Elapsed}");
        }
コード例 #13
0
ファイル: RazorTests.cs プロジェクト: delixfe/Performance
        public void ViewParsing(bool designTime)
        {
            // Arrange
            var chunkTreeCache = new DefaultChunkTreeCache(new TestFileProvider());
            var razorHost      = new MvcRazorHost(chunkTreeCache)
            {
                DesignTimeMode = designTime
            };
            var assembly     = typeof(RazorTests).GetTypeInfo().Assembly;
            var assemblyName = assembly.GetName().Name;
            var stream       = assembly.GetManifestResourceStream($"{assemblyName}.compiler.resources.RazorTests.TestFile.cshtml");
            GeneratorResults result;

            // Act
            using (Collector.StartCollection())
            {
                result = razorHost.GenerateCode("test/path", stream);
            }

            // Assert
            Assert.Empty(result.ErrorSink.Errors);
            Assert.Empty(result.ParserErrors);
            Assert.True(result.Success);
        }
コード例 #14
0
        /// <summary>
        /// Gets the list of chunks that are to be inherited by a specified page.
        /// Chunks are inherited from _ViewStarts that are applicable to the page.
        /// </summary>
        /// <param name="razorHost">The <see cref="MvcRazorHost"/> used to parse _ViewStart pages.</param>
        /// <param name="fileSystem">The filesystem that represents the application.</param>
        /// <param name="pagePath">The path of the page to locate inherited chunks for.</param>
        /// <returns>A list of chunks that are applicable to the given page.</returns>
        public List <Chunk> GetInheritedChunks([NotNull] MvcRazorHost razorHost,
                                               [NotNull] IFileSystem fileSystem,
                                               [NotNull] string pagePath)
        {
            var inheritedChunks = new List <Chunk>();

            var templateEngine = new RazorTemplateEngine(razorHost);

            foreach (var viewStart in ViewStartUtility.GetViewStartLocations(fileSystem, pagePath))
            {
                IFileInfo fileInfo;
                if (fileSystem.TryGetFileInfo(viewStart, out fileInfo))
                {
                    var parsedTree  = ParseViewFile(templateEngine, fileInfo);
                    var chunksToAdd = parsedTree.Chunks
                                      .Where(chunk => ChunkMergers.ContainsKey(chunk.GetType()));
                    inheritedChunks.AddRange(chunksToAdd);
                }
            }

            inheritedChunks.AddRange(_defaultInheritedChunks);

            return(inheritedChunks);
        }
コード例 #15
0
        public void GetInheritedChunks_ReturnsEmptySequenceIfNoViewStartsArePresent()
        {
            // Arrange
            var fileProvider = new TestFileProvider();

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

            // Act
            var codeTrees = utility.GetInheritedCodeTrees(@"Views\home\Index.cshtml");

            // Assert
            Assert.Empty(codeTrees);
        }
コード例 #16
0
        /// <summary>
        /// Initializes a new instance of <see cref="ChunkInheritanceUtility"/>.
        /// </summary>
        /// <param name="razorHost">The <see cref="MvcRazorHost"/> used to parse <c>_ViewImports</c> pages.</param>
        /// <param name="chunkTreeCache"><see cref="IChunkTreeCache"/> that caches <see cref="ChunkTree"/> instances.
        /// </param>
        /// <param name="defaultInheritedChunks">Sequence of <see cref="Chunk"/>s inherited by default.</param>
        public ChunkInheritanceUtility(
            MvcRazorHost razorHost,
            IChunkTreeCache chunkTreeCache,
            IReadOnlyList<Chunk> defaultInheritedChunks)
        {
            if (razorHost == null)
            {
                throw new ArgumentNullException(nameof(razorHost));
            }

            if (chunkTreeCache == null)
            {
                throw new ArgumentNullException(nameof(chunkTreeCache));
            }

            if (defaultInheritedChunks == null)
            {
                throw new ArgumentNullException(nameof(defaultInheritedChunks));
            }

            _razorHost = razorHost;
            _defaultInheritedChunks = defaultInheritedChunks;
            _chunkTreeCache = chunkTreeCache;
        }
コード例 #17
0
        /// <summary>
        /// Initializes a new instance of <see cref="ChunkInheritanceUtility"/>.
        /// </summary>
        /// <param name="razorHost">The <see cref="MvcRazorHost"/> used to parse <c>_ViewImports</c> pages.</param>
        /// <param name="chunkTreeCache"><see cref="IChunkTreeCache"/> that caches <see cref="ChunkTree"/> instances.
        /// </param>
        /// <param name="defaultInheritedChunks">Sequence of <see cref="Chunk"/>s inherited by default.</param>
        public ChunkInheritanceUtility(
            MvcRazorHost razorHost,
            IChunkTreeCache chunkTreeCache,
            IReadOnlyList <Chunk> defaultInheritedChunks)
        {
            if (razorHost == null)
            {
                throw new ArgumentNullException(nameof(razorHost));
            }

            if (chunkTreeCache == null)
            {
                throw new ArgumentNullException(nameof(chunkTreeCache));
            }

            if (defaultInheritedChunks == null)
            {
                throw new ArgumentNullException(nameof(defaultInheritedChunks));
            }

            _razorHost = razorHost;
            _defaultInheritedChunks = defaultInheritedChunks;
            _chunkTreeCache         = chunkTreeCache;
        }
コード例 #18
0
        public void MergeInheritedChunks_MergesDefaultInheritedChunks()
        {
            // Arrange
            var fileProvider = new TestFileProvider();
            fileProvider.AddFile(@"/Views/_ViewImports.cshtml",
                               "@inject DifferentHelper<TModel> Html");
            var cache = new DefaultChunkTreeCache(fileProvider);
            using (var host = new MvcRazorHost(cache, new TagHelperDescriptorResolver(designTime: false)))
            {
                var defaultChunks = new Chunk[]
                {
                new InjectChunk("MyTestHtmlHelper", "Html"),
                new UsingChunk { Namespace = "AppNamespace.Model" },
                };
                var inheritedChunkTrees = new ChunkTree[]
                {
                new ChunkTree
                {
                    Children = new Chunk[]
                    {
                        new UsingChunk { Namespace = "InheritedNamespace" },
                        new LiteralChunk { Text = "some text" }
                    }
                },
                new ChunkTree
                {
                    Children = 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.Collection(chunkTree.Children,
                    chunk => Assert.Same(defaultChunks[1], chunk),
                    chunk => Assert.Same(inheritedChunkTrees[0].Children[0], chunk),
                    chunk => Assert.Same(defaultChunks[0], chunk));
            }
        }
コード例 #19
0
        public void MergeInheritedChunks_MergesDefaultInheritedChunks()
        {
            // Arrange
            var fileProvider = new TestFileProvider();
            fileProvider.AddFile(@"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]);
        }
コード例 #20
0
 public MyRazorTemplateEngine(MyMvcRazorHost myHost, MvcRazorHost host) : base(host)
 {
     _myHost = myHost;
 }
コード例 #21
0
        public void GetInheritedChunks_ReturnsEmptySequenceIfNoGlobalsArePresent()
        {
            // Arrange
            var fileProvider = new TestFileProvider();
            fileProvider.AddFile(@"/_ViewImports.cs", string.Empty);
            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 defaultChunks = new Chunk[]
                {
                new InjectChunk("MyTestHtmlHelper", "Html"),
                new UsingChunk { Namespace = "AppNamespace.Model" },
                };
                var utility = new ChunkInheritanceUtility(host, cache, defaultChunks);

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

                // Assert
                Assert.Empty(chunkTrees);
            }
        }
コード例 #22
0
        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 DefaultChunkTreeCache(fileProvider);
            using (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"));

                // Assert
                Assert.Collection(chunkTreeResults,
                    chunkTreeResult =>
                    {
                        var viewImportsPath = @"/Views/_ViewImports.cshtml";
                        Assert.Collection(chunkTreeResult.ChunkTree.Children,
                            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);
                            });
                        Assert.Equal(viewImportsPath, chunkTreeResult.FilePath);
                    },
                    chunkTreeResult =>
                    {
                        var viewImportsPath = "/Views/home/_ViewImports.cshtml";
                        Assert.Collection(chunkTreeResult.ChunkTree.Children,
                            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);
                            });
                        Assert.Equal(viewImportsPath, chunkTreeResult.FilePath);
                    });
            }
        }
コード例 #23
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);
                });
            });
        }
 public PreMinifyingMvcRazorHost(IChunkTreeCache chunkTreeCache, ITagHelperDescriptorResolver descriptorResolver)
 {
     _host = new MvcRazorHost(chunkTreeCache, descriptorResolver);
 }
 public CustomRazorTemplateEngine(ChunkVisitorMinifyingMvcRazorHost myHost, MvcRazorHost host) : base(host)
 {
     _myHost = myHost;
 }
 public ChunkVisitorMinifyingMvcRazorHost(IChunkTreeCache chunkTreeCache, ITagHelperDescriptorResolver descriptorResolver)
 {
     _chunkTreeCache = chunkTreeCache;
     _host           = new MvcRazorHost(chunkTreeCache, descriptorResolver);
 }
コード例 #27
0
        //public MyMvcRazorHost(string root)
        //{
        //    _host = new MvcRazorHost(root);
        //}

        public MyMvcRazorHost(IChunkTreeCache chunkTreeCache)
        {
            _chunkTreeCache = chunkTreeCache;
            _host           = new MvcRazorHost(chunkTreeCache);
        }