Example #1
0
        public void WriteTemplate(CodeRenderingContext context, TemplateIntermediateNode node)
        {
            const string ItemParameterName  = "item";
            const string TemplateWriterName = "__razor_template_writer";

            context.CodeWriter
            .Write(ItemParameterName).Write(" => ")
            .WriteStartNewObject(TemplateTypeName);

            using (context.CodeWriter.BuildAsyncLambda(TemplateWriterName))
            {
                context.NodeWriter.BeginWriterScope(context, TemplateWriterName);

                context.RenderChildren(node);

                context.NodeWriter.EndWriterScope(context);
            }

            context.CodeWriter.WriteEndMethodInvocation(endLine: false);
        }
        public void WriteTemplate_WritesTemplateCode()
        {
            // Arrange
            var node = new TemplateIntermediateNode()
            {
                Children =
                {
                    new CSharpExpressionIntermediateNode()
                }
            };
            var extension = new TemplateTargetExtension()
            {
                TemplateTypeName = "global::TestTemplate"
            };

            var nodeWriter = new RuntimeNodeWriter()
            {
                PushWriterMethod = "TestPushWriter",
                PopWriterMethod  = "TestPopWriter"
            };

            var context = TestCodeRenderingContext.CreateRuntime(nodeWriter: nodeWriter);

            // Act
            extension.WriteTemplate(context, node);

            // Assert
            var expected = @"item => new global::TestTemplate(async(__razor_template_writer) => {
    TestPushWriter(__razor_template_writer);
    Render Children
    TestPopWriter();
}
)";

            var output = context.CodeWriter.GenerateCode();

            Assert.Equal(expected, output);
        }