コード例 #1
0
        public void Merge_IgnoresChunkIfChunkWithMatchingPropertyNameWasVisitedInCodeTree()
        {
            // Arrange
            var merger = new InjectChunkMerger("dynamic");
            var codeTree = new CodeTree();

            // Act
            merger.VisitChunk(new InjectChunk("MyTypeA", "MyProperty"));
            merger.Merge(codeTree, new InjectChunk("MyTypeB", "MyProperty"));

            // Assert
            Assert.Empty(codeTree.Chunks);
        }
コード例 #2
0
        public void Merge_IgnoresNamespacesThatHaveBeenVisitedInCodeTree()
        {
            // Arrange
            var merger = new UsingChunkMerger();
            var codeTree = new CodeTree();

            // Act
            merger.VisitChunk(new UsingChunk { Namespace = "Microsoft.AspNet.Mvc" });
            merger.Merge(codeTree, new UsingChunk { Namespace = "Microsoft.AspNet.Mvc" });

            // Assert
            Assert.Empty(codeTree.Chunks);
        }
コード例 #3
0
        public void Merge_IgnoresSetBaseTypeChunksIfCodeTreeContainsOne()
        {
            // Arrange
            var merger = new SetBaseTypeChunkMerger("dynamic");
            var codeTree = new CodeTree();

            // Act
            merger.VisitChunk(new SetBaseTypeChunk { TypeName = "MyBaseType1" });
            merger.Merge(codeTree, new SetBaseTypeChunk { TypeName = "MyBaseType2" });

            // Assert
            Assert.Empty(codeTree.Chunks);
        }
コード例 #4
0
        public void Merge_SetsBaseTypeIfItHasNotBeenSetInCodeTree()
        {
            // Arrange
            var expected = "MyApp.Razor.MyBaseType";
            var merger = new SetBaseTypeChunkMerger("dynamic");
            var codeTree = new CodeTree();

            // Act
            merger.Merge(codeTree, new SetBaseTypeChunk { TypeName = expected });

            // Assert
            var chunk = Assert.Single(codeTree.Chunks);
            var setBaseTypeChunk = Assert.IsType<SetBaseTypeChunk>(chunk);
            Assert.Equal(expected, setBaseTypeChunk.TypeName);
        }
コード例 #5
0
        public void Merge_AddsNamespacesThatHaveNotBeenVisitedInCodeTree()
        {
            // Arrange
            var expected = "MyApp.Models";
            var merger = new UsingChunkMerger();
            var codeTree = new CodeTree();

            // Act
            merger.VisitChunk(new UsingChunk { Namespace = "Microsoft.AspNet.Mvc" });
            merger.Merge(codeTree, new UsingChunk { Namespace = expected });

            // Assert
            var chunk = Assert.Single(codeTree.Chunks);
            var usingChunk = Assert.IsType<UsingChunk>(chunk);
            Assert.Equal(expected, usingChunk.Namespace);
        }
コード例 #6
0
        public void Merge_AddsChunkIfChunkWithMatchingPropertyNameWasNotVisitedInCodeTree()
        {
            // Arrange
            var expectedType = "MyApp.MyHelperType";
            var expectedProperty = "MyHelper";
            var merger = new InjectChunkMerger("dynamic");
            var codeTree = new CodeTree();

            // Act
            merger.Merge(codeTree, new InjectChunk(expectedType, expectedProperty));

            // Assert
            var chunk = Assert.Single(codeTree.Chunks);
            var injectChunk = Assert.IsType<InjectChunk>(chunk);
            Assert.Equal(expectedType, injectChunk.TypeName);
            Assert.Equal(expectedProperty, injectChunk.MemberName);
        }
コード例 #7
0
        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());
        }
