diff --git a/test/Microsoft.AspNetCore.Razor.Language.Test/Legacy/CSharpTemplateTest.cs b/test/Microsoft.AspNetCore.Razor.Language.Test/Legacy/CSharpTemplateTest.cs index 0631ff1c7f..1b69d3750c 100644 --- a/test/Microsoft.AspNetCore.Razor.Language.Test/Legacy/CSharpTemplateTest.cs +++ b/test/Microsoft.AspNetCore.Razor.Language.Test/Legacy/CSharpTemplateTest.cs @@ -8,314 +8,88 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy { public class CSharpTemplateTest : CsHtmlCodeParserTestBase { - private const string TestTemplateCode = " @

Foo #@item

"; - - private TemplateBlock TestTemplate() + public CSharpTemplateTest() { - return new TemplateBlock( - new MarkupBlock( - Factory.MarkupTransition(), - new MarkupTagBlock( - Factory.Markup("

").Accepts(AcceptedCharactersInternal.None)), - Factory.Markup("Foo #"), - new ExpressionBlock( - Factory.CodeTransition(), - Factory.Code("item") - .AsImplicitExpression(CSharpCodeParser.DefaultKeywords) - .Accepts(AcceptedCharactersInternal.NonWhiteSpace) - ), - new MarkupTagBlock( - Factory.Markup("

").Accepts(AcceptedCharactersInternal.None)) - ) - ); + UseBaselineTests = true; } - private const string TestNestedTemplateCode = " @

Foo #@Html.Repeat(10, @

@item

)

"; - - private TemplateBlock TestNestedTemplate() - { - return new TemplateBlock( - new MarkupBlock( - Factory.MarkupTransition(), - new MarkupTagBlock( - Factory.Markup("

").Accepts(AcceptedCharactersInternal.None)), - Factory.Markup("Foo #"), - new ExpressionBlock( - Factory.CodeTransition(), - Factory.Code("Html.Repeat(10, ") - .AsImplicitExpression(CSharpCodeParser.DefaultKeywords), - new TemplateBlock( - new MarkupBlock( - Factory.MarkupTransition(), - new MarkupTagBlock( - Factory.Markup("

").Accepts(AcceptedCharactersInternal.None)), - Factory.EmptyHtml(), - new ExpressionBlock( - Factory.CodeTransition(), - Factory.Code("item") - .AsImplicitExpression(CSharpCodeParser.DefaultKeywords) - .Accepts(AcceptedCharactersInternal.NonWhiteSpace) - ), - new MarkupTagBlock( - Factory.Markup("

").Accepts(AcceptedCharactersInternal.None)) - ) - ), - Factory.Code(")") - .AsImplicitExpression(CSharpCodeParser.DefaultKeywords) - .Accepts(AcceptedCharactersInternal.NonWhiteSpace) - ), - new MarkupTagBlock( - Factory.Markup("

