public void Accept_CorrectlyGeneratesCode(IList <Chunk> chunks)
        {
            // Arrange
            var writer       = new CSharpCodeWriter();
            var context      = ChunkVisitorTestFactory.CreateCodeGeneratorContext();
            var chunkVisitor = new ViewComponentTagHelperChunkVisitor(writer, context);

            var assembly       = typeof(ViewComponentTagHelperChunkVisitorTest).GetTypeInfo().Assembly;
            var path           = "TestFiles/Output/Runtime/GeneratedViewComponentTagHelperClasses.cs";
            var expectedOutput = ResourceFile.ReadResource(assembly, path, sourceFile: true);

            // Act
            chunkVisitor.Accept(chunks);
            var resultOutput = writer.GenerateCode();

#if GENERATE_BASELINES
            if (!string.Equals(expectedOutput, resultOutput, System.StringComparison.Ordinal))
            {
                ResourceFile.UpdateFile(assembly, path, expectedOutput, resultOutput);
            }
#else
            // Assert
            Assert.Equal(expectedOutput, resultOutput, ignoreLineEndingDifferences: true);
#endif
        }
Exemple #2
0
        public void Accept_CorrectlyDecoratesViewComponentChunks()
        {
            // Arrange
            var codeGeneratorContext = ChunkVisitorTestFactory.CreateCodeGeneratorContext();
            var syntaxTreeNode       = new Mock <Span>(new SpanBuilder());

            foreach (var chunk in ChunkVisitorTestFactory.GetTestChunks(visitedTagHelperChunks: false))
            {
                codeGeneratorContext.ChunkTreeBuilder.AddChunk(chunk, syntaxTreeNode.Object);
            }

            var tagHelperChunkDecorator = new TagHelperChunkDecorator(codeGeneratorContext);
            var expectedChunks          = ChunkVisitorTestFactory.GetTestChunks(visitedTagHelperChunks: true);

            // Act
            var resultChunks = codeGeneratorContext.ChunkTreeBuilder.Root.Children;

            tagHelperChunkDecorator.Accept(resultChunks);

            // Assert
            // Test the normal tag helper chunk, Baz.
            Assert.Equal(expectedChunks.Count, resultChunks.Count);

            var expectedTagHelperChunk = (TagHelperChunk)expectedChunks[0];
            var resultTagHelperChunk   = Assert.IsType <TagHelperChunk>(resultChunks[0]);

            Assert.Equal(
                expectedTagHelperChunk.Descriptors.First().TypeName,
                Assert.Single(resultTagHelperChunk.Descriptors).TypeName);

            // Test the parent chunk with view component tag helper inside, Foo.
            var expectedParentChunk = (ParentChunk)expectedChunks[1];
            var resultParentChunk   = Assert.IsType <TagHelperChunk>(resultChunks[1]);

            Assert.Single(resultParentChunk.Children);

            expectedTagHelperChunk = (TagHelperChunk)expectedParentChunk.Children.First();
            resultTagHelperChunk   = (TagHelperChunk)resultParentChunk.Children.First();

            Assert.Equal(expectedTagHelperChunk.Descriptors.First().TypeName,
                         resultTagHelperChunk.Descriptors.First().TypeName,
                         StringComparer.Ordinal);

            // Test the view component tag helper, Bar.
            expectedTagHelperChunk = expectedChunks.ElementAt(2) as TagHelperChunk;
            resultTagHelperChunk   = resultChunks.ElementAt(2) as TagHelperChunk;
            Assert.NotNull(resultTagHelperChunk);

            Assert.Equal(expectedTagHelperChunk.Descriptors.First().TypeName,
                         resultTagHelperChunk.Descriptors.First().TypeName,
                         StringComparer.Ordinal);
        }