コード例 #8
0
        public void Merge_IgnoresNamespacesThatHaveBeenVisitedDuringMerge()
        {
            // Arrange
            var merger = new UsingChunkMerger();
            var codeTree = new CodeTree();

            // Act
            merger.Merge(codeTree, new UsingChunk { Namespace = "Microsoft.AspNet.Mvc" });
            merger.Merge(codeTree, new UsingChunk { Namespace = "Microsoft.AspNet.Mvc" });
            merger.Merge(codeTree, new UsingChunk { Namespace = "Microsoft.AspNet.Mvc.Razor" });

            // Assert
            Assert.Equal(2, codeTree.Chunks.Count);
            var chunk = Assert.IsType<UsingChunk>(codeTree.Chunks[0]);
            Assert.Equal("Microsoft.AspNet.Mvc", chunk.Namespace);
            chunk = Assert.IsType<UsingChunk>(codeTree.Chunks[1]);
            Assert.Equal("Microsoft.AspNet.Mvc.Razor", chunk.Namespace);
        }
コード例 #9
0
        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());
        }
コード例 #10
0
        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);
        }
コード例 #11
0
ファイル: MvcRazorParserTest.cs プロジェクト: RehanSaeed/Mvc
        public void GetTagHelperDescriptors_ReturnsExpectedDirectiveDescriptors(
            CodeTree[] codeTrees,
            TagHelperDirectiveDescriptor[] expectedDirectiveDescriptors)
        {
            // Arrange
            var builder = new BlockBuilder { Type = BlockType.Comment };
            var block = new Block(builder);

            IList<TagHelperDirectiveDescriptor> descriptors = null;
            var resolver = new Mock<ITagHelperDescriptorResolver>();
            resolver.Setup(r => r.Resolve(It.IsAny<TagHelperDescriptorResolutionContext>()))
                    .Callback((TagHelperDescriptorResolutionContext context) =>
                    {
                        descriptors = context.DirectiveDescriptors;
                    })
                    .Returns(Enumerable.Empty<TagHelperDescriptor>())
                    .Verifiable();

            var baseParser = new RazorParser(
                new CSharpCodeParser(),
                new HtmlMarkupParser(),
                tagHelperDescriptorResolver: resolver.Object);
            var parser = new TestableMvcRazorParser(baseParser, codeTrees, defaultInheritedChunks: new Chunk[0]);

            // Act
            parser.GetTagHelperDescriptorsPublic(block, errorSink: new ErrorSink()).ToArray();

            // Assert
            Assert.NotNull(descriptors);
            Assert.Equal(expectedDirectiveDescriptors.Length, descriptors.Count);

            for (var i = 0; i < expectedDirectiveDescriptors.Length; i++)
            {
                var expected = expectedDirectiveDescriptors[i];
                var actual = descriptors[i];

                Assert.Equal(expected.DirectiveText, actual.DirectiveText, StringComparer.Ordinal);
                Assert.Equal(SourceLocation.Zero, actual.Location);
                Assert.Equal(expected.DirectiveType, actual.DirectiveType);
            }
        }
コード例 #12
0
        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]);
        }
コード例 #13
0
        public void Merge_MacthesNamespacesInCaseSensitiveManner()
        {
            // Arrange
            var merger = new UsingChunkMerger();
            var codeTree = new CodeTree();

            // Act
            merger.Merge(codeTree, new UsingChunk { Namespace = "Microsoft.AspNet.Mvc" });
            merger.Merge(codeTree, new UsingChunk { Namespace = "Microsoft.AspNet.mvc" });

            // Assert
            Assert.Equal(2, codeTree.Chunks.Count);
            var chunk = Assert.IsType<UsingChunk>(codeTree.Chunks[0]);
            Assert.Equal("Microsoft.AspNet.Mvc", chunk.Namespace);
            chunk = Assert.IsType<UsingChunk>(codeTree.Chunks[1]);
            Assert.Equal("Microsoft.AspNet.mvc", chunk.Namespace);
        }
コード例 #14
0
        public void Merge_IgnoresSetBaseTypeChunksIfSetBaseTypeWasPreviouslyMerged()
        {
            // Arrange
            var merger = new SetBaseTypeChunkMerger("dynamic");
            var codeTree = new CodeTree();

            // Act
            merger.Merge(codeTree, new SetBaseTypeChunk { TypeName = "MyBase1" });
            merger.Merge(codeTree, new SetBaseTypeChunk { TypeName = "MyBase2" });

            // Assert
            var chunk = Assert.Single(codeTree.Chunks);
            var setBaseTypeChunk = Assert.IsType<SetBaseTypeChunk>(chunk);
            Assert.Equal("MyBase1", setBaseTypeChunk.TypeName);
        }