").Accepts(AcceptedCharactersInternal.None)) - ) - ); - } [Fact] public void ParseBlockHandlesSingleLineTemplate() { - ParseBlockTest("{ var foo = @: bar" + Environment.NewLine - + "; }", - new StatementBlock( - Factory.MetaCode("{").Accepts(AcceptedCharactersInternal.None), - Factory.Code(" var foo = ") - .AsStatement() - .AutoCompleteWith(autoCompleteString: null), - new TemplateBlock( - new MarkupBlock( - Factory.MarkupTransition(), - Factory.MetaMarkup(":", HtmlSymbolType.Colon), - Factory.Markup(" bar" + Environment.NewLine) - .With(new SpanEditHandler(CSharpLanguageCharacteristics.Instance.TokenizeString)) - .Accepts(AcceptedCharactersInternal.None) - ) - ), - Factory.Code("; ").AsStatement(), - Factory.MetaCode("}").Accepts(AcceptedCharactersInternal.None) - )); + ParseBlockTest("{ var foo = @: bar" + Environment.NewLine + "; }"); } [Fact] public void ParseBlockHandlesSingleLineImmediatelyFollowingStatementChar() { - ParseBlockTest("{i@: bar" + Environment.NewLine - + "}", - new StatementBlock( - Factory.MetaCode("{").Accepts(AcceptedCharactersInternal.None), - Factory.Code("i") - .AsStatement() - .AutoCompleteWith(autoCompleteString: null), - new TemplateBlock( - new MarkupBlock( - Factory.MarkupTransition(), - Factory.MetaMarkup(":", HtmlSymbolType.Colon), - Factory.Markup(" bar" + Environment.NewLine) - .With(new SpanEditHandler(CSharpLanguageCharacteristics.Instance.TokenizeString)) - .Accepts(AcceptedCharactersInternal.None) - ) - ), - Factory.EmptyCSharp().AsStatement(), - Factory.MetaCode("}").Accepts(AcceptedCharactersInternal.None) - )); + ParseBlockTest("{i@: bar" + Environment.NewLine + "}"); } [Fact] public void ParseBlockHandlesSimpleTemplateInExplicitExpressionParens() { - ParseBlockTest("(Html.Repeat(10," + TestTemplateCode + "))", - new ExpressionBlock( - Factory.MetaCode("(").Accepts(AcceptedCharactersInternal.None), - Factory.Code("Html.Repeat(10, ").AsExpression(), - TestTemplate(), - Factory.Code(")").AsExpression(), - Factory.MetaCode(")").Accepts(AcceptedCharactersInternal.None) - )); + ParseBlockTest("(Html.Repeat(10, @

Foo #@item

))"); } [Fact] public void ParseBlockHandlesSimpleTemplateInImplicitExpressionParens() { - ParseBlockTest("Html.Repeat(10," + TestTemplateCode + ")", - new ExpressionBlock( - Factory.Code("Html.Repeat(10, ") - .AsImplicitExpression(CSharpCodeParser.DefaultKeywords), - TestTemplate(), - Factory.Code(")") - .AsImplicitExpression(CSharpCodeParser.DefaultKeywords) - .Accepts(AcceptedCharactersInternal.NonWhiteSpace) - )); + ParseBlockTest("Html.Repeat(10, @

Foo #@item

)"); } [Fact] public void ParseBlockHandlesTwoTemplatesInImplicitExpressionParens() { - ParseBlockTest("Html.Repeat(10," + TestTemplateCode + "," + TestTemplateCode + ")", - new ExpressionBlock( - Factory.Code("Html.Repeat(10, ") - .AsImplicitExpression(CSharpCodeParser.DefaultKeywords), - TestTemplate(), - Factory.Code(", ") - .AsImplicitExpression(CSharpCodeParser.DefaultKeywords), - TestTemplate(), - Factory.Code(")") - .AsImplicitExpression(CSharpCodeParser.DefaultKeywords).Accepts(AcceptedCharactersInternal.NonWhiteSpace) - )); + ParseBlockTest("Html.Repeat(10, @

Foo #@item

, @

Foo #@item

)"); } [Fact] public void ParseBlockProducesErrorButCorrectlyParsesNestedTemplateInImplicitExpressionParens() { - ParseBlockTest("Html.Repeat(10," + TestNestedTemplateCode + ")", - new ExpressionBlock( - Factory.Code("Html.Repeat(10, ") - .AsImplicitExpression(CSharpCodeParser.DefaultKeywords), - TestNestedTemplate(), - Factory.Code(")") - .AsImplicitExpression(CSharpCodeParser.DefaultKeywords) - .Accepts(AcceptedCharactersInternal.NonWhiteSpace) - ), - GetNestedTemplateError(42)); + ParseBlockTest("Html.Repeat(10, @

Foo #@Html.Repeat(10, @

@item

)

)"); } [Fact] public void ParseBlockHandlesSimpleTemplateInStatementWithinCodeBlock() { - ParseBlockTest("foreach(foo in Bar) { Html.ExecuteTemplate(foo," + TestTemplateCode + "); }", - new StatementBlock( - Factory.Code("foreach(foo in Bar) { Html.ExecuteTemplate(foo, ").AsStatement(), - TestTemplate(), - Factory.Code("); }") - .AsStatement() - .Accepts(AcceptedCharactersInternal.None) - )); + ParseBlockTest("foreach(foo in Bar) { Html.ExecuteTemplate(foo, @

Foo #@item

); }"); } [Fact] public void ParseBlockHandlesTwoTemplatesInStatementWithinCodeBlock() { - ParseBlockTest("foreach(foo in Bar) { Html.ExecuteTemplate(foo," + TestTemplateCode + "," + TestTemplateCode + "); }", - new StatementBlock( - Factory.Code("foreach(foo in Bar) { Html.ExecuteTemplate(foo, ").AsStatement(), - TestTemplate(), - Factory.Code(", ").AsStatement(), - TestTemplate(), - Factory.Code("); }") - .AsStatement() - .Accepts(AcceptedCharactersInternal.None) - )); + ParseBlockTest("foreach(foo in Bar) { Html.ExecuteTemplate(foo, @

Foo #@item

, @

Foo #@item

); }"); } [Fact] public void ParseBlockProducesErrorButCorrectlyParsesNestedTemplateInStatementWithinCodeBlock() { - ParseBlockTest("foreach(foo in Bar) { Html.ExecuteTemplate(foo," + TestNestedTemplateCode + "); }", - new StatementBlock( - Factory.Code("foreach(foo in Bar) { Html.ExecuteTemplate(foo, ") - .AsStatement(), - TestNestedTemplate(), - Factory.Code("); }") - .AsStatement() - .Accepts(AcceptedCharactersInternal.None) - ), - GetNestedTemplateError(74)); + ParseBlockTest("foreach(foo in Bar) { Html.ExecuteTemplate(foo, @

Foo #@Html.Repeat(10, @

@item

)

); }"); } [Fact] public void ParseBlockHandlesSimpleTemplateInStatementWithinStatementBlock() { - ParseBlockTest("{ var foo = bar; Html.ExecuteTemplate(foo," + TestTemplateCode + "); }", - new StatementBlock( - Factory.MetaCode("{").Accepts(AcceptedCharactersInternal.None), - Factory.Code(" var foo = bar; Html.ExecuteTemplate(foo, ") - .AsStatement() - .AutoCompleteWith(autoCompleteString: null), - TestTemplate(), - Factory.Code("); ").AsStatement(), - Factory.MetaCode("}").Accepts(AcceptedCharactersInternal.None) - )); + ParseBlockTest("{ var foo = bar; Html.ExecuteTemplate(foo, @

Foo #@item

); }"); } [Fact] public void ParseBlockHandlessTwoTemplatesInStatementWithinStatementBlock() { - ParseBlockTest("{ var foo = bar; Html.ExecuteTemplate(foo," + TestTemplateCode + "," + TestTemplateCode + "); }", - new StatementBlock( - Factory.MetaCode("{").Accepts(AcceptedCharactersInternal.None), - Factory.Code(" var foo = bar; Html.ExecuteTemplate(foo, ") - .AsStatement() - .AutoCompleteWith(autoCompleteString: null), - TestTemplate(), - Factory.Code(", ").AsStatement(), - TestTemplate(), - Factory.Code("); ").AsStatement(), - Factory.MetaCode("}").Accepts(AcceptedCharactersInternal.None) - )); + ParseBlockTest("{ var foo = bar; Html.ExecuteTemplate(foo, @

Foo #@item

, @

Foo #@item

); }"); } [Fact] public void ParseBlockProducesErrorButCorrectlyParsesNestedTemplateInStatementWithinStatementBlock() { - ParseBlockTest("{ var foo = bar; Html.ExecuteTemplate(foo," + TestNestedTemplateCode + "); }", - new StatementBlock( - Factory.MetaCode("{").Accepts(AcceptedCharactersInternal.None), - Factory.Code(" var foo = bar; Html.ExecuteTemplate(foo, ") - .AsStatement() - .AutoCompleteWith(autoCompleteString: null), - TestNestedTemplate(), - Factory.Code("); ").AsStatement(), - Factory.MetaCode("}").Accepts(AcceptedCharactersInternal.None) - ), - GetNestedTemplateError(69)); + ParseBlockTest("{ var foo = bar; Html.ExecuteTemplate(foo, @

Foo #@Html.Repeat(10, @

@item

)

); }"); } [Fact] public void ParseBlock_WithDoubleTransition_DoesNotThrow() { - FixupSpans = true; - - // Arrange - var testTemplateWithDoubleTransitionCode = " @

