public void WriteCSharpExpression_WithSource_WritesPadding() { // Arrange var codeWriter = new CodeWriter(); var writer = new RuntimeNodeWriter() { WriteCSharpExpressionMethod = "Test", }; var context = TestCodeRenderingContext.CreateRuntime(); var node = new CSharpExpressionIntermediateNode() { Source = new SourceSpan("test.cshtml", 8, 0, 8, 3, 0, 11), }; 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( @" #nullable restore #line (1,9)-(1,12) 5 ""test.cshtml"" Test(iRender Children ++); #line default #line hidden #nullable disable ", csharp, ignoreLineEndingDifferences: true); }
public void WriteHtmlLiteral_WithinMaxSize_WritesSingleLiteral() { // Arrange var codeWriter = new CodeWriter(); var writer = new RuntimeNodeWriter(); var context = TestCodeRenderingContext.CreateRuntime(); // Act writer.WriteHtmlLiteral(context, maxStringLiteralLength: 6, "Hello"); // Assert var csharp = context.CodeWriter.GenerateCode(); Assert.Equal( @"WriteLiteral(""Hello""); ", csharp, ignoreLineEndingDifferences: true); }
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); }
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); }
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); }
public static CodeRenderingContext CreateRuntime( string newLineString = null, string suppressUniqueIds = "test", RazorSourceDocument source = null, IntermediateNodeWriter nodeWriter = null) { var codeWriter = new CodeWriter(); var documentNode = new DocumentIntermediateNode(); var options = RazorCodeGenerationOptions.CreateDefault(); if (source == 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 == null) { nodeWriter = new RuntimeNodeWriter(); } var context = new DefaultCodeRenderingContext(codeWriter, nodeWriter, codeDocument, documentNode, options); context.Visitor = new RenderChildrenVisitor(context); return(context); }
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); }
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 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); }
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); }
public void WriteHtmlAttributeValue_RendersCorrectly() { // Arrange var writer = new RuntimeNodeWriter(); 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( @"WriteAttributeValue("""", 16, ""hello-world"", 16, 11, true); ", csharp, ignoreLineEndingDifferences: true); }
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); }
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); }