コード例 #15
0
        public void Merge_ResolvesModelNameInTypesWithTModelToken()
        {
            // Arrange
            var merger = new InjectChunkMerger("dynamic");
            var codeTree = new CodeTree();

            // Act
            merger.Merge(codeTree, new InjectChunk("MyHelper<TModel>", "MyProperty"));

            // Assert
            var chunk = Assert.Single(codeTree.Chunks);
            var injectChunk = Assert.IsType<InjectChunk>(chunk);
            Assert.Equal("MyHelper<dynamic>", injectChunk.TypeName);
            Assert.Equal("MyProperty", injectChunk.MemberName);
        }
コード例 #16
0
        public void Merge_ReplacesTModelTokensWithModel()
        {
            // Arrange
            var merger = new InjectChunkMerger("MyTestModel2");
            var codeTree = new CodeTree();

            // Act
            merger.Merge(codeTree, new InjectChunk("MyHelper<TModel>", "MyProperty"));

            // Assert
            var chunk = Assert.Single(codeTree.Chunks);
            var injectChunk = Assert.IsType<InjectChunk>(chunk);
            Assert.Equal("MyHelper<MyTestModel2>", injectChunk.TypeName);
            Assert.Equal("MyProperty", injectChunk.MemberName);
        }
コード例 #17
0
 public CodeTreeBuilder()
 {
     CodeTree    = new CodeTree();
     _blockChain = new Stack <ChunkBlock>();
 }
コード例 #18
0
        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);
        }
コード例 #19
0
        public void Merge_IgnoresChunkIfChunkWithMatchingPropertyNameWasPreviouslyMerged()
        {
            // Arrange
            var merger = new InjectChunkMerger("dynamic");
            var codeTree = new CodeTree();

            // Act
            merger.Merge(codeTree, new InjectChunk("SomeType", "Property"));
            merger.Merge(codeTree, new InjectChunk("SomeOtherType", "Property"));

            // Assert
            var chunk = Assert.Single(codeTree.Chunks);
            var injectChunk = Assert.IsType<InjectChunk>(chunk);
            Assert.Equal("SomeType", injectChunk.TypeName);
            Assert.Equal("Property", injectChunk.MemberName);
        }
コード例 #20
0
        public void Merge_MatchesPropertyNameInCaseSensitiveManner()
        {
            // Arrange
            var merger = new InjectChunkMerger("dynamic");
            var codeTree = new CodeTree();

            // Act
            merger.VisitChunk(new InjectChunk("MyType", "MyProperty"));
            merger.Merge(codeTree, new InjectChunk("MyType", "myproperty"));
            merger.Merge(codeTree, new InjectChunk("MyTypeB", "different-property"));

            // Assert
            Assert.Equal(2, codeTree.Chunks.Count);
            var injectChunk = Assert.IsType<InjectChunk>(codeTree.Chunks[0]);
            Assert.Equal("MyType", injectChunk.TypeName);
            Assert.Equal("myproperty", injectChunk.MemberName);

            injectChunk = Assert.IsType<InjectChunk>(codeTree.Chunks[1]);
            Assert.Equal("MyTypeB", injectChunk.TypeName);
            Assert.Equal("different-property", injectChunk.MemberName);
        }
コード例 #21
0
        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);
        }
コード例 #22
0
 private static Dictionary<Type, IChunkMerger> GetMergerMappings(CodeTree codeTree, string defaultModel)
 {
     var modelType = ChunkHelper.GetModelTypeName(codeTree, defaultModel);
     return new Dictionary<Type, IChunkMerger>
     {
         { typeof(UsingChunk), new UsingChunkMerger() },
         { typeof(InjectChunk), new InjectChunkMerger(modelType) },
         { typeof(SetBaseTypeChunk), new SetBaseTypeChunkMerger(modelType) }
     };
 }