Foo #@item

"; - var testTemplateWithDoubleTransition = new TemplateBlock( - new MarkupBlock( - Factory.MarkupTransition(), - new MarkupTagBlock( - Factory.Markup("(" foo='", 46, 0, 46), new LocationTagged("'", 54, 0, 54)), - Factory.Markup(" foo='").With(SpanChunkGenerator.Null), - new MarkupBlock( - Factory.Markup("@").With(new LiteralAttributeChunkGenerator(new LocationTagged(string.Empty, 52, 0, 52), new LocationTagged("@", 52, 0, 52))).Accepts(AcceptedCharactersInternal.None), - Factory.Markup("@").With(SpanChunkGenerator.Null).Accepts(AcceptedCharactersInternal.None)), - Factory.Markup("'").With(SpanChunkGenerator.Null)), - Factory.Markup(">").Accepts(AcceptedCharactersInternal.None)), - Factory.Markup("Foo #"), - new ExpressionBlock( - Factory.CodeTransition(), - Factory.Code("item") - .AsImplicitExpression(CSharpCodeParser.DefaultKeywords) - .Accepts(AcceptedCharactersInternal.NonWhiteSpace) - ), - new MarkupTagBlock( - Factory.Markup("

").Accepts(AcceptedCharactersInternal.None)) - ) - ); - - var expected = new StatementBlock( - Factory.MetaCode("{").Accepts(AcceptedCharactersInternal.None), - Factory.Code(" var foo = bar; Html.ExecuteTemplate(foo, ") - .AsStatement() - .AutoCompleteWith(autoCompleteString: null), - testTemplateWithDoubleTransition, - Factory.Code("); ").AsStatement(), - Factory.MetaCode("}").Accepts(AcceptedCharactersInternal.None)); - - // Act & Assert - ParseBlockTest("{ var foo = bar; Html.ExecuteTemplate(foo," + testTemplateWithDoubleTransitionCode + "); }", expected); - } - - private static RazorDiagnostic GetNestedTemplateError(int characterIndex) - { - return RazorDiagnosticFactory.CreateParsing_InlineMarkupBlocksCannotBeNested( - new SourceSpan(new SourceLocation(characterIndex, 0, characterIndex), contentLength: 1)); + ParseBlockTest("{ var foo = bar; Html.ExecuteTemplate(foo, @

Foo #@item

); }"); } } } diff --git a/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/ParserTests/CSharpTemplateTest/ParseBlockHandlesSimpleTemplateInExplicitExpressionParens.syntaxtree.txt b/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/ParserTests/CSharpTemplateTest/ParseBlockHandlesSimpleTemplateInExplicitExpressionParens.syntaxtree.txt new file mode 100644 index 0000000000..ec95fa14ec --- /dev/null +++ b/test/Microsoft.AspNetCore.Razor.Language.Test/TestFiles/ParserTests/CSharpTemplateTest/ParseBlockHandlesSimpleTemplateInExplicitExpressionParens.syntaxtree.txt @@ -0,0 +1,39 @@ +Expression block - Gen - 37 - (0:0,0) + MetaCode span - Gen - [(] - SpanEditHandler;Accepts:None - (0:0,0) - Symbols:1 + CSharpSymbolType.LeftParenthesis;[(]; + Code span - Gen - [Html.Repeat(10, ] - SpanEditHandler;Accepts:Any - (1:0,1) - Symbols:7 + CSharpSymbolType.Identifier;[Html]; + CSharpSymbolType.Dot;[.]; + CSharpSymbolType.Identifier;[Repeat]; + CSharpSymbolType.LeftParenthesis;[(]; + CSharpSymbolType.IntegerLiteral;[10]; + CSharpSymbolType.Comma;[,]; + CSharpSymbolType.WhiteSpace;[ ]; + Template block - Gen