public void WriteHtmlLiteral_GreaterThanMaxSize_WritesMultipleLiterals()
        {
            // Arrange
            var codeWriter = new CodeWriter();
            var writer     = new RuntimeNodeWriter();
            var context    = TestCodeRenderingContext.CreateRuntime();

            // Act
            writer.WriteHtmlLiteral(context, maxStringLiteralLength: 6, "Hello World");

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

            Assert.Equal(
                @"WriteLiteral(""Hello "");
WriteLiteral(""World"");
",
                csharp,
                ignoreLineEndingDifferences: true);
        }
Пример #2
0
        public void WriteHtmlLiteral_GreaterThanMaxSize_SingleEmojisSplit()
        {
            // Arrange
            var codeWriter = new CodeWriter();
            var writer     = new RuntimeNodeWriter();
            var context    = TestCodeRenderingContext.CreateRuntime();

            // Act
            writer.WriteHtmlLiteral(context, maxStringLiteralLength: 2, " 👦");

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

            Assert.Equal(
                @"WriteLiteral("" "");
WriteLiteral(""👦"");
",
                csharp,
                ignoreLineEndingDifferences: true);
        }
Пример #3
0
        public void WriteCSharpExpression_WritesLinePragma_WithSource()
        {
            // Arrange
            var codeWriter = new CodeWriter();
            var writer     = new RuntimeNodeWriter()
            {
                WriteCSharpExpressionMethod = "Test",
            };
            var context = TestCodeRenderingContext.CreateRuntime();

            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(
                @"
#nullable restore
#line 1 ""test.cshtml""
Test(i++);

#line default
#line hidden
#nullable disable
",
                csharp,
                ignoreLineEndingDifferences: true);
        }
Пример #4
0
        public void EndWriterScope_RendersCorrectly()
        {
            // Arrange
            var writer = new RuntimeNodeWriter()
            {
                PopWriterMethod = "TestPopWriter"
            };
            var context = TestCodeRenderingContext.CreateRuntime();

            // Act
            writer.EndWriterScope(context);

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

            Assert.Equal(
                @"TestPopWriter();
",
                csharp,
                ignoreLineEndingDifferences: true);
        }
Пример #5
0
        public void BeginWriterScope_UsesSpecifiedWriter_RendersCorrectly()
        {
            // Arrange
            var writer = new RuntimeNodeWriter()
            {
                PushWriterMethod = "TestPushWriter"
            };
            var context = TestCodeRenderingContext.CreateRuntime();

            // Act
            writer.BeginWriterScope(context, "MyWriter");

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

            Assert.Equal(
                @"TestPushWriter(MyWriter);
",
                csharp,
                ignoreLineEndingDifferences: true);
        }
Пример #6
0
        public void WriteCSharpCode_WritesPadding_WithSource()
        {
            // Arrange
            var codeWriter = new CodeWriter();
            var writer     = new RuntimeNodeWriter();
            var context    = TestCodeRenderingContext.CreateRuntime();

            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(
                @"
#nullable restore
#line 1 ""test.cshtml""
    if (true) { }

#line default
#line hidden
#nullable disable
",
                csharp,
                ignoreLineEndingDifferences: true);
        }
Пример #7
0
        public void WriteCSharpExpression_WithExtensionNode_WritesPadding()
        {
            // Arrange
            var codeWriter = new CodeWriter();
            var writer     = new RuntimeNodeWriter()
            {
                WriteCSharpExpressionMethod = "Test",
            };
            var context = TestCodeRenderingContext.CreateRuntime();

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

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

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

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

            Assert.Equal(
                @"Test(iRender Children
++);
",
                csharp,
                ignoreLineEndingDifferences: true);
        }
        public void WriteHtmlAttributeValue_RendersCorrectly()
        {
            var writer = new TagHelperHtmlAttributeRuntimeNodeWriter();

            var content        = "<input checked=\"hello-world @false\" />";
            var sourceDocument = TestRazorSourceDocument.Create(content);
            var codeDocument   = RazorCodeDocument.Create(sourceDocument);
            var documentNode   = Lower(codeDocument);
            var node           = documentNode.Children.OfType <HtmlAttributeIntermediateNode>().Single().Children[0] as HtmlAttributeValueIntermediateNode;

            var context = TestCodeRenderingContext.CreateRuntime();

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

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

            Assert.Equal(
                @"AddHtmlAttributeValue("""", 16, ""hello-world"", 16, 11, true);
",
                csharp,
                ignoreLineEndingDifferences: true);
        }
Пример #9
0
        public void WriteCSharpCodeAttributeValue_BuffersResult()
        {
            // Arrange
            var writer = new RuntimeNodeWriter();

            var content        = "<input checked=\"hello-world @if(@true){ }\" />";
            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.CreateRuntime(source: sourceDocument);

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

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

            Assert.Equal(
                @"WriteAttributeValue("" "", 27, new Microsoft.AspNetCore.Mvc.Razor.HelperResult(async(__razor_attribute_value_writer) => {
    PushWriter(__razor_attribute_value_writer);
#nullable restore
#line 1 ""test.cshtml""
                             if(@true){ }

#line default
#line hidden
#nullable disable
    PopWriter();
}
), 28, 13, false);
",
                csharp,
                ignoreLineEndingDifferences: true);
        }
Пример #10
0
        public void WriteCSharpCode_WhitespaceContent_DoesNothing()
        {
            // Arrange
            var codeWriter = new CodeWriter();
            var writer     = new RuntimeNodeWriter();
            var context    = TestCodeRenderingContext.CreateRuntime();

            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);
        }
Пример #11
0
        public void WriteUsingDirective_NoSource_WritesContent()
        {
            // Arrange
            var codeWriter = new CodeWriter();
            var writer     = new RuntimeNodeWriter();
            var context    = TestCodeRenderingContext.CreateRuntime();

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

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

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

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