public void WriteCSharpExpression_SkipsLinePragma_WithoutSource()
        {
            // Arrange
            var writer  = new DesignTimeNodeWriter();
            var context = TestCodeRenderingContext.CreateDesignTime();

            var node    = new CSharpExpressionIntermediateNode();
            var builder = IntermediateNodeBuilder.Create(node);

            builder.Add(new IntermediateToken()
            {
                Content = "i++",
                Kind    = TokenKind.CSharp,
            });

            // Act
            writer.WriteCSharpExpression(context, node);

            // Assert
            var csharp = context.CodeWriter.GenerateCode();

            Assert.Equal(
                @"__o = i++;
",
                csharp,
                ignoreLineEndingDifferences: true);
        }
        public void WriteUsingDirective_WithSource_WritesContentWithLinePragmaAndMapping()
        {
            // Arrange
            var writer         = new DesignTimeNodeWriter();
            var sourceDocument = TestRazorSourceDocument.Create("@using System;");
            var context        = TestCodeRenderingContext.CreateDesignTime();

            var originalSpan          = new SourceSpan("test.cshtml", 0, 0, 0, 6);
            var generatedSpan         = new SourceSpan(null, 21 + Environment.NewLine.Length, 1, 0, 6);
            var expectedSourceMapping = new SourceMapping(originalSpan, generatedSpan);
            var node = new UsingDirectiveIntermediateNode()
            {
                Content = "System",
                Source  = originalSpan,
            };

            // Act
            writer.WriteUsingDirective(context, node);

            // Assert
            var mapping = Assert.Single(((DefaultCodeRenderingContext)context).SourceMappings);

            Assert.Equal(expectedSourceMapping, mapping);
            var csharp = context.CodeWriter.GenerateCode();

            Assert.Equal(
                @"#line 1 ""test.cshtml""
using System;

#line default
#line hidden
",
                csharp,
                ignoreLineEndingDifferences: true);
        }
        public void WriteCSharpCodeAttributeValue_WithExpression_RendersCorrectly()
        {
            var writer         = new DesignTimeNodeWriter();
            var content        = "<input checked=\"hello-world @if(@true){ @false }\" />";
            var sourceDocument = TestRazorSourceDocument.Create(content);
            var codeDocument   = RazorCodeDocument.Create(sourceDocument);
            var documentNode   = Lower(codeDocument);
            var node           = documentNode.Children.OfType <HtmlAttributeIntermediateNode>().Single().Children[1] as CSharpCodeAttributeValueIntermediateNode;

            var context = TestCodeRenderingContext.CreateDesignTime(source: sourceDocument);

            // Act
            writer.WriteCSharpCodeAttributeValue(context, node);

            // Assert
            var csharp = context.CodeWriter.GenerateCode();

            Assert.Equal(
                @"#line 1 ""test.cshtml""
                             if(@true){ 

#line default
#line hidden
Render Children
#line 1 ""test.cshtml""
                                               }

#line default
#line hidden
",
                csharp,
                ignoreLineEndingDifferences: true);
        }
        public void WriteCSharpCode_SkipsLinePragma_WithoutSource()
        {
            // Arrange
            var writer  = new DesignTimeNodeWriter();
            var context = TestCodeRenderingContext.CreateDesignTime();

            var node = new CSharpCodeIntermediateNode();

            IntermediateNodeBuilder.Create(node)
            .Add(new IntermediateToken()
            {
                Kind    = TokenKind.CSharp,
                Content = "if (true) { }"
            });

            // Act
            writer.WriteCSharpCode(context, node);

            // Assert
            var csharp = context.CodeWriter.GenerateCode();

            Assert.Equal(
                @"if (true) { }
",
                csharp,
                ignoreLineEndingDifferences: true);
        }
        public void WriteCSharpCode_WritesPadding_WithSource()
        {
            // Arrange
            var writer  = new DesignTimeNodeWriter();
            var context = TestCodeRenderingContext.CreateDesignTime();

            var node = new CSharpCodeIntermediateNode()
            {
                Source = new SourceSpan("test.cshtml", 0, 0, 0, 17),
            };

            IntermediateNodeBuilder.Create(node)
            .Add(new IntermediateToken()
            {
                Kind    = TokenKind.CSharp,
                Content = "    if (true) { }",
            });

            // Act
            writer.WriteCSharpCode(context, node);

            // Assert
            var csharp = context.CodeWriter.GenerateCode();

            Assert.Equal(
                @"#line 1 ""test.cshtml""
    if (true) { }

#line default
#line hidden
",
                csharp,
                ignoreLineEndingDifferences: true);
        }
        public void WriteCSharpCode_WhitespaceContentWithSource_WritesContent()
        {
            // Arrange
            var writer  = new DesignTimeNodeWriter();
            var context = TestCodeRenderingContext.CreateDesignTime();

            var node = new CSharpCodeIntermediateNode()
            {
                Source = new SourceSpan("test.cshtml", 0, 0, 0, 3),
            };

            IntermediateNodeBuilder.Create(node)
            .Add(new IntermediateToken()
            {
                Kind    = TokenKind.CSharp,
                Content = "    "
            });

            // Act
            writer.WriteCSharpCode(context, node);

            // Assert
            var csharp = context.CodeWriter.GenerateCode();

            Assert.Equal(
                @"    
",
                csharp,
                ignoreLineEndingDifferences: true);
        }
        public void WriteCSharpExpression_WritesLinePragma_WithSource()
        {
            // Arrange
            var writer  = new DesignTimeNodeWriter();
            var context = TestCodeRenderingContext.CreateDesignTime();

            var node = new CSharpExpressionIntermediateNode()
            {
                Source = new SourceSpan("test.cshtml", 0, 0, 0, 3),
            };
            var builder = IntermediateNodeBuilder.Create(node);

            builder.Add(new IntermediateToken()
            {
                Content = "i++",
                Kind    = TokenKind.CSharp,
            });

            // Act
            writer.WriteCSharpExpression(context, node);

            // Assert
            var csharp = context.CodeWriter.GenerateCode();

            Assert.Equal(
                @"#line 1 ""test.cshtml""
__o = i++;

#line default
#line hidden
",
                csharp,
                ignoreLineEndingDifferences: true);
        }
        public static CodeRenderingContext CreateDesignTime(
            string newLineString              = null,
            string suppressUniqueIds          = "test",
            RazorSourceDocument source        = null,
            IntermediateNodeWriter nodeWriter = null)
        {
            var codeWriter   = new CodeWriter();
            var documentNode = new DocumentIntermediateNode();
            var options      = RazorCodeGenerationOptions.CreateDesignTimeDefault();

            if (source is null)
            {
                source = TestRazorSourceDocument.Create();
            }

            var codeDocument = RazorCodeDocument.Create(source);

            if (newLineString != null)
            {
                codeDocument.Items[CodeRenderingContext.NewLineString] = newLineString;
            }

            if (suppressUniqueIds != null)
            {
                codeDocument.Items[CodeRenderingContext.SuppressUniqueIds] = suppressUniqueIds;
            }

            if (nodeWriter is null)
            {
                nodeWriter = new DesignTimeNodeWriter();
            }

            var context = new DefaultCodeRenderingContext(codeWriter, nodeWriter, codeDocument, documentNode, options);

            context.Visitor = new RenderChildrenVisitor(context);

            return(context);
        }
        public void WriteCSharpCode_WhitespaceContent_DoesNothing()
        {
            // Arrange
            var writer  = new DesignTimeNodeWriter();
            var context = TestCodeRenderingContext.CreateDesignTime();

            var node = new CSharpCodeIntermediateNode();

            IntermediateNodeBuilder.Create(node)
            .Add(new IntermediateToken()
            {
                Kind    = TokenKind.CSharp,
                Content = "  \t"
            });

            // Act
            writer.WriteCSharpCode(context, node);

            // Assert
            var csharp = context.CodeWriter.GenerateCode();

            Assert.Empty(csharp);
        }
        public void WriteUsingDirective_NoSource_WritesContent()
        {
            // Arrange
            var writer  = new DesignTimeNodeWriter();
            var context = TestCodeRenderingContext.CreateDesignTime();

            var node = new UsingDirectiveIntermediateNode()
            {
                Content = "System",
            };

            // Act
            writer.WriteUsingDirective(context, node);

            // Assert
            var csharp = context.CodeWriter.GenerateCode();

            Assert.Equal(
                @"using System;
",
                csharp,
                ignoreLineEndingDifferences: true);